Search code examples
pythontwistedfilelock

Why are Twisted's DeferredFilesystemLocks not safe for concurrent use?


In the Twisted API for DeferredFilesystemLock, it is stated that deferUntilLocked is not safe for concurrent use.

I would like to understand in what way it is unsafe and what makes it unsafe, in order to ensure that I don't misuse the file locks.


Solution

  • Arguably the method is actually quite safe for concurrent use. If you read the first four lines of the implementation then it's clear that an attempt at concurrent use will immediately raise AlreadyTryingToLockError.

    Perhaps the warning is meant to tell you that you'll get an exception rather than useful locking behavior, though.

    The implementation of that exception should provide a hint about why concurrent use isn't allowed. DeferredFilesystemLock uses some instance attributes, starting with _tryLockCall, to keep track of progress in the attempt to acquire the lock. If concurrent attempts were allowed, they would each trample over the use of this attribute (and others) by each other.

    This could be enhanced with relative ease. All that would be necessary is to keep the state associated with the lock attempt on a new object allocated per-attempt (instead of on the DeferredFilesystemLock instance). Or, DeferredLock could help.