The PidFile class registers close() to be called at process exit time, but if the user manually calls close(), this can lead to a race condition where Instance A of a script closes the pid file and instance Instance B of the same script is allowed to run, but A delete's B's pid file.
Pseudo Code
pid_file = PidFile(...)
try:
pid_file.create()
except Exception:
if not pid_file.filename or not os.path.exists(pid_file.filename):
raise
raise MyAlreadyRunningError
try:
do_something()
finally:
pid_file.close()
# A hasn't called at exit yet, but B is allowed to run
# Shortly later, A exits and A's `atexit` closes the pid file that B creates
It would be possible to not call close(), but we have tests to check for MyAlreadyRunningError and pytest does not exit (therefore never calls the registered atexit handlers) until after the full suite of test has been run.
The PidFile class registers
close()
to be called at process exit time, but if the user manually callsclose()
, this can lead to a race condition where Instance A of a script closes the pid file and instance Instance B of the same script is allowed to run, but A delete's B's pid file.Pseudo Code
It would be possible to not call
close()
, but we have tests to check for MyAlreadyRunningError and pytest does not exit (therefore never calls the registeredatexit
handlers) until after the full suite of test has been run.