Skip to content

raising resource warnings prevents resource cleanup #94900

Open
@graingert

Description

@graingert

Bug report
in a number of places a ResourceWarning is issued before cleaning up a resource eg

def __del__(self, _warn=warnings.warn):
if self._pipe is not None:
_warn(f"unclosed transport {self!r}", ResourceWarning, source=self)
self._pipe.close()

normally this is fine - however if you're running with -Werror these warnings are raised as exceptions and so prevent the underlying resource from being cleaned up. This is in particular a problem when running a test suite with filterwarnings=['error', ... where resource cleanup can be delayed for many tests.

I think these should instead be restructured as:

 def __del__(self, _warn=warnings.warn): 
     if self._pipe is not None: 
         msg = f"unclosed transport {self!r}"  # grab the repr before closing the pipe so it displays as "open" still
         self._pipe.close()
         _warn(msg, ResourceWarning, source=self)  # issue the warning after closing the resource

Your environment

  • CPython versions tested on:
  • Operating system and architecture:

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions