Skip to content

Closing the stdout file descriptor when finished#3864

Merged
paulromano merged 1 commit intoopenmc-dev:developfrom
esheder:descriptor_fix
Mar 11, 2026
Merged

Closing the stdout file descriptor when finished#3864
paulromano merged 1 commit intoopenmc-dev:developfrom
esheder:descriptor_fix

Conversation

@esheder
Copy link
Contributor

@esheder esheder commented Mar 11, 2026

Description

Currently, since the _run function in the executor.py file uses the stdout of the Popen it creates, it can sometimes fail to close that resource when the process is finished, leading to a ResourceWarning: 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

  • I have performed a self-review of my own code
  • I changed no C++ source files, so I did not have to run clang-format (version 18)
  • I have followed the style guidelines for Python source files (if applicable)
  • There were no corresponding changes to be made to the documentation
  • I have added tests that prove my fix is effective or that my feature works (if applicable)

If adding a test for this case is required, I can try to find a minimal working example of this warning case.

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.
Copy link
Contributor

@paulromano paulromano left a comment

Choose a reason for hiding this comment

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

Makes sense; thanks @esheder!

@paulromano paulromano enabled auto-merge (squash) March 11, 2026 16:47
@paulromano paulromano merged commit ba94c58 into openmc-dev:develop Mar 11, 2026
16 checks passed
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