-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 ArrowWriter closes stream at exit #1971
Merged
Merged
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
49d14fb
Test ArrowWriter closes stream at exit
albertvillanova d3c0e0b
Implement ArrowWriter as context manager and close stream at exit
albertvillanova 19cd459
Specify exception class
albertvillanova b28967f
Use ArrowWriter as context manager
albertvillanova 8c230ee
Fix ArrowWriter.close
albertvillanova 4d50e95
Merge remote-tracking branch 'upstream/master' into fix-arrow-writer
albertvillanova 2a7a861
Fix ArrowWriter.close
albertvillanova 5c7af22
Test stream already closed at ArrowWriter context exit
albertvillanova ba368f5
Revert "Use ArrowWriter as context manager"
albertvillanova d2e123d
Use ArrowWriter as context manager in tests
albertvillanova 84ad7f3
Generalize Exception type in ArrowWriter.close
albertvillanova c521ffe
Use ArrowWriter as context manager in benchmarks
albertvillanova 7d241ce
Use ArrowWriter as context manager in arrow_writer and builder
albertvillanova e00d72a
Use ArrowWriter as context manager in arrow_dataset
albertvillanova 7f287fb
Use ArrowWriter as context manager in metric
albertvillanova 10d9258
Revert "Use ArrowWriter as context manager in metric"
albertvillanova 6f1a701
Merge remote-tracking branch 'upstream/master' into fix-arrow-writer
albertvillanova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -336,7 +336,8 @@ def _finalize(self): | |
from all the nodes if main node or all_process is True. | ||
""" | ||
if self.writer is not None: | ||
self.writer.finalize() | ||
with self.writer as writer: | ||
writer.finalize() | ||
self.writer = None | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @lhoestq , do you think this is the cause of the test error? I am trying to understand why... It happens with |
||
if self.filelock is not None: | ||
self.filelock.release() | ||
|
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.
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 usage of the
with
statement here can make the user think that the writer opens and creates a file here.This is not the case since finalize just writes down the latest examples and close the file.
It would be prettier to use .close() instead of a with statement here, to remove this ambiguity.
The with statement should be used to define the start and end point of the usage of the writer (file opened until closed).
However in this case the writer is an attribute of the Metric object and so there's no code block with a defined start/end.
In this case I think it's better to use .close() instead. You can even do try/except to make sure that close() is called even if finalize fails.
It would makes things clearer IMO.
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.
@lhoestq, I think the main point is in your sentence:
Exactly! This is why I decided to open this PR: an exception was raised, and the resource was not closed. If an exception is raised within .finalize(), the file will no be closed. So that the solution would be: try... except... finally: close().
And that is exactly the use case for
with
: use a context manager instead of try... except... finally: close().See: https://docs.python.org/3/reference/compound_stmts.html#the-with-statement
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.
In this case maybe you can you add in the writer's docstring that the
with
can also be used only when callingfinalize
?As a user I'd expect the
with
statement of aWriter
class to be necessarily used from start of writing till closing, but here you can write and then use thewith
statement to close. Let me know if that makes sense.That's definitely not a big deal but I'd rather have things said explicitly :)
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.
Yes @lhoestq, you are right. One should use the context manager from start of writing until closing. I left some usages of writer without context manager, because the refactoring was not immediate, for a follow-up PR. Maybe I can remove the
with
in this case, to finish this PR. And then open another PR for the remaining non-trivial cases?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.
Sounds good !