Conversation
I found a race condition between the methods __setitem__ and __getitem__. Sometimes when the method __getitem__ is called, the file containing the session data is not complete because it is still being written by another thread. I fixed the problem by adding a recursive lock to the class and modifying the methods __setitem__ and __getitem__ so they acquire the lock before trying to read or write the session file.
Race condition in DiskStore
|
That still doesn't solve the issue of half written files. I guess the right way to solve this is by writing to a tmp file first and move the file to actual location. |
|
Anand, |
|
@adrianomarto I'm a bit out of context, but if app crashes in the middle of |
|
@gmelikov, if you look at the code carefully, you will see that the file is closed in the "finally" clause (and it was already like this before my changes). That should suffice to take care of the crash condition, which is not the problem being addressed here. The problem I am trying to solve happens when a thread tries to read the file when the file is still being written. The problem still happens if you move the file from elsewhere instead of writing the file directly to where it belongs. Solving the problem by moving the file form a temporary folder would require the moving operation to be atomic, which I don't dare say to the case in all operating systems. On the other hand, solving the problem by using locks is guaranteed to work, as the locking operation is atomic regardless of the operating system. |
|
@adrianomarto IIRC now DiskStore isn't crashproof too, By default
|
|
@adrianomarto https://docs.python.org/3/library/os.html#os.rename
|
|
Conflicts: This PR needs to be re-based. |
|
Fixed in 058672d |
I found a race condition between the methods setitem and getitem. Sometimes when the method getitem is called, the file containing the session data is not complete because it is still being written by another thread.
I fixed the problem by adding a recursive lock to the class and modifying the methods setitem and getitem so they acquire the lock before trying to read or write the session file.