Skip to content

Commit

Permalink
Fixed: unlocking and locking didn't work when a multiprocessing
Browse files Browse the repository at this point in the history
process was running (and presumably other conditions).

The fix was to make an explicit flock unlock call, in addition to
closing the file.

I lost hours debugging a ZEO test failure before I figured this out. :/
  • Loading branch information
Jim Fulton committed Jun 18, 2016
1 parent 8e8068f commit 671c916
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Change History
***************

- Fixed: unlocking and locking didn't work when a multiprocessing
process was running (and presumably other conditions).

1.2.0 (2016-06-09)
==================

Expand Down
4 changes: 1 addition & 3 deletions src/zc/lockfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def _lock_file(file):
raise LockError("Couldn't lock %r" % file.name)

def _unlock_file(file):
# File is automatically unlocked on close
pass

fcntl.flock(file.fileno(), fcntl.LOCK_UN)

class LazyHostName(object):
"""Avoid importing socket and calling gethostname() unnecessarily"""
Expand Down
16 changes: 16 additions & 0 deletions src/zc/lockfile/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,22 @@ def lock(locked, before_closing):
'f.lock', '123/myhostname', '')]
assert test_logger.log_entries == expected, test_logger.log_entries

def test_unlock_and_lock_while_multiprocessing_process_running(self):
import multiprocessing

lock = zc.lockfile.LockFile('l')
p = multiprocessing.Process(target=time.sleep, args=(1,))
p.daemon = True
p.start()

# release and re-acquire should work (obviously)
lock.close()
lock = zc.lockfile.LockFile('l')
self.assertTrue(p.is_alive())

lock.close()
p.join()


def test_suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit 671c916

Please sign in to comment.