Re: Race condition on loading packages from 2 maxima processes in parallel?
Raymond Toy <[email protected]> Sun, 14 Jun 2026 08:10:17 -0700
| Newsgroups | gmane.comp.mathematics.maxima.general |
|---|---|
| Message-ID | <[email protected]> |
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.
​
_______________________________________________
Maxima-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maxima-discuss