raising resource warnings prevents resource cleanup

Open
#94,900 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
backend

Research direction

Start with Lib/asyncio/unix_events.py at the linked lines and look for other places where ResourceWarning is issued before resource cleanup. Exercise the affected cleanup paths with -Werror or filterwarnings=['error', ...]. Done means warning escalation no longer prevents the underlying resource from being closed.

Written by the indexing model from the issue text.

Description

stdlib type-bug

Bug report
in a number of places a ResourceWarning is issued before cleaning up a resource eg
https://github.com/python/cpython/blob/944ff8c57c9e8ff5a5bd18925070ddb1504490ab/Lib/asyncio/unix_events.py#L559-L562
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:
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.