Closing the stdout file descriptor when finished#3864
Merged
paulromano merged 1 commit intoopenmc-dev:developfrom Mar 11, 2026
Merged
Closing the stdout file descriptor when finished#3864paulromano merged 1 commit intoopenmc-dev:developfrom
paulromano merged 1 commit intoopenmc-dev:developfrom
Conversation
Currently, since we use the stdout of the Popen we create, it can sometimes fail to close that resource when the process is finished, leading to a "ResourceWarning: unclosed file <_io.TextIOWrapper name=29 encoding='UTF-8'>" warning.
paulromano
approved these changes
Mar 11, 2026
Contributor
paulromano
left a comment
There was a problem hiding this comment.
Makes sense; thanks @esheder!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Currently, since the
_runfunction in theexecutor.pyfile uses thestdoutof thePopenit creates, it can sometimes fail to close that resource when the process is finished, leading to aResourceWarning: unclosed file <_io.TextIOWrapper ...>warning.This happened to me consistently when plotting a specific model, but does not happen for all the models I try to plot. However, Python's tracemalloc option points to this particular file descriptor being left open on those rare cases, and this fix stopped the warnings on my end.
I don't think it hurts to release this resource explicitly when the process is finished and we break from the printing loop, so this is a case of a small cost in terms of lines of code for a small benefit in terms of unintended warnings.
Checklist
If adding a test for this case is required, I can try to find a minimal working example of this warning case.