Skip to content

ValueError: I/O operation on closed file. in ZipFile destructor #81954

Closed

Description

@joernheissler
BPO 37773
Nosy @pitrou, @vstinner, @serhiy-storchaka, @joernheissler, @tirkarthi, @akulakov

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2019-08-06.09:35:50.904>
labels = ['3.10', '3.9', 'type-crash', '3.11']
title = 'ValueError: I/O operation on closed file. in ZipFile destructor'
updated_at = <Date 2022-01-23.20:02:33.133>
user = 'https://github.com/joernheissler'

bugs.python.org fields:

activity = <Date 2022-01-23.20:02:33.133>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = []
creation = <Date 2019-08-06.09:35:50.904>
creator = 'joernheissler'
dependencies = []
files = []
hgrepos = []
issue_num = 37773
keywords = ['3.8regression']
message_count = 10.0
messages = ['349104', '349105', '389594', '389605', '389611', '389689', '389690', '389691', '396649', '396657']
nosy_count = 6.0
nosy_names = ['pitrou', 'vstinner', 'serhiy.storchaka', 'joernheissler', 'xtreak', 'andrei.avk']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'crash'
url = 'https://bugs.python.org/issue37773'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

Activity

  1. transferred this issue fromon Apr 10, 2022
  2. added
    type-bugAn unexpected behavior, bug, or error
    and removed
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Jul 11, 2022
  3. ChrisBarker-NOAA commented on Apr 18, 2023

    @ChrisBarker-NOAA
    Contributor

    (Python 3.10.10 -- excuse the noise if it's been fixed in 3.11)

    What's the status of this? I'm getting similar warnings in my tests:

      /Users/chris.barker/miniconda3/envs/gnome/lib/python3.10/site-packages/_pytest/unraisableexception.py:78: PytestUnraisableExceptionWarning: Exception ignored in: <function ZipFile.__del__ at 0x100aade10>
      
      Traceback (most recent call last):
        File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.10/zipfile.py", line 1819, in __del__
          self.close()
        File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.10/zipfile.py", line 1836, in close
          self.fp.seek(self.start_dir)
        File "/Users/chris.barker/miniconda3/envs/gnome/lib/python3.10/tempfile.py", line 779, in seek
          return self._file.seek(*args)
      ValueError: I/O operation on closed file.
      
        warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
    

    This is pretty buried -- I'm not using zipfile directly, it's being used by numpy.savez(), which I am using to store a cache in a temp dir.

    The odd thing is that this warning does not pop up when I run that test by itself, only when it is run as part of the suite -- which makes me thing it's maybe related to this issue with the GC and old references, but ???

    Anyway, I think @akulakov has the right idea -- if you put a guard around the close() call, the warning will go away. Maybe that would hide another more important error, but it seems not unlikely that an object may be deleted having been partially closed already.

  4. added
    3.12only security fixes
    3.13only security fixes
    and removed on Oct 27, 2023
  5. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Nov 23, 2023
  6. serhiy-storchaka commented on May 18, 2026

    @serhiy-storchaka
    Member

    This issue is not specific for zipfile. You will get similar symptoms for buffered files with something in the buffer, and for any object with non-trivial destructor. This is how the garbage collector works. More smart garbage collector could see that the BytesIO should not be destroyed before the ZipFile which refers to it. It could also see that destroying the BytesIO and the ZipFile will not help to break the loop cause by the function. But this is a complex issue.

    Recommendation: always use ZipFile as the content manager, or ensure that close() is always called (in the finally block if necessary). Do not rely on the garbage collector, it can destroy object in wrong order.

  7. cmaloney commented on Jun 27, 2026

    @cmaloney
    Contributor

    I Created #152354 to raise ResourceWarning on this case in the same way that GzipFile and TextIOWrapper do which should make the source(s) more straightforward to find with tracemalloc.

  8. added a commit that references this issue on Aug 7, 2026
  9. vstinner commented on Aug 7, 2026

    @vstinner
    Member

    The zipfile module now emits a ResourceWarning if a zipfile object is not closed explicitly and it's open in write mode. I close the issue.

  10. added a commit that references this issue on Aug 7, 2026
  11. added 2 commits that reference this issue on Aug 14, 2026
  12. added a commit that references this issue on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.11only security fixes3.12only security fixes3.13only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions