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

Unable to call close() twice #89

Open
jonathan-conder-sm opened this issue Oct 11, 2023 · 0 comments
Open

Unable to call close() twice #89

jonathan-conder-sm opened this issue Oct 11, 2023 · 0 comments

Comments

@jonathan-conder-sm
Copy link

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.

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

No branches or pull requests

1 participant