-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Remove subprocess.PIPE usage by using a temp file #22654
Remove subprocess.PIPE usage by using a temp file #22654
Conversation
Codecov Report
@@ Coverage Diff @@
## master #22654 +/- ##
==========================================
- Coverage 74.20% 74.18% -0.02%
==========================================
Files 708 708
Lines 93465 93473 +8
==========================================
- Hits 69352 69347 -5
- Misses 22838 22851 +13
Partials 1275 1275
Flags with carried forward coverage won't be shown. Click here to find out more.
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
Assigning reviewers. If you would like to opt out of this review, comment R: @TheNeuralBit for label python. Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Run XVR_PythonUsingJava_Dataflow PostCommit |
Run XVR_Direct PostCommit |
with tempfile.NamedTemporaryFile(delete=False) as stdout_file: | ||
self._stdout_file_name = stdout_file.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the tempfile docs:
The resulting object can be used as a context manager (see Examples). On completion of the context or destruction of the file object the temporary file will be removed from the filesystem.
Doesn't this mean that the temp file will get deleted when this context completes? I think instead we can just not use it in a context, and the file will get deleted when stdout_file is destroyed:
with tempfile.NamedTemporaryFile(delete=False) as stdout_file: | |
self._stdout_file_name = stdout_file.name | |
self._stdout_file = tempfile.NamedTemporaryFile(delete=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the file will be deleted since we set "delete=False" but probably safer to not use a context manager here.
self._process = subprocess.Popen( | ||
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | ||
cmd, | ||
stdout=open(self._stdout_file_name, 'wb'), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the object returned by NamedTemporaryFile is already an opened file object with mode w+b
, if you apply the above suggestion, this could be:
stdout=open(self._stdout_file_name, 'wb'), | |
stdout=self._stdout_file, |
except Exception as e: | ||
logging.error(( | ||
'Could not remove temporary file %s due to %r' % | ||
(self._stdout_file_name, e))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be able to omit this:
It will be destroyed as soon as it is closed (including an implicit close when the object is garbage collected)
Or we can explicitly request it with self._stdout_file.close()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have to manually delete since we set "delete=False" when creating the temp file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
except Exception as e: | ||
logging.error(( | ||
'Could not remove temporary file %s due to %r' % | ||
(self._stdout_file_name, e))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we have to manually delete since we set "delete=False" when creating the temp file.
with tempfile.NamedTemporaryFile(delete=False) as stdout_file: | ||
self._stdout_file_name = stdout_file.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think the file will be deleted since we set "delete=False" but probably safer to not use a context manager here.
This reverts commit 0c2e235.
@robertwb I think you had concerns about this change. Do you think this should be reverted ? |
* Remove subprocess.PIPE usage by using a temp file * Remove context manager usage * Fix yapf
I'm curious what the concerns were. My understanding was that this was a net positive, orthogonal to the grpc issue. |
@TheNeuralBit I reverted it before the new release cut since new grpcio has been released and we haven't run into issues. I think @robertwb believed that PIPE is the correct construct to use here (instead of temp files) and we should not run into deadlocks due to the way we use PIPE. We can get it in again in the future if needed. |
This is to address concerns regarding deadlocks. See #22533 for details.
Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
R: @username
).addresses #123
), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, commentfixes #<ISSUE NUMBER>
instead.CHANGES.md
with noteworthy changes.See the Contributor Guide for more tips on how to make review process smoother.
To check the build health, please visit https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md
GitHub Actions Tests Status (on master branch)
See CI.md for more information about GitHub Actions CI.