Repository navigation
fix(arrow/cdata): don't call the async producer after the handler is released #1609
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 all 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 |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ import ( | |
| "runtime/cgo" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "unsafe" | ||
|
|
||
| "github.com/apache/arrow-go/v18/arrow" | ||
|
|
@@ -511,17 +512,24 @@ func (rr *cRecordReader) release() { | |
| } | ||
|
|
||
| type cAsyncStreamHandler struct { | ||
| // mu guards producer: once release returns the producer may be freed, | ||
| // so it must not be called again. | ||
| mu sync.Mutex | ||
| producer *CArrowAsyncProducer | ||
| taskQueue chan taskState | ||
| ctx context.Context | ||
| } | ||
|
|
||
| func asyncTaskQueue(ctx context.Context, schema *arrow.Schema, recordStream chan<- RecordMessage, taskQueue <-chan taskState, producer *CArrowAsyncProducer) { | ||
| func asyncTaskQueue(ctx context.Context, schema *arrow.Schema, recordStream chan<- RecordMessage, taskQueue <-chan taskState, h *cAsyncStreamHandler) { | ||
| defer close(recordStream) | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| C.goCallCancel(producer) | ||
| h.mu.Lock() | ||
| if h.producer != nil { | ||
| C.goCallCancel(h.producer) | ||
| } | ||
| h.mu.Unlock() | ||
| return | ||
| case task, ok := <-taskQueue: | ||
| // if the queue closes or we receive a nil task, we're done | ||
|
|
@@ -535,7 +543,11 @@ func asyncTaskQueue(ctx context.Context, schema *arrow.Schema, recordStream chan | |
| } | ||
|
|
||
| // request another batch now that we've processed this one | ||
| C.goCallRequest(producer, C.int64_t(1)) | ||
| h.mu.Lock() | ||
|
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. A correction to the description. It says "the spec asks producers not to call
That matters because
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. You're right, I couldn't back that sentence up. I removed it. The description now says what the spec text does: only synchronous |
||
| if h.producer != nil { | ||
| C.goCallRequest(h.producer, C.int64_t(1)) | ||
| } | ||
| h.mu.Unlock() | ||
|
|
||
| var out CArrowDeviceArray | ||
| if C.goExtractTaskData(&task.task, &out) != C.int(0) { | ||
|
|
@@ -573,6 +585,7 @@ func (h *cAsyncStreamHandler) onError(code C.int, message, metadata *C.char) { | |
|
|
||
| func (h *cAsyncStreamHandler) release() { | ||
| close(h.taskQueue) | ||
| h.mu.Lock() | ||
| h.taskQueue, h.producer = nil, nil | ||
| h.producer = nil | ||
| h.mu.Unlock() | ||
| } | ||
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.
Not from this PR, but related to the same spec section. On cancellation the goroutine calls
canceland returns at once, so tasks still intaskQueue, and any the producer sends afterwards for requests already made, are never passed toextract_data.The spec says a consumer that doesn't want a task's data "must still call
extract_dataso that the producer can perform any required cleanup", passingNULL. It also says the consumer must expect moreon_next_taskcalls aftercancel. So the producer never gets to clean those tasks up.Now that
asyncTaskExtracthandles aNULLout, the consumer side could drain the queue aftercanceland callextract_datawithNULLfor each task untilreleasecloses the channel. That seems better as a follow-up than in this PR. I found this by reading the code and haven't run a conforming producer against it.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.
Agreed, that's a follow-up and not part of this PR. After
cancelthe consumer could keep drainingtaskQueueand callextract_datawith NULL for each task untilreleasecloses the channel, whichasyncTaskExtractnow handles. There is a related producer-side gap I found by reading:exportAsyncProducerallocates a handle slot andcgo.Handleper task (exports.go:431) and frees them only whenon_next_taskfails (exports.go:443-444), whileasyncTaskExtract(exports.go:332-345) releases the record but never frees them. I haven't reproduced that at runtime. Happy to open an issue for both once this PR merges.