Skip to content
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

Fix ResourceWarning from SubunitTestRunner._list() #342

Merged
merged 2 commits into from
Feb 7, 2023
Merged

Conversation

mtreinish
Copy link
Owner

This commit fixes the ResourceWarning emitted by stestr's SubunitRunner._list method caused by a leaking file descriptor when that method exits. The root cause of this was this method was calling fdopen() internally to open a new descriptor to the user specified result stream and never closing it when the method returns. The issue was that the return from that method had a reference to that fd and closing it would have resulted in potentially unexpected behavior. However, this code is not needed, it was just ported from subunit's SubunitTestRunner class when this code was forked and rewritten to use unittest instead of testtools. The subunit code is used in a broader context and the fdopen might be needed there. But for stestr, we always use stdout for the result stream as this only gets called internally the worker processes to run tests. If we used something other than stdout the result stream would not get sent to the parent process. Since we're alwwys using stdout we don't need an fdopen call because we never will need a fresh fd for it. This commit removes the legacy baggage causing this ResourceWarning and just interacts with the stream directly avoiding an additional open that never gets closed.

Related to #320

This commit fixes the ResourceWarning emitted by stestr's
SubunitRunner._list method caused by a leaking file descriptor when that
method exits. The root cause of this was this method was calling
fdopen() internally to open a new descriptor to the user specified
result stream and never closing it when the method returns. The issue
was that the return from that method had a reference to that fd and
closing it would have resulted in potentially unexpected behavior.
However, this code is not needed, it was just ported from subunit's
SubunitTestRunner class when this code was forked and rewritten to use
unittest instead of testtools. The subunit code is used in a broader
context and the fdopen might be needed there. But for stestr, we
always use stdout for the result stream as this only gets called
internally the worker processes to run tests. If we used something other
than stdout the result stream would not get sent to the parent process.
Since we're alwwys using stdout we don't need an fdopen call because we
never will need a fresh fd for it. This commit removes the legacy
baggage causing this ResourceWarning and just interacts with the stream
directly avoiding an additional open that never gets closed.

Related to #320
@codecov-commenter
Copy link

Codecov Report

Merging #342 (3556406) into main (883201c) will increase coverage by 0.04%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##             main     #342      +/-   ##
==========================================
+ Coverage   61.21%   61.26%   +0.04%     
==========================================
  Files          30       30              
  Lines        2617     2610       -7     
  Branches      433      432       -1     
==========================================
- Hits         1602     1599       -3     
+ Misses        891      888       -3     
+ Partials      124      123       -1     
Flag Coverage Δ
unittests 61.26% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
stestr/subunit_runner/run.py 86.04% <100.00%> (+6.04%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@mtreinish mtreinish requested a review from masayukig February 6, 2023 23:19
@masayukig
Copy link
Collaborator

LGTM. This change works on my laptop (py311, Fedora 37), too.

@masayukig masayukig merged commit d1ebe00 into main Feb 7, 2023
@masayukig masayukig deleted the fix-warnings branch February 7, 2023 01:08
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.

3 participants