Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions arrow/cdata/cdata_exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import (
"runtime/cgo"
"strconv"
"strings"
"sync"
"unsafe"

"github.com/apache/arrow-go/v18/arrow"
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Member

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 cancel and returns at once, so tasks still in taskQueue, and any the producer sends afterwards for requests already made, are never passed to extract_data.

The spec says a consumer that doesn't want a task's data "must still call extract_data so that the producer can perform any required cleanup", passing NULL. It also says the consumer must expect more on_next_task calls after cancel. So the producer never gets to clean those tasks up.

Now that asyncTaskExtract handles a NULL out, the consumer side could drain the queue after cancel and call extract_data with NULL for each task until release closes 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.

Copy link
Copy Markdown
Contributor Author

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 cancel the consumer could keep draining taskQueue and call extract_data with NULL for each task until release closes the channel, which asyncTaskExtract now handles. There is a related producer-side gap I found by reading: exportAsyncProducer allocates a handle slot and cgo.Handle per task (exports.go:431) and frees them only when on_next_task fails (exports.go:443-444), while asyncTaskExtract (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.

case task, ok := <-taskQueue:
// if the queue closes or we receive a nil task, we're done
Expand All @@ -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()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 release from inside request while not cancelled". I can't find that in the spec.

  • The ArrowAsyncProducer.request docs only forbid request from synchronously calling on_next_task or on_error.
  • cancel must not call any consumer handler other than on_error, so release is ruled out from inside cancel, but not from inside request.

That matters because h.mu is held across this call. If a producer called release synchronously from inside request, the release callback would run on this goroutine and block on h.mu forever. A conforming producer is unlikely to do that, since it calls release after the end of the stream or an error. Could you either correct the spec claim, or list this case as a known deadlock next to the lock-ordering case the description already mentions?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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 on_next_task and on_error calls are forbidden from request, and abi.h only says later release calls "should" be scheduled. It also lists a synchronous release from inside request (deadlock on h.mu) as a known case next to the lock-ordering one.

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) {
Expand Down Expand Up @@ -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()
}
48 changes: 48 additions & 0 deletions arrow/cdata/cdata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1661,6 +1661,54 @@ func TestAsyncReleaseAfterOnSchemaFailure(t *testing.T) {
}
}

// A producer may free its ArrowAsyncProducer once release returns, so the
// consumer must not call request() for tasks still buffered at that point.
func TestAsyncNoRequestAfterRelease(t *testing.T) {
handler := testAsyncHandler()
defer freeAsyncHandler(handler)

ch := CreateAsyncDeviceStreamHandler(context.Background(), 4, handler)
var schema CArrowSchema
ExportArrowSchema(arrow.NewSchema([]arrow.Field{{Name: "a", Type: arrow.PrimitiveTypes.Int32}}, nil), &schema)

done := make(chan struct{})
go func() {
defer close(done)
asyncProduceThenRelease(handler, &schema, 3)
}()

asyncStream := <-ch
require.NoError(t, asyncStream.Err)
for range asyncStream.Stream {
}
<-done
defer freeAsyncProducer()

late, extracts := asyncProducerCounts()
assert.Zero(t, late, "request called after release")
assert.Equal(t, 3, extracts, "buffered tasks must still be extracted")
}

// A consumer may pass NULL to extract_data when it doesn't want the data.
func TestAsyncTaskExtractNull(t *testing.T) {
mem := memory.NewCheckedAllocator(memory.DefaultAllocator)
defer mem.AssertSize(t, 0)

bldr := array.NewInt32Builder(mem)
defer bldr.Release()
bldr.AppendValues([]int32{1, 2, 3}, nil)
arr := bldr.NewArray()
defer arr.Release()
rec := array.NewRecordBatch(arrow.NewSchema([]arrow.Field{{Name: "a", Type: arrow.PrimitiveTypes.Int32}}, nil), []arrow.Array{arr}, 3)

// a Go-side handle slot (not createHandle's malloc) so nothing is left to free
hnd := cgo.NewHandle(rec)
defer hnd.Delete()
hptr := uintptr(hnd)
task := CArrowAsyncTask{private_data: unsafe.Pointer(&hptr)}
assert.Zero(t, int(asyncTaskExtract(&task, nil)))
}

