Skip to content

Avoid file descriptor refleaks in as_file. #234

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

Merged
merged 2 commits into from
Jul 30, 2021
Merged

Conversation

jaraco
Copy link
Member

@jaraco jaraco commented Jul 30, 2021

As reported in python/cpython#27436 (comment), merging the latest importlib_resources into CPython revealed a refleak. It appears the refleak has long been a part of the implementation but was previously uncovered by the tests. This change fixes that refleak.

@jaraco jaraco merged commit 64484b7 into main Jul 30, 2021
@jaraco jaraco deleted the bugfix/as_file-refleak branch July 30, 2021 20:55
@jaraco jaraco restored the bugfix/as_file-refleak branch July 30, 2021 20:55
@jaraco jaraco deleted the bugfix/as_file-refleak branch July 30, 2021 20:55
del reader
yield pathlib.Path(raw_path)
finally:
try:
os.remove(raw_path)
except (FileNotFoundError, PermissionError):
except FileNotFoundError:
Copy link
Member

@FFY00 FFY00 Jul 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this part though? IIRC it was added because for some reason Windows will set high permissions on temporary files sometimes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this exception was masking the underlying issue that the file descriptor wasn't being closed. Windows won't allow deletion of files in use, so when we encountered the issue, we suppressed the exception, allowing the file descriptor to remain unclosed and on Windows leaving the file undeleted. Now that the file descriptor is unconditionally closed, there's no longer any need to suppress this exception and as you can see, tests still pass on Windows.

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

Successfully merging this pull request may close these issues.

2 participants