Re: Race condition on loading packages from 2 maxima processes in parallel?

Gunter K�nigsmann via Maxima-discuss <[email protected]> Mon, 15 Jun 2026 15:45:07 +0200
Newsgroups gmane.comp.mathematics.maxima.general
Message-ID <[email protected]>
Perhaps there is no ideal solution here: Waiting for the process Id that created Thet lock file to finish is OS-specific and still doesn't proof that the process with that ID will finally finish that file. Waiting a minute till it is generated (telling the user that we do so) and if the lock file persists moving the compiled file away and compiling the package ourselves? Might not work on MS windows that doesn't move files if someone else still uses them. 

Kind regards, 

  Gunter


On 14 June 2026 17:10:17 CEST, Raymond Toy <[email protected]> wrote:
>On 6/14/26 7:00 AM, David Scherfgen wrote:
>
>> Gemini proposed this Common Lisp file locking mechanism (but read below):
>> 
>> (defmacro with-file-lock ((lock-file &key (sleep-time 0.1)) &body body)
>>   "Acquires an exclusive lock using purely ANSI Common Lisp atomic file creation."
>>   (let ((stream-sym (gensym "STREAM")))
>>     `(let ((,stream-sym nil))
>>        (unwind-protect
>>            (progn
>>              ;; Spinlock: keep trying to create the file until successful
>>              (loop
>>                (handler-case
>>                    (progn
>>                      (setf ,stream-sym (open ,lock-file
>>                                              :direction :output
>>                                              :if-exists :error
>>  :if-does-not-exist :create))
>>                      (return)) ; Successfully acquired lock; exit the loop
>>                  (file-error ()
>>                    ;; Lock file exists; wait and retry
>>                    (sleep ,sleep-time))))
>>              ;; Execute the compilation or protected code
>>              ,@body)
>>          ;; Cleanup: close the stream and delete the lock file
>>          (when ,stream-sym
>>            (close ,stream-sym)
>>            (ignore-errors (delete-file ,lock-file)))))))
>> 
>> I tried it by having multiple processes do this:
>> 
>> (with-file-lock (".lockfile") (format t "Entered~%") (sleep 30) (format t "Exited~%"))
>> 
>> Seems to work ...
>> There could be a problem, though, when a process crashes / gets killed while it has the lock. Then it won't release the lock, and no process could ever acquire it again.
>> In theory, one could define a "timeout" and say that if the lockfile exists and is too old (check using file-write-date), then the process probably crashed.
>I don’t think that’s terrible. You could limit the number of retries so that it eventually exits with an error. And it’s just a file, so I can always just remove the file myself.
>
>I used to get these kinds of lock file errors in vi and emacs once in a while when the editor crashed while I was editing and/or saving the file.
>
>&#8203;

_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss