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 ValueError: I/O operation on closed file during tests #123

Merged
merged 21 commits into from
May 20, 2024

Conversation

yunzheng
Copy link
Member

@yunzheng yunzheng commented May 8, 2024

Sometimes the stdout file object is closed by flow.record internals as it is sometimes mocked and swapped by pytest during tests which in turn can confuse is_stdout() to return False causing the file to be closed.

This is now fixed by adding two custom methods for getting the stdio streams:

  • flow.record.utils.get_stdout()
  • flow.record.utils.get_stdin()

These methods are the preferred way to get the stdio streams as they also set an extra attribute on the returned file object that is checked by is_stdout().

@yunzheng yunzheng marked this pull request as draft May 8, 2024 09:08
@codecov-commenter
Copy link

codecov-commenter commented May 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.76%. Comparing base (0865b50) to head (ca4f7e1).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #123      +/-   ##
==========================================
+ Coverage   83.72%   83.76%   +0.04%     
==========================================
  Files          34       34              
  Lines        3403     3413      +10     
==========================================
+ Hits         2849     2859      +10     
  Misses        554      554              
Flag Coverage Δ
unittests 83.76% <100.00%> (+0.04%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yunzheng
Copy link
Member Author

yunzheng commented May 8, 2024

This sadly didn't fix the issue :(

Will leave this PR in Draft state for more adhoc tests.

@yunzheng yunzheng changed the title Fix ValueError: I/O operation on closed file Fix ValueError: I/O operation on closed file during tests May 17, 2024
@yunzheng yunzheng marked this pull request as ready for review May 17, 2024 12:56
@yunzheng
Copy link
Member Author

yunzheng commented May 17, 2024

Took a while but this should now be fixed. I took quite a deep dive into the pytest internals to figure out the cause.

It's mainly caused due to pytest's capture.py swapping sys.stdout with it's own objects (which is fine btw), however in our own code we check the file objects by comparing them to sys.stdout or sys.stdout.buffer, but as sys.stdout keeps getting swapped multiple times (eg: nested pytest operations/fixtures) it could be that a file object that was once a sys.stdout file is now not seen as stdout anymore, and therefore closed by flow.record internals at certain points.

The easiest fix was to ensure that when we request a stdout stream in flow.record to mark them as stdout ourselves, and ensure that they don't get closed.

I have no idea why this only happened on PyPy. Could be a race condition.

@yunzheng yunzheng requested a review from Schamper May 17, 2024 13:05
flow/record/utils.py Show resolved Hide resolved
flow/record/utils.py Show resolved Hide resolved
flow/record/utils.py Outdated Show resolved Hide resolved
@yunzheng yunzheng requested a review from Schamper May 17, 2024 13:21
@yunzheng yunzheng merged commit a8fd59d into main May 20, 2024
18 checks passed
@yunzheng yunzheng deleted the fix-io-error-during-pytest branch May 20, 2024 15:07
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