LocalFileIdentifiableStore: Fix concurrency issues#591
Open
hpoeche wants to merge 3 commits into
Open
Conversation
Previously the LocalFileIdentifiableStore allowed multiple instances (potentially across multiple processes) to acces the same directory. This can lead to undetected invalid cache entries. Additionally it would increase the overhead for adding thread-safety to the R/W operations as synchronization across multiple instances would be necessary. In order to overcome these limitations, these changes introduce a directory lock mechanism, that only allows one instance of the class to hold a lock on a `.lock` file in the directory. A contex manager is used to secure operations on the directory and prevent concurrent release of the lock. For now this only work on POSIX machines, as it relies on `flock` functionatily which is not provided by Windows.
Until now the `LocalFileIdentifiableStore` had a TOCTOU race condition
in the `add()` and `commit()` methods. After a file was checked
for existance a concurrent running `add()` or `discard()` could still
create or remove the file before the original invocation accesses it.
To fix this, all write accesses to a file need to be mutual exclusive.
In this implementation I chose to use a global `_writing_lock` to
lock write operations to all files of the store, as locking individual
files has no performance benefit, due to serialization of multiple threads
through Pythons GIL.
This implementation prevents the following concurrency issues:
1. Two concurrent `add()`: Instead of silent overwriting, first call will
succeed, second failes with `KeyError`.
2. `commit()` + `discard()`: Instead of `commit()` creating file after
`discard()` deleted it, the file is now certainly discarded.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously the
LocalFileIdentifiableStorehad no access control for concurrent access through the same or multiple instances on the same directory.This caused serious problems especially if this store is used as storage backend for a multi-worker WSGI server.
Issues with multiple instances
Multiple instances of the
LocalFileIdentifiableStorewere allowed to operate on the same directory. The main issue lies in theself._object_cacheof each instance which does not get updated or invalidated if the other instance is modifying the underlying files through a write operation. In order for the caches to be coherent with the persistent content a inter-process cache synchronization would be necessary. This would introduce to much overhead so we decided to lock the access to a directory to a single instance ofLocalFileIdentifiableStore. This also helps to simplify resolving the following issues.Issues with concurrent calls to a single instance
Concurrent calls to writing methods
add()andcommit()currently race in the_write_atomic()method on theos.replace(...)call. Whenever two threads invoke the method with an identifiable that has the same ID but different content both calls succeed. However, the persisted content depends on which thread gets scheduled to execute theos.replace(...)last.This occurs multiple times across the writing functions. For example in the
add()function the time of check (TOC) for existence may be different that the time of use (TOU) where the new file is created. A concurrently executedadd()might be scheduled such that at TOC both threads find no file for the ID so they both create a new file at TOU. Here again the last scheduled thread silently overwrites the content from the first.Changes
In order to fix the multi-instance issue, these changes introduce a directory lock mechanism, that only allows one instance of the class to hold a lock on a
.lockfile in the directory. A context manager is used to secure operations on the directory and prevent concurrent release of the lock.Caveat: For now this only work on POSIX machines, as it relies on
flockfunctionality which is not provided by Windows.To fix concurrent writes from the same store instance, all write accesses to a file need to be mutual exclusive.
In this implementation I chose to use a global
_writing_lockto lock write operations to all files of the store, as locking individual files has no performance benefit, due to serialization of multiple threads through Pythons GIL.This implementation prevents the following concurrency issues:
add(): Instead of silent overwriting, first call will second fails withKeyError.commit()+discard(): Instead ofcommit()creating file afterdiscard()deleted it, the file is now certainly discarded. Depending on the scheduled ordercommit()may raise aKeyError.Fixes #554