Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions pdd/sync_determine_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,18 @@ def acquire(self):
# Create lock file and acquire file descriptor lock
self.lock_file.touch()
self.fd = open(self.lock_file, 'w')

try:
# Critical section - must close file if anything fails
if HAS_FCNTL:
# POSIX systems
fcntl.flock(self.fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
elif HAS_MSVCRT:
# Windows systems
msvcrt.locking(self.fd.fileno(), msvcrt.LK_NBLCK, 1)

# Write current PID to lock file
self.fd.write(str(self.current_pid))
self.fd.flush()
except:
# Close file on ANY exception (not just IOError/OSError)
if self.fd:
self.fd.close()
self.fd = None
raise


if HAS_FCNTL:
# POSIX systems
fcntl.flock(self.fd.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
elif HAS_MSVCRT:
# Windows systems
msvcrt.locking(self.fd.fileno(), msvcrt.LK_NBLCK, 1)

# Write current PID to lock file
self.fd.write(str(self.current_pid))
self.fd.flush()

except (IOError, OSError) as e:
if self.fd:
self.fd.close()
Expand Down
Loading
Loading