func TestAsyncPropagateError(t *testing.T) {
reclist := arrdata.Records["primitives"]

Expand Down
61 changes: 61 additions & 0 deletions arrow/cdata/cdata_test_framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package cdata
// #include <stdlib.h>
// #include <stdint.h>
// #include <string.h>
// #include <errno.h>
// #include "abi.h"
// #include "helpers.h"
//
Expand Down Expand Up @@ -60,6 +61,49 @@ package cdata
// handler->release(handler);
// }
//
// static volatile int g_async_released;
// static volatile int g_async_late_requests;
// static volatile int g_async_extracts;
// static struct ArrowAsyncProducer* g_async_producer;
//
// static void test_async_request(struct ArrowAsyncProducer* p, int64_t n) {
// if (__atomic_load_n(&g_async_released, __ATOMIC_SEQ_CST)) __atomic_fetch_add(&g_async_late_requests, 1, __ATOMIC_SEQ_CST);
// }
//
// static int async_late_requests() { return g_async_late_requests; }
// static int async_extracts() { return g_async_extracts; }
// static void free_async_producer() { free(g_async_producer); }
//
// static void test_async_cancel(struct ArrowAsyncProducer* p) {}
//
// static int test_async_extract(struct ArrowAsyncTask* task, struct ArrowDeviceArray* out) {
// // stay busy until release so the later tasks are still buffered then
// while (!__atomic_load_n(&g_async_released, __ATOMIC_SEQ_CST)) {}
// __atomic_fetch_add(&g_async_extracts, 1, __ATOMIC_SEQ_CST);
// return EINVAL; // no data; consumer skips the task
// }
//
// // run_async_producer acts as a conforming producer: n tasks, end of
// // stream, then release. request() calls after release are counted.
// static int run_async_producer(struct ArrowAsyncDeviceStreamHandler* handler, struct ArrowSchema* schema, int n) {
// __atomic_store_n(&g_async_released, 0, __ATOMIC_SEQ_CST);
// g_async_late_requests = g_async_extracts = 0;
// g_async_producer = get_test_async_producer(ARROW_DEVICE_CPU);
// g_async_producer->request = test_async_request;
// g_async_producer->cancel = test_async_cancel;
// handler->producer = g_async_producer;
// int rc = handler->on_schema(handler, schema);
// for (int i = 0; rc == 0 && i < n; i++) {
// struct ArrowAsyncTask task = {test_async_extract, NULL};
// rc = handler->on_next_task(handler, &task, NULL);
// }
// if (rc == 0) rc = handler->on_next_task(handler, NULL, NULL);
// handler->release(handler);
// // from here the producer may be freed
// __atomic_store_n(&g_async_released, 1, __ATOMIC_SEQ_CST);
// return rc;
// }
//
// void release_test_arr(struct ArrowArray* arr);
//
// static int32_t* get_data() {
Expand Down Expand Up @@ -217,6 +261,23 @@ func asyncOnSchemaThenRelease(handler *CArrowAsyncDeviceStreamHandler, schema *C
return status, handler.release == nil
}

// asyncProduceThenRelease runs a producer that sends n tasks and end of
// stream and then releases handler. Call freeAsyncProducer once the
// consumer is done.
func asyncProduceThenRelease(handler *CArrowAsyncDeviceStreamHandler, schema *CArrowSchema, n int) {
C.run_async_producer(handler, schema, C.int(n))
}

// asyncProducerCounts returns the request calls made after release and
// the tasks extracted.
func asyncProducerCounts() (late, extracts int) {
return int(C.async_late_requests()), int(C.async_extracts())
}

func freeAsyncProducer() {
C.free_async_producer()
}

func testNested(fmts, names []string, isnull []bool) **CArrowSchema {
if len(fmts) != len(names) {
panic("testing nested lists must have same size fmts and names")
Expand Down
12 changes: 9 additions & 3 deletions arrow/cdata/exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,15 @@ func asyncStreamOnSchema(self *CArrowAsyncDeviceStreamHandler, schema *CArrowSch
handler.ch <- AsyncRecordBatchStream{Schema: sc,
AdditionalMetadata: meta, Stream: recordStream}

self.private_data = createHandle(cgo.NewHandle(&cAsyncStreamHandler{
streamHandler := &cAsyncStreamHandler{
producer: self.producer,
ctx: handler.ctx,
taskQueue: taskQueue,
}))
}
self.private_data = createHandle(cgo.NewHandle(streamHandler))

C.goCallRequest(self.producer, C.int64_t(handler.queueSize))
go asyncTaskQueue(handler.ctx, sc, recordStream, taskQueue, self.producer)
go asyncTaskQueue(handler.ctx, sc, recordStream, taskQueue, streamHandler)
return 0
}

Expand Down Expand Up @@ -333,6 +334,11 @@ func asyncTaskExtract(task *CArrowAsyncTask, out *CArrowDeviceArray) C.int {
rec := h.Value().(arrow.RecordBatch)
defer rec.Release()

// a NULL out means the consumer doesn't want the data
if out == nil {
return C.int(0)
}

out.device_id, out.device_type = C.int64_t(-1), C.ARROW_DEVICE_CPU
ExportArrowRecordBatch(rec, &out.array, nil)
return C.int(0)
Expand Down
Loading