Long story short
I would like to be able to call close() twice. My use case is something like this:
from aiofile import async_open
from asyncio import run
from pathlib import Path
from tempfile import TemporaryDirectory
async def main() -> None:
with TemporaryDirectory() as tmpdir:
async with async_open(Path(tmpdir) / "foo", "wb") as f:
await f.close()
run(main())
except between open/close there is a bunch of nested calls so I can't easily leave the async with section. And I want to do other stuff after closing the file, within those calls.
Expected behavior
The second close should be ignored, like it is in the non-async case.
Actual behavior
$ python example.py
Traceback (most recent call last):
File "example.py", line 13, in <module>
run(main())
File "asyncio/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "asyncio/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "asyncio/base_events.py", line 653, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "example.py", line 9, in main
async with async_open(Path(tmpdir) / "foo", "wb") as f:
File "aiofile/utils.py", line 210, in __aexit__
await self.close()
File "aiofile/utils.py", line 199, in close
await self.file.close()
File "aiofile/aio.py", line 197, in close
await self.fdsync()
File "aiofile/aio.py", line 295, in fdsync
return await self.__context.fdsync(self.fileno())
^^^^^^^^^^^^^
File "aiofile/aio.py", line 204, in fileno
return self._file_obj.fileno()
^^^^^^^^^^^^^^^^^^^^^^^
ValueError: I/O operation on closed file
Steps to reproduce
Run the above script
Environment info
Kernel version: Linux foo 5.15.0-86-generic #96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
File system: ext4
I have been produced this problem with implementations:
- ✅
export CAIO_IMPL=linux - Native linux implementation
- ✅
export CAIO_IMPL=thread - Thread implementation
- ✅
export CAIO_IMPL=python - Pure Python implementation
Additional info
Python version is 3.11.5, aiofile version is 3.8.8
Workaround is to set f.file._file_obj_owner = False after closing.
Long story short
I would like to be able to call close() twice. My use case is something like this:
except between open/close there is a bunch of nested calls so I can't easily leave the
async withsection. And I want to do other stuff after closing the file, within those calls.Expected behavior
The second close should be ignored, like it is in the non-async case.
Actual behavior
Steps to reproduce
Run the above script
Environment info
Kernel version:
Linux foo 5.15.0-86-generic #96-Ubuntu SMP Wed Sep 20 08:23:49 UTC 2023 x86_64 x86_64 x86_64 GNU/LinuxFile system:
ext4I have been produced this problem with implementations:
export CAIO_IMPL=linux- Native linux implementationexport CAIO_IMPL=thread- Thread implementationexport CAIO_IMPL=python- Pure Python implementationAdditional info
Python version is
3.11.5,aiofileversion is3.8.8Workaround is to set
f.file._file_obj_owner = Falseafter closing.