alexdelorenzo/aiopath

Put a lock on IterableAIOFile's read/write methods

alexdelorenzo opened this issue · 2 comments

As a fix for #13, IterableAIOFile overrides the read()/write() methods in order to keep track of the file cursor's offset. While there's a lock in the parent classes read()/write() methods, no lock is held when managing the offset.

Two coroutines with access to the same IterableAIOFile can end up with a data race if they call either of those methods concurrently.

Simple fix is to enter a shared asyncio.Lock object before reading, writing or managing the file cursor offset.

Also, it's worth considering using the high-level API for aiofile, and making it conform to the typing.IO interface instead of using IterableAIOFile.

The downside is handling the errors and newline arguments from the open() builtin or pathlib.Path. The high-level API doesn't let the user specify those parameters.

Ported to anyio as of f8514b9