Fix pidfile access

This commit is contained in:
Labrys of Knossos 2022-12-14 03:56:01 -05:00
commit fc3d6a5744

View file

@ -21,9 +21,9 @@ if os.name == 'nt':
class WindowsProcess:
def __init__(self):
self.mutex = None
self.mutexname = 'nzbtomedia_{pid}'.format(
pid=core.PID_FILE.replace('\\', '/'),
) # {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}'
# {D0E858DF-985E-4907-B7FB-8D732C3FC3B9}
_path_str = os.fspath(core.PID_FILE).replace('\\', '/')
self.mutexname = f'nzbtomedia_{_path_str}'
self.CreateMutex = CreateMutex
self.CloseHandle = CloseHandle
self.GetLastError = GetLastError
@ -80,21 +80,15 @@ class PosixProcess:
if not self.lasterror:
# Write my pid into pidFile to keep multiple copies of program from running
try:
fp = open(self.pidpath, 'w')
fp.write(str(os.getpid()))
fp.close()
except Exception:
pass
with self.pidpath.open(mode='w') as fp:
fp.write(os.getpid())
return self.lasterror
def __del__(self):
if not self.lasterror:
if self.lock_socket:
self.lock_socket.close()
if os.path.isfile(self.pidpath):
os.unlink(self.pidpath)
self.pidpath.unlink(missing_ok=True)
ProcessType = typing.Type[typing.Union[PosixProcess, WindowsProcess]]