-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24334] Fix race condition in ArrowPythonRunner causes unclean shutdown of Arrow memory allocator #21397
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -70,19 +70,13 @@ class ArrowPythonRunner( | |
| val arrowSchema = ArrowUtils.toArrowSchema(schema, timeZoneId) | ||
| val allocator = ArrowUtils.rootAllocator.newChildAllocator( | ||
| s"stdout writer for $pythonExec", 0, Long.MaxValue) | ||
|
|
||
| val root = VectorSchemaRoot.create(arrowSchema, allocator) | ||
| val arrowWriter = ArrowWriter.create(root) | ||
|
|
||
| context.addTaskCompletionListener { _ => | ||
| root.close() | ||
| allocator.close() | ||
| } | ||
|
|
||
| val writer = new ArrowStreamWriter(root, null, dataOut) | ||
| writer.start() | ||
|
|
||
| Utils.tryWithSafeFinally { | ||
| val arrowWriter = ArrowWriter.create(root) | ||
| val writer = new ArrowStreamWriter(root, null, dataOut) | ||
| writer.start() | ||
|
|
||
| while (inputIterator.hasNext) { | ||
| val nextBatch = inputIterator.next() | ||
|
|
||
|
|
@@ -94,8 +88,18 @@ class ArrowPythonRunner( | |
| writer.writeBatch() | ||
| arrowWriter.reset() | ||
| } | ||
| } { | ||
| writer.end() | ||
|
Contributor
Author
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. This is moved out of the finally block because this function will throw exception if the thread is interrupted. writer.end() also doesn't do any clean up - it just writes some final bits to the output channel so we don't need to call it in clean up.
Contributor
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. So basically your change makes sure that
Contributor
Author
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. The routine in
Member
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. maybe add a comment to explain the same - that this should be outside of the finally? |
||
| } { | ||
| // If we close root and allocator in TaskCompletionListener, there could be a race | ||
| // condition where the writer thread keeps writing to the VectorSchemaRoot while | ||
| // it's being closed by the TaskCompletion listener. | ||
| // Closing root and allocator here is cleaner because root and allocator is owned | ||
| // by the writer thread and is only visible to the writer thread. | ||
| // | ||
| // If the writer thread is interrupted by TaskCompletionListener, it should either | ||
| // (1) in the try block, in which case it will get an InterruptedException when | ||
| // performing io, and goes into the finally block or (2) in the finally block, | ||
| // in which case it will ignore the interruption and close the resources. | ||
| root.close() | ||
| allocator.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.
Yea, I think it's good enough to have it in PR description. Let's just put this in the PR description.
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.
SGTM! Moved to PR description.