Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent concurrent AIOFile.open() calls re-opening the file #69

Merged
merged 4 commits into from
Aug 8, 2022

Conversation

h4l
Copy link
Contributor

@h4l h4l commented Jun 10, 2022

I implemented this with an asyncio.Lock as you suggested ( f6b5b46 ), but then I noticed that _run_in_thread(open, ...) that provides the file obj returns an asyncio.Future, so we can use that to synchronise callers without needing an explicit lock. Either seems fine to me.

Also, I noticed a related minor bug while doing this. It seems the intention of the code was that calling open() after a previous close() would raise an InvalidStateError, but in fact this could never happen, as open() would always return from the first if statement before check to raise the error would happen.

This fixes #68.

h4l added 4 commits June 10, 2022 21:12
AIOFile.open() intended to raise an error if it was called after the
_file_obj had been closed. However in practice it could never happen, as
the function would always return None from the first if statement.

This is a change to the behaviour of open(), but it does appear to be
the intended behaviour, and it won't change the overall behaviour of
AIOFile, as even if someone was relying on open() succeeding after
close(), all methods that operate on the _file_obj would fail after
the re-open.
This test ensures open() only opens an underlying file once when called
concurrently. This test currently fails, as described in:
mosquito#68
AIOFile.open() now uses a lock to prevent concurrent calls opening a
backing file multiple times.

This fixes mosquito#68
An alternate implementation to prevent concurrent open() calls opening
a backing file multiple times. This implementation uses the Future
returned by _run_in_thread() to synchronise concurrent open calls.

This fixes mosquito#68
@mosquito mosquito merged commit ddf9607 into mosquito:master Aug 8, 2022
@mosquito
Copy link
Owner

mosquito commented Aug 8, 2022

released in 3.8.0

@h4l
Copy link
Contributor Author

h4l commented Aug 12, 2022

Great, thanks for merging.

@h4l h4l deleted the concurrent-open-fix-future branch August 12, 2022 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Race condition in AIOFile.open()
2 participants