CurtHagenlocher commented on code in PR #503:
URL: https://github.com/apache/arrow-dotnet/pull/503#discussion_r4578690771


##########
test/arrow-go-interop/proxy.c:
##########
@@ -0,0 +1,151 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// See proxy.h for why this exists.
+
+#include "proxy.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct InteropProxy {
+  struct ArrowAsyncDeviceStreamHandler* outer;
+  struct ArrowAsyncDeviceStreamHandler inner;
+  struct ArrowAsyncProducer producer;
+
+  // Guards real_producer and released. Held while forwarding request/cancel,
+  // so release can't complete (and the real producer be freed) mid-call. A
+  // spinlock keeps this free of threading-library dependencies; critical
+  // sections are short because request/cancel must not block or call back.
+  volatile char lock;
+  struct ArrowAsyncProducer* real_producer;
+  int released;
+};
+
+static void proxy_lock(struct InteropProxy* p) {
+  while (__atomic_test_and_set(&p->lock, __ATOMIC_ACQUIRE)) {
+  }
+}
+
+static void proxy_unlock(struct InteropProxy* p) {
+  __atomic_clear(&p->lock, __ATOMIC_RELEASE);
+}
+
+static struct InteropProxy* from_outer(struct ArrowAsyncDeviceStreamHandler* 
self) {
+  return (struct InteropProxy*)self->private_data;
+}
+
+// ---- proxy producer, called by arrow-go ----
+
+static void proxy_request(struct ArrowAsyncProducer* self, int64_t n) {
+  struct InteropProxy* p = (struct InteropProxy*)self->private_data;
+  proxy_lock(p);
+  if (!p->released && p->real_producer != NULL) {
+    p->real_producer->request(p->real_producer, n);
+  }
+  proxy_unlock(p);
+}
+
+static void proxy_cancel(struct ArrowAsyncProducer* self) {
+  struct InteropProxy* p = (struct InteropProxy*)self->private_data;
+  proxy_lock(p);
+  if (!p->released && p->real_producer != NULL) {
+    p->real_producer->cancel(p->real_producer);
+  }
+  proxy_unlock(p);
+}
+
+// ---- outer handler, called by the .NET producer; forwards to arrow-go ----
+
+static int outer_on_schema(struct ArrowAsyncDeviceStreamHandler* self,
+                           struct ArrowSchema* schema) {
+  struct InteropProxy* p = from_outer(self);
+
+  proxy_lock(p);
+  p->real_producer = self->producer;
+  proxy_unlock(p);
+
+  p->producer.device_type = self->producer->device_type;
+  p->producer.additional_metadata = self->producer->additional_metadata;
+  p->inner.producer = &p->producer;
+
+  // Not under the lock: arrow-go calls request() synchronously from on_schema.
+  return p->inner.on_schema(&p->inner, schema);
+}
+
+static int outer_on_next_task(struct ArrowAsyncDeviceStreamHandler* self,
+                              struct ArrowAsyncTask* task, const char* 
metadata) {
+  struct InteropProxy* p = from_outer(self);
+  return p->inner.on_next_task(&p->inner, task, metadata);
+}
+
+static void outer_on_error(struct ArrowAsyncDeviceStreamHandler* self, int 
code,
+                           const char* message, const char* metadata) {
+  struct InteropProxy* p = from_outer(self);
+  p->inner.on_error(&p->inner, code, message, metadata);
+}
+
+static void outer_release(struct ArrowAsyncDeviceStreamHandler* self) {
+  struct InteropProxy* p = from_outer(self);
+
+  // From here on the real producer may be freed; stop forwarding to it.
+  proxy_lock(p);
+  p->released = 1;
+  p->real_producer = NULL;
+  proxy_unlock(p);
+
+  if (p->inner.release != NULL) {
+    p->inner.release(&p->inner);
+  }
+
+  self->release = NULL;
+  self->private_data = NULL;

Review Comment:
   Fixed in 81be4ae. `outer_release` now clears `real_producer` first, which 
stops forwarding, and sets `released` only after arrow-go's release callback 
has returned. So `ArrowGoAsync_ConsumerFree` can no longer free the proxy while 
arrow-go is still writing to `p->inner`.



##########
test/arrow-go-interop/proxy.c:
##########
@@ -0,0 +1,151 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// See proxy.h for why this exists.
+
+#include "proxy.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+struct InteropProxy {
+  struct ArrowAsyncDeviceStreamHandler* outer;
+  struct ArrowAsyncDeviceStreamHandler inner;
+  struct ArrowAsyncProducer producer;
+
+  // Guards real_producer and released. Held while forwarding request/cancel,
+  // so release can't complete (and the real producer be freed) mid-call. A
+  // spinlock keeps this free of threading-library dependencies; critical
+  // sections are short because request/cancel must not block or call back.
+  volatile char lock;
+  struct ArrowAsyncProducer* real_producer;
+  int released;
+};
+
+static void proxy_lock(struct InteropProxy* p) {
+  while (__atomic_test_and_set(&p->lock, __ATOMIC_ACQUIRE)) {
+  }
+}
+
+static void proxy_unlock(struct InteropProxy* p) {
+  __atomic_clear(&p->lock, __ATOMIC_RELEASE);
+}
+
+static struct InteropProxy* from_outer(struct ArrowAsyncDeviceStreamHandler* 
self) {
+  return (struct InteropProxy*)self->private_data;
+}
+
+// ---- proxy producer, called by arrow-go ----
+
+static void proxy_request(struct ArrowAsyncProducer* self, int64_t n) {
+  struct InteropProxy* p = (struct InteropProxy*)self->private_data;
+  proxy_lock(p);
+  if (!p->released && p->real_producer != NULL) {
+    p->real_producer->request(p->real_producer, n);
+  }
+  proxy_unlock(p);
+}
+
+static void proxy_cancel(struct ArrowAsyncProducer* self) {
+  struct InteropProxy* p = (struct InteropProxy*)self->private_data;
+  proxy_lock(p);
+  if (!p->released && p->real_producer != NULL) {
+    p->real_producer->cancel(p->real_producer);
+  }
+  proxy_unlock(p);
+}
+
+// ---- outer handler, called by the .NET producer; forwards to arrow-go ----
+
+static int outer_on_schema(struct ArrowAsyncDeviceStreamHandler* self,
+                           struct ArrowSchema* schema) {
+  struct InteropProxy* p = from_outer(self);
+
+  proxy_lock(p);
+  p->real_producer = self->producer;
+  proxy_unlock(p);
+
+  p->producer.device_type = self->producer->device_type;
+  p->producer.additional_metadata = self->producer->additional_metadata;
+  p->inner.producer = &p->producer;
+
+  // Not under the lock: arrow-go calls request() synchronously from on_schema.
+  return p->inner.on_schema(&p->inner, schema);
+}
+
+static int outer_on_next_task(struct ArrowAsyncDeviceStreamHandler* self,
+                              struct ArrowAsyncTask* task, const char* 
metadata) {
+  struct InteropProxy* p = from_outer(self);
+  return p->inner.on_next_task(&p->inner, task, metadata);
+}
+
+static void outer_on_error(struct ArrowAsyncDeviceStreamHandler* self, int 
code,
+                           const char* message, const char* metadata) {
+  struct InteropProxy* p = from_outer(self);
+  p->inner.on_error(&p->inner, code, message, metadata);
+}
+
+static void outer_release(struct ArrowAsyncDeviceStreamHandler* self) {
+  struct InteropProxy* p = from_outer(self);
+
+  // From here on the real producer may be freed; stop forwarding to it.
+  proxy_lock(p);
+  p->released = 1;
+  p->real_producer = NULL;
+  proxy_unlock(p);
+
+  if (p->inner.release != NULL) {
+    p->inner.release(&p->inner);
+  }
+
+  self->release = NULL;
+  self->private_data = NULL;

Review Comment:
   Fixed in 81be4ae. `outer_release` writes `private_data` first and clears 
`release` last, with `__atomic_store_n(..., __ATOMIC_RELEASE)`. The test reads 
it with `Volatile.Read`, so once it sees `release` cleared, the proxy has 
nothing left to write.



##########
src/Apache.Arrow/C/CArrowAsyncDeviceStreamConsumer.cs:
##########
@@ -0,0 +1,546 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+using Apache.Arrow.Ipc;
+
+namespace Apache.Arrow.C
+{
+    /// <summary>
+    /// Consumes data from an async device stream producer by providing an
+    /// <see cref="CArrowAsyncDeviceStreamHandler"/>. Returns an <see 
cref="IArrowArrayStream"/>
+    /// that yields the received batches.
+    /// </summary>
+    [Experimental("ArrowAsyncDeviceStreamApi")]
+    public static class CArrowAsyncDeviceStreamConsumer
+    {
+        /// <summary>
+        /// The default number of batches the consumer keeps requested ahead 
of the reader.
+        /// </summary>
+        public const long DefaultQueueSize = 16;
+
+        private const int EOTHER = 131;
+
+#if NET5_0_OR_GREATER
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowSchema*, int> OnSchemaPtr => 
&OnSchema;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowAsyncTask*, byte*, int> 
OnNextTaskPtr => &OnNextTask;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, int, byte*, byte*, void> OnErrorPtr 
=> &OnError;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, void> ReleasePtr => &Release;
+#else
+        internal unsafe delegate int 
OnSchemaDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowSchema* schema);
+        private static unsafe readonly NativeDelegate<OnSchemaDelegate> 
s_onSchema = new NativeDelegate<OnSchemaDelegate>(OnSchema);
+        private static unsafe IntPtr OnSchemaPtr => s_onSchema.Pointer;
+        internal unsafe delegate int 
OnNextTaskDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowAsyncTask* 
task, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnNextTaskDelegate> 
s_onNextTask = new NativeDelegate<OnNextTaskDelegate>(OnNextTask);
+        private static unsafe IntPtr OnNextTaskPtr => s_onNextTask.Pointer;
+        internal unsafe delegate void 
OnErrorDelegate(CArrowAsyncDeviceStreamHandler* handler, int code, byte* 
message, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnErrorDelegate> 
s_onError = new NativeDelegate<OnErrorDelegate>(OnError);
+        private static unsafe IntPtr OnErrorPtr => s_onError.Pointer;
+        internal unsafe delegate void 
ReleaseHandlerDelegate(CArrowAsyncDeviceStreamHandler* handler);
+        private static unsafe readonly NativeDelegate<ReleaseHandlerDelegate> 
s_release = new NativeDelegate<ReleaseHandlerDelegate>(Release);
+        private static unsafe IntPtr ReleasePtr => s_release.Pointer;
+#endif
+
+        /// <summary>
+        /// Create a consumer handler and return an <see 
cref="IArrowArrayStream"/>
+        /// that will yield batches pushed by the producer.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// The caller must pass <paramref name="handler"/> to a producer. The 
producer
+        /// populates the handler's producer field and calls on_schema; the 
consumer
+        /// then requests <paramref name="queueSize"/> batches and requests 
more as
+        /// the reader consumes them, so at most about that many batches are 
buffered.
+        /// </para>
+        /// <para>
+        /// The consumer owns the handler's memory and frees it when the 
producer
+        /// calls the handler's release callback, so the caller must not free 
it and
+        /// must not use the pointer after handing it to the producer. A 
handler that
+        /// is never handed to a producer is never released, and leaks.
+        /// </para>
+        /// <para>
+        /// Disposing the returned stream before the end cancels the producer, 
which
+        /// may release the handler some time later.
+        /// </para>
+        /// </remarks>
+        /// <example>
+        /// <code>
+        /// IArrowArrayStream stream = 
CArrowAsyncDeviceStreamConsumer.CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler);
+        /// foreign_produce_function(handler);
+        /// RecordBatch batch = await stream.ReadNextRecordBatchAsync();
+        /// </code>
+        /// </example>
+        /// <param name="handler">Receives the newly allocated handler to pass 
to a producer.</param>
+        /// <param name="queueSize">The number of batches to keep requested 
ahead of the reader.</param>
+        /// <returns>An IArrowArrayStream that yields the received 
batches.</returns>
+        public static unsafe IArrowArrayStream CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler, long queueSize = DefaultQueueSize)
+        {
+            if (queueSize <= 0)
+            {
+                throw new ArgumentOutOfRangeException(nameof(queueSize), 
queueSize, "The queue size must be positive.");
+            }
+
+            CArrowAsyncDeviceStreamHandler* newHandler = 
CArrowAsyncDeviceStreamHandler.Create();
+            var state = new ConsumerState(newHandler, queueSize);
+
+            newHandler->on_schema = OnSchemaPtr;
+            newHandler->on_next_task = OnNextTaskPtr;
+            newHandler->on_error = OnErrorPtr;
+            newHandler->release = ReleasePtr;
+
+            GCHandle gch = GCHandle.Alloc(state);
+            newHandler->private_data = (void*)GCHandle.ToIntPtr(gch);
+
+            handler = newHandler;
+            return state;
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static int OnSchema(CArrowAsyncDeviceStreamHandler* 
handler, CArrowSchema* schema)
+        {
+            try
+            {
+                var state = ConsumerState.FromPointer(handler->private_data);
+                Schema importedSchema = 
CArrowSchemaImporter.ImportSchema(schema);
+                state.SetSchema(importedSchema);
+
+                // Calling request() synchronously from on_schema is 
explicitly allowed.
+                state.RequestFromProducer(state.QueueSize);

Review Comment:
   Fixed in 81be4ae. When there's no producer yet, `CancelProducer` only 
records the cancel. `on_schema` now calls `StartRequesting`, which sends that 
deferred cancel instead of the initial request. The new 
`DisposingConsumerBeforeProducerAttachesReleases` test covers this, and it 
fails without the fix.



##########
src/Apache.Arrow/C/CArrowAsyncDeviceStreamConsumer.cs:
##########
@@ -0,0 +1,546 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+using Apache.Arrow.Ipc;
+
+namespace Apache.Arrow.C
+{
+    /// <summary>
+    /// Consumes data from an async device stream producer by providing an
+    /// <see cref="CArrowAsyncDeviceStreamHandler"/>. Returns an <see 
cref="IArrowArrayStream"/>
+    /// that yields the received batches.
+    /// </summary>
+    [Experimental("ArrowAsyncDeviceStreamApi")]
+    public static class CArrowAsyncDeviceStreamConsumer
+    {
+        /// <summary>
+        /// The default number of batches the consumer keeps requested ahead 
of the reader.
+        /// </summary>
+        public const long DefaultQueueSize = 16;
+
+        private const int EOTHER = 131;
+
+#if NET5_0_OR_GREATER
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowSchema*, int> OnSchemaPtr => 
&OnSchema;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowAsyncTask*, byte*, int> 
OnNextTaskPtr => &OnNextTask;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, int, byte*, byte*, void> OnErrorPtr 
=> &OnError;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, void> ReleasePtr => &Release;
+#else
+        internal unsafe delegate int 
OnSchemaDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowSchema* schema);
+        private static unsafe readonly NativeDelegate<OnSchemaDelegate> 
s_onSchema = new NativeDelegate<OnSchemaDelegate>(OnSchema);
+        private static unsafe IntPtr OnSchemaPtr => s_onSchema.Pointer;
+        internal unsafe delegate int 
OnNextTaskDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowAsyncTask* 
task, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnNextTaskDelegate> 
s_onNextTask = new NativeDelegate<OnNextTaskDelegate>(OnNextTask);
+        private static unsafe IntPtr OnNextTaskPtr => s_onNextTask.Pointer;
+        internal unsafe delegate void 
OnErrorDelegate(CArrowAsyncDeviceStreamHandler* handler, int code, byte* 
message, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnErrorDelegate> 
s_onError = new NativeDelegate<OnErrorDelegate>(OnError);
+        private static unsafe IntPtr OnErrorPtr => s_onError.Pointer;
+        internal unsafe delegate void 
ReleaseHandlerDelegate(CArrowAsyncDeviceStreamHandler* handler);
+        private static unsafe readonly NativeDelegate<ReleaseHandlerDelegate> 
s_release = new NativeDelegate<ReleaseHandlerDelegate>(Release);
+        private static unsafe IntPtr ReleasePtr => s_release.Pointer;
+#endif
+
+        /// <summary>
+        /// Create a consumer handler and return an <see 
cref="IArrowArrayStream"/>
+        /// that will yield batches pushed by the producer.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// The caller must pass <paramref name="handler"/> to a producer. The 
producer
+        /// populates the handler's producer field and calls on_schema; the 
consumer
+        /// then requests <paramref name="queueSize"/> batches and requests 
more as
+        /// the reader consumes them, so at most about that many batches are 
buffered.
+        /// </para>
+        /// <para>
+        /// The consumer owns the handler's memory and frees it when the 
producer
+        /// calls the handler's release callback, so the caller must not free 
it and
+        /// must not use the pointer after handing it to the producer. A 
handler that
+        /// is never handed to a producer is never released, and leaks.
+        /// </para>
+        /// <para>
+        /// Disposing the returned stream before the end cancels the producer, 
which
+        /// may release the handler some time later.
+        /// </para>
+        /// </remarks>
+        /// <example>
+        /// <code>
+        /// IArrowArrayStream stream = 
CArrowAsyncDeviceStreamConsumer.CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler);
+        /// foreign_produce_function(handler);
+        /// RecordBatch batch = await stream.ReadNextRecordBatchAsync();
+        /// </code>
+        /// </example>
+        /// <param name="handler">Receives the newly allocated handler to pass 
to a producer.</param>
+        /// <param name="queueSize">The number of batches to keep requested 
ahead of the reader.</param>
+        /// <returns>An IArrowArrayStream that yields the received 
batches.</returns>
+        public static unsafe IArrowArrayStream CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler, long queueSize = DefaultQueueSize)
+        {
+            if (queueSize <= 0)
+            {
+                throw new ArgumentOutOfRangeException(nameof(queueSize), 
queueSize, "The queue size must be positive.");
+            }
+
+            CArrowAsyncDeviceStreamHandler* newHandler = 
CArrowAsyncDeviceStreamHandler.Create();
+            var state = new ConsumerState(newHandler, queueSize);
+
+            newHandler->on_schema = OnSchemaPtr;
+            newHandler->on_next_task = OnNextTaskPtr;
+            newHandler->on_error = OnErrorPtr;
+            newHandler->release = ReleasePtr;
+
+            GCHandle gch = GCHandle.Alloc(state);
+            newHandler->private_data = (void*)GCHandle.ToIntPtr(gch);
+
+            handler = newHandler;
+            return state;
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static int OnSchema(CArrowAsyncDeviceStreamHandler* 
handler, CArrowSchema* schema)
+        {
+            try
+            {
+                var state = ConsumerState.FromPointer(handler->private_data);
+                Schema importedSchema = 
CArrowSchemaImporter.ImportSchema(schema);
+                state.SetSchema(importedSchema);
+
+                // Calling request() synchronously from on_schema is 
explicitly allowed.
+                state.RequestFromProducer(state.QueueSize);
+                return 0;
+            }
+            catch (Exception)
+            {
+                return EOTHER;
+            }

Review Comment:
   Fixed in 81be4ae. The `on_schema` catch now calls `SignalError(ex)` before 
returning `EOTHER`, so both `ReadNextRecordBatchAsync` and `Schema` report the 
import failure. The new `SchemaImportFailureIsReported` test covers this, and 
it fails without the fix.



##########
src/Apache.Arrow/C/CArrowAsyncDeviceStreamConsumer.cs:
##########
@@ -0,0 +1,546 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.InteropServices;
+using System.Threading;
+using System.Threading.Tasks;
+using Apache.Arrow.Ipc;
+
+namespace Apache.Arrow.C
+{
+    /// <summary>
+    /// Consumes data from an async device stream producer by providing an
+    /// <see cref="CArrowAsyncDeviceStreamHandler"/>. Returns an <see 
cref="IArrowArrayStream"/>
+    /// that yields the received batches.
+    /// </summary>
+    [Experimental("ArrowAsyncDeviceStreamApi")]
+    public static class CArrowAsyncDeviceStreamConsumer
+    {
+        /// <summary>
+        /// The default number of batches the consumer keeps requested ahead 
of the reader.
+        /// </summary>
+        public const long DefaultQueueSize = 16;
+
+        private const int EOTHER = 131;
+
+#if NET5_0_OR_GREATER
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowSchema*, int> OnSchemaPtr => 
&OnSchema;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, CArrowAsyncTask*, byte*, int> 
OnNextTaskPtr => &OnNextTask;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, int, byte*, byte*, void> OnErrorPtr 
=> &OnError;
+        private static unsafe delegate* 
unmanaged<CArrowAsyncDeviceStreamHandler*, void> ReleasePtr => &Release;
+#else
+        internal unsafe delegate int 
OnSchemaDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowSchema* schema);
+        private static unsafe readonly NativeDelegate<OnSchemaDelegate> 
s_onSchema = new NativeDelegate<OnSchemaDelegate>(OnSchema);
+        private static unsafe IntPtr OnSchemaPtr => s_onSchema.Pointer;
+        internal unsafe delegate int 
OnNextTaskDelegate(CArrowAsyncDeviceStreamHandler* handler, CArrowAsyncTask* 
task, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnNextTaskDelegate> 
s_onNextTask = new NativeDelegate<OnNextTaskDelegate>(OnNextTask);
+        private static unsafe IntPtr OnNextTaskPtr => s_onNextTask.Pointer;
+        internal unsafe delegate void 
OnErrorDelegate(CArrowAsyncDeviceStreamHandler* handler, int code, byte* 
message, byte* metadata);
+        private static unsafe readonly NativeDelegate<OnErrorDelegate> 
s_onError = new NativeDelegate<OnErrorDelegate>(OnError);
+        private static unsafe IntPtr OnErrorPtr => s_onError.Pointer;
+        internal unsafe delegate void 
ReleaseHandlerDelegate(CArrowAsyncDeviceStreamHandler* handler);
+        private static unsafe readonly NativeDelegate<ReleaseHandlerDelegate> 
s_release = new NativeDelegate<ReleaseHandlerDelegate>(Release);
+        private static unsafe IntPtr ReleasePtr => s_release.Pointer;
+#endif
+
+        /// <summary>
+        /// Create a consumer handler and return an <see 
cref="IArrowArrayStream"/>
+        /// that will yield batches pushed by the producer.
+        /// </summary>
+        /// <remarks>
+        /// <para>
+        /// The caller must pass <paramref name="handler"/> to a producer. The 
producer
+        /// populates the handler's producer field and calls on_schema; the 
consumer
+        /// then requests <paramref name="queueSize"/> batches and requests 
more as
+        /// the reader consumes them, so at most about that many batches are 
buffered.
+        /// </para>
+        /// <para>
+        /// The consumer owns the handler's memory and frees it when the 
producer
+        /// calls the handler's release callback, so the caller must not free 
it and
+        /// must not use the pointer after handing it to the producer. A 
handler that
+        /// is never handed to a producer is never released, and leaks.
+        /// </para>
+        /// <para>
+        /// Disposing the returned stream before the end cancels the producer, 
which
+        /// may release the handler some time later.
+        /// </para>
+        /// </remarks>
+        /// <example>
+        /// <code>
+        /// IArrowArrayStream stream = 
CArrowAsyncDeviceStreamConsumer.CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler);
+        /// foreign_produce_function(handler);
+        /// RecordBatch batch = await stream.ReadNextRecordBatchAsync();
+        /// </code>
+        /// </example>
+        /// <param name="handler">Receives the newly allocated handler to pass 
to a producer.</param>
+        /// <param name="queueSize">The number of batches to keep requested 
ahead of the reader.</param>
+        /// <returns>An IArrowArrayStream that yields the received 
batches.</returns>
+        public static unsafe IArrowArrayStream CreateStream(out 
CArrowAsyncDeviceStreamHandler* handler, long queueSize = DefaultQueueSize)
+        {
+            if (queueSize <= 0)
+            {
+                throw new ArgumentOutOfRangeException(nameof(queueSize), 
queueSize, "The queue size must be positive.");
+            }
+
+            CArrowAsyncDeviceStreamHandler* newHandler = 
CArrowAsyncDeviceStreamHandler.Create();
+            var state = new ConsumerState(newHandler, queueSize);
+
+            newHandler->on_schema = OnSchemaPtr;
+            newHandler->on_next_task = OnNextTaskPtr;
+            newHandler->on_error = OnErrorPtr;
+            newHandler->release = ReleasePtr;
+
+            GCHandle gch = GCHandle.Alloc(state);
+            newHandler->private_data = (void*)GCHandle.ToIntPtr(gch);
+
+            handler = newHandler;
+            return state;
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static int OnSchema(CArrowAsyncDeviceStreamHandler* 
handler, CArrowSchema* schema)
+        {
+            try
+            {
+                var state = ConsumerState.FromPointer(handler->private_data);
+                Schema importedSchema = 
CArrowSchemaImporter.ImportSchema(schema);
+                state.SetSchema(importedSchema);
+
+                // Calling request() synchronously from on_schema is 
explicitly allowed.
+                state.RequestFromProducer(state.QueueSize);
+                return 0;
+            }
+            catch (Exception)
+            {
+                return EOTHER;
+            }
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static int OnNextTask(CArrowAsyncDeviceStreamHandler* 
handler, CArrowAsyncTask* task, byte* metadata)
+        {
+            try
+            {
+                var state = ConsumerState.FromPointer(handler->private_data);
+
+                if (task == null)
+                {
+                    // End of stream
+                    state.SignalComplete();
+                    return 0;
+                }
+
+                // The task is only valid during this call, so extract it now. 
Even an
+                // unwanted task must be extracted so the producer can clean 
up. An
+                // output array is always passed, even though the spec allows 
NULL,
+                // because not every producer handles NULL (arrow-go v18.8.0 
doesn't).
+                CArrowDeviceArray cDeviceArray = new CArrowDeviceArray();
+#if NET5_0_OR_GREATER
+                int errno = task->extract_data(task, &cDeviceArray);
+#else
+                int errno = 
NativeDelegate.GetDelegateForFunctionPointer<CArrowAsyncDeviceStreamProducer.ExtractDataDelegate>(task->extract_data)(task,
 &cDeviceArray);
+#endif
+                if (errno != 0)
+                {
+                    // The producer follows a failed extract with on_error.
+                    return 0;
+                }
+
+                if (cDeviceArray.array.release == default)
+                {
+                    return 0;
+                }
+
+                if (state.IsDisposed)
+                {
+                    // Cancelled; drop any batches still in flight.
+                    ReleaseArray(&cDeviceArray.array);
+                    return 0;
+                }
+
+                RecordBatch batch;
+                try
+                {
+                    batch = 
CArrowDeviceArrayImporter.ImportRecordBatch(&cDeviceArray, state.Schema);
+                }
+                catch (Exception ex)
+                {
+                    ReleaseArray(&cDeviceArray.array);
+                    state.SignalError(ex);
+                    return EOTHER;
+                }
+
+                state.EnqueueBatch(batch);
+                return 0;
+            }
+            catch (Exception)
+            {
+                return EOTHER;
+            }
+        }
+
+        private unsafe static void ReleaseArray(CArrowArray* array)
+        {
+            if (array->release != default)
+            {
+#if NET5_0_OR_GREATER
+                array->release(array);
+#else
+                
NativeDelegate.GetDelegateForFunctionPointer<CArrowArrayExporter.ReleaseArrowArray>(array->release)(array);
+#endif
+            }
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static void OnError(CArrowAsyncDeviceStreamHandler* 
handler, int code, byte* message, byte* metadata)
+        {
+            try
+            {
+                var state = ConsumerState.FromPointer(handler->private_data);
+                string msg = message != null ? 
StringUtil.PtrToStringUtf8(message) : $"Async stream error. Code: {code}";
+                state.SignalError(new Exception(msg));
+            }
+            catch (Exception)
+            {
+                // Cannot propagate errors from on_error
+            }
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static void Release(CArrowAsyncDeviceStreamHandler* 
handler)
+        {
+            try
+            {
+                GCHandle gch = 
GCHandle.FromIntPtr((IntPtr)handler->private_data);
+                var state = (ConsumerState)gch.Target;
+
+                // After this, the state no longer touches the handler.
+                state.SignalReleased();
+                gch.Free();
+            }
+            catch (Exception)
+            {
+                // Cannot propagate errors from release
+            }
+
+            // release is the last call the producer makes on the handler, and 
the
+            // consumer allocated it in CreateStream, so it can be freed here.
+            CArrowAsyncDeviceStreamHandler.Free(handler);
+        }
+
+        private sealed unsafe class ConsumerState : IArrowArrayStream
+        {
+            private readonly CArrowAsyncDeviceStreamHandler* _handler;
+            private readonly long _replenishThreshold;
+
+            // Guards the batch queue and waiters.
+            private readonly object _gate = new object();
+            private readonly Queue<RecordBatch> _buffered = new 
Queue<RecordBatch>();
+            private readonly Queue<Waiter> _waiters = new Queue<Waiter>();
+            private readonly TaskCompletionSource<Schema> _schemaReady;
+            private bool _completed;
+            private Exception _error;
+            private long _consumedSinceRequest;
+
+            // Guards every use of the handler and producer structs: the 
handler is
+            // freed in release, and the producer may free its struct as soon 
as
+            // release returns. Held while calling request/cancel, so release
+            // (which takes it too) can't complete in the middle of one.
+            // Separate from _gate so a producer that blocks in request() while
+            // delivering a batch can't deadlock against EnqueueBatch.
+            private readonly object _producerGate = new object();
+            private bool _released;
+            private bool _cancelled;
+
+            private volatile bool _disposed;
+
+            public ConsumerState(CArrowAsyncDeviceStreamHandler* handler, long 
queueSize)
+            {
+                _handler = handler;
+                QueueSize = queueSize;
+                // Re-request in chunks rather than one batch at a time, so the
+                // producer sees fewer request() calls.
+                _replenishThreshold = Math.Max(1, queueSize / 2);
+                _schemaReady = new 
TaskCompletionSource<Schema>(TaskCreationOptions.RunContinuationsAsynchronously);
+            }
+
+            public long QueueSize { get; }
+
+            public bool IsDisposed => _disposed;
+
+            public Schema Schema
+            {
+                get
+                {
+                    // Block until schema is available
+                    return _schemaReady.Task.GetAwaiter().GetResult();
+                }
+            }
+
+            public static ConsumerState FromPointer(void* ptr)
+            {
+                GCHandle gch = GCHandle.FromIntPtr((IntPtr)ptr);
+                return (ConsumerState)gch.Target;
+            }
+
+            public void SetSchema(Schema schema)
+            {
+                _schemaReady.TrySetResult(schema);
+            }
+
+            public void RequestFromProducer(long n)
+            {
+                lock (_producerGate)
+                {
+                    if (_released || _cancelled || _handler->producer == null)
+                    {
+                        return;
+                    }
+
+                    CArrowAsyncProducer* producer = _handler->producer;
+#if NET5_0_OR_GREATER
+                    producer->request(producer, n);
+#else
+                    
NativeDelegate.GetDelegateForFunctionPointer<CArrowAsyncDeviceStreamProducer.RequestDelegate>(producer->request)(producer,
 n);
+#endif
+                }
+            }
+
+            private void CancelProducer()
+            {
+                lock (_producerGate)
+                {
+                    if (_released || _cancelled)
+                    {
+                        return;
+                    }
+                    _cancelled = true;
+
+                    CArrowAsyncProducer* producer = _handler->producer;
+                    if (producer == null)
+                    {
+                        // Never attached to a producer, so there's nothing to 
cancel.
+                        return;
+                    }
+#if NET5_0_OR_GREATER
+                    producer->cancel(producer);
+#else
+                    
NativeDelegate.GetDelegateForFunctionPointer<CArrowAsyncDeviceStreamProducer.CancelDelegate>(producer->cancel)(producer);
+#endif
+                }
+            }
+
+            /// <summary>
+            /// Called whenever a batch is handed to the reader; asks the 
producer for
+            /// more once enough of the queue has drained.
+            /// </summary>
+            private void OnBatchConsumed()
+            {
+                long toRequest = 0;
+                lock (_gate)
+                {
+                    if (++_consumedSinceRequest >= _replenishThreshold)
+                    {
+                        toRequest = _consumedSinceRequest;
+                        _consumedSinceRequest = 0;
+                    }
+                }
+
+                if (toRequest > 0)
+                {
+                    RequestFromProducer(toRequest);
+                }
+            }
+
+            public void EnqueueBatch(RecordBatch batch)
+            {
+                Waiter waiter = null;
+                lock (_gate)
+                {
+                    // Skip waiters whose read was cancelled; handing the 
batch to
+                    // one of them would lose it.
+                    while (_waiters.Count > 0)
+                    {
+                        Waiter candidate = _waiters.Dequeue();
+                        if (candidate.Completion.TrySetResult(batch))
+                        {
+                            waiter = candidate;
+                            break;
+                        }
+                    }
+
+                    if (waiter == null)
+                    {
+                        _buffered.Enqueue(batch);
+                    }
+                }

Review Comment:
   Fixed in 81be4ae. `EnqueueBatch` now checks `_disposed` under `_gate`. 
`Dispose` sets the flag before it drains the queue under the same lock, so each 
batch is either drained or rejected, and rejected batches are disposed outside 
the lock. While I was there, I also made `Dispose` fail any reads that are 
still waiting with `ObjectDisposedException`, so they no longer wait forever.



##########
src/Apache.Arrow/C/CArrowAsyncDeviceStreamProducer.cs:
##########
@@ -0,0 +1,618 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements. See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+using System;
+using System.Diagnostics.CodeAnalysis;
+using System.Runtime.InteropServices;
+using System.Threading.Tasks;
+using Apache.Arrow.Ipc;
+
+namespace Apache.Arrow.C
+{
+    /// <summary>
+    /// Produces data from an <see cref="IArrowArrayStream"/> by pushing it 
through
+    /// an <see cref="CArrowAsyncDeviceStreamHandler"/>.
+    /// </summary>
+    [Experimental("ArrowAsyncDeviceStreamApi")]
+    public static class CArrowAsyncDeviceStreamProducer
+    {
+        private const int EINVAL = 22;
+        private const int EOTHER = 131;
+
+#if NET5_0_OR_GREATER
+        private static unsafe delegate* unmanaged<CArrowAsyncProducer*, long, 
void> RequestPtr => &OnRequest;
+        private static unsafe delegate* unmanaged<CArrowAsyncProducer*, void> 
CancelPtr => &OnCancel;
+        private static unsafe delegate* unmanaged<CArrowAsyncTask*, 
CArrowDeviceArray*, int> ExtractDataPtr => &ExtractData;
+#else
+        internal unsafe delegate void RequestDelegate(CArrowAsyncProducer* 
producer, long n);
+        private static unsafe readonly NativeDelegate<RequestDelegate> 
s_request = new NativeDelegate<RequestDelegate>(OnRequest);
+        private static unsafe IntPtr RequestPtr => s_request.Pointer;
+        internal unsafe delegate void CancelDelegate(CArrowAsyncProducer* 
producer);
+        private static unsafe readonly NativeDelegate<CancelDelegate> s_cancel 
= new NativeDelegate<CancelDelegate>(OnCancel);
+        private static unsafe IntPtr CancelPtr => s_cancel.Pointer;
+        internal unsafe delegate int ExtractDataDelegate(CArrowAsyncTask* 
task, CArrowDeviceArray* deviceArray);
+        private static unsafe readonly NativeDelegate<ExtractDataDelegate> 
s_extractData = new NativeDelegate<ExtractDataDelegate>(ExtractData);
+        private static unsafe IntPtr ExtractDataPtr => s_extractData.Pointer;
+#endif
+
+        /// <summary>
+        /// Produce data from an <see cref="IArrowArrayStream"/> by pushing it 
through
+        /// the provided handler. The producer populates the handler's 
producer field
+        /// and calls on_schema before this method returns; batches are then 
delivered
+        /// asynchronously as the consumer requests them.
+        /// </summary>
+        /// <remarks>
+        /// Handler callbacks are never invoked concurrently, and the 
handler's release
+        /// callback is called exactly once, as the last call on the handler. 
The
+        /// source stream is disposed after release. As with the C stream 
exporters,
+        /// the producer takes ownership of each batch the source returns and 
disposes
+        /// it once the consumer has released (or declined) the exported data.
+        /// </remarks>
+        /// <param name="arrayStream">The source stream to produce 
from.</param>
+        /// <param name="handler">The consumer-allocated handler to push data 
through.</param>
+        public static unsafe void Produce(IArrowArrayStream arrayStream, 
CArrowAsyncDeviceStreamHandler* handler)
+        {
+            if (arrayStream == null)
+            {
+                throw new ArgumentNullException(nameof(arrayStream));
+            }
+            if (handler == null)
+            {
+                throw new ArgumentNullException(nameof(handler));
+            }
+
+            var state = new ProducerState(arrayStream, handler);
+            state.Start();
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static void OnRequest(CArrowAsyncProducer* producer, 
long n)
+        {
+            // Exceptions must not escape an unmanaged callback. request() has 
no
+            // return value; errors it detects are reported later via on_error.
+            try
+            {
+                var state = ProducerState.FromPointer(producer->private_data);
+                state.Request(n);
+            }
+            catch (Exception)
+            {
+            }
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static void OnCancel(CArrowAsyncProducer* producer)
+        {
+            // Exceptions must not escape an unmanaged callback.
+            try
+            {
+                var state = ProducerState.FromPointer(producer->private_data);
+                state.Cancel();
+            }
+            catch (Exception)
+            {
+            }
+        }
+
+#if NET5_0_OR_GREATER
+        [UnmanagedCallersOnly]
+#endif
+        private unsafe static int ExtractData(CArrowAsyncTask* task, 
CArrowDeviceArray* deviceArray)
+        {
+            // Consumers may extract after the producer has finished and 
released
+            // the handler (arrow-go does), so this must only depend on the 
task.
+            TaskState taskState = null;
+            try
+            {
+                // extract_data is called exactly once per task, so the task's
+                // GCHandle is always freed here. Clearing private_data makes 
an
+                // erroneous second call fail cleanly instead of touching a 
freed handle.
+                GCHandle gch = GCHandle.FromIntPtr((IntPtr)task->private_data);
+                task->private_data = null;
+                try
+                {
+                    taskState = (TaskState)gch.Target;
+                    if (deviceArray != null)
+                    {
+                        // Like the C stream exporters, take over the source 
stream's
+                        // reference: the exported array disposes the batch 
when released
+                        // (or right away if the export fails).
+                        
CArrowDeviceArrayExporter.ExportRecordBatch(taskState.Batch, deviceArray, 
transferOwnership: true);
+                    }
+                    else
+                    {
+                        // The consumer doesn't want the data.
+                        taskState.Batch.Dispose();
+                    }
+                    return 0;
+                }
+                finally
+                {
+                    gch.Free();
+                }
+            }
+            catch (Exception ex)
+            {
+                // The spec says a failed extract_data is followed by on_error.
+                taskState?.Producer.ReportError(EOTHER, ex.Message);
+                return EOTHER;
+            }
+        }
+
+        /// <summary>
+        /// What the produce loop should do next.
+        /// </summary>
+        private enum Step
+        {
+            /// <summary>No demand: the loop exits until the next 
request().</summary>
+            Idle,
+            /// <summary>Read a batch from the source and deliver it.</summary>
+            Produce,
+            /// <summary>Report a pending error, then release.</summary>
+            Error,
+            /// <summary>Cancelled: release without further 
callbacks.</summary>
+            Release,
+        }
+
+        /// <summary>
+        /// State for one produced stream.
+        /// </summary>
+        /// <remarks>
+        /// All handler callbacks after on_schema are made from a single 
produce loop,
+        /// and at most one loop runs at a time (<see cref="_running"/>), so 
callbacks
+        /// are never concurrent. request() and cancel() only update state 
under
+        /// <see cref="_gate"/> and start the loop if it isn't running; they 
never call
+        /// back into the handler themselves, as the spec requires.
+        /// </remarks>
+        private sealed unsafe class ProducerState
+        {
+            private readonly IArrowArrayStream _arrayStream;
+            private readonly CArrowAsyncDeviceStreamHandler* _handler;
+            private CArrowAsyncProducer* _producer;
+
+            private readonly object _gate = new object();
+            private long _demand;
+            private bool _schemaDelivered;
+            private bool _running;
+            private bool _cancelled;
+            private bool _finished;
+            private bool _hasPendingError;
+            private int _pendingErrorCode;
+            private string _pendingErrorMessage;
+
+            public ProducerState(IArrowArrayStream arrayStream, 
CArrowAsyncDeviceStreamHandler* handler)
+            {
+                _arrayStream = arrayStream;
+                _handler = handler;
+            }
+
+            public IArrowArrayStream ArrayStream => _arrayStream;
+
+            public static ProducerState FromPointer(void* ptr)
+            {
+                GCHandle gch = GCHandle.FromIntPtr((IntPtr)ptr);
+                return (ProducerState)gch.Target;
+            }
+
+            public void Start()
+            {
+                // Allocate and populate the producer struct
+                _producer = CArrowAsyncProducer.Create();
+                _producer->device_type = ArrowDeviceType.Cpu;
+                _producer->request = RequestPtr;
+                _producer->cancel = CancelPtr;
+                _producer->additional_metadata = null;
+
+                GCHandle gch = GCHandle.Alloc(this);
+                _producer->private_data = (void*)GCHandle.ToIntPtr(gch);
+
+                // Set the producer on the handler before calling on_schema
+                _handler->producer = _producer;
+
+                // Deliver the schema. If it can't be exported, the consumer 
still
+                // has to hear about it: on_error followed by release.
+                CArrowSchema cSchema = new CArrowSchema();
+                try
+                {
+                    CArrowSchemaExporter.ExportSchema(_arrayStream.Schema, 
&cSchema);
+                }
+                catch (Exception ex)
+                {
+                    FailWithError(EOTHER, ex.Message);
+                    return;
+                }
+
+                // The consumer may call request() or cancel() from within 
on_schema.
+                // That only records state: the loop doesn't start until 
on_schema
+                // has returned, so on_next_task can't overlap it.
+#if NET5_0_OR_GREATER
+                int errno = _handler->on_schema(_handler, &cSchema);
+#else
+                int errno = 
NativeDelegate.GetDelegateForFunctionPointer<CArrowAsyncDeviceStreamConsumer.OnSchemaDelegate>(_handler->on_schema)(_handler,
 &cSchema);
+#endif
+
+                // A consumer that fails may not have taken ownership of the 
schema.
+                if (cSchema.release != default)
+                {
+#if NET5_0_OR_GREATER
+                    cSchema.release(&cSchema);
+#else
+                    
NativeDelegate.GetDelegateForFunctionPointer<CArrowSchemaExporter.ReleaseArrowSchema>(cSchema.release)(&cSchema);
+#endif
+                }
+
+                if (errno != 0)
+                {
+                    // Per the spec: no on_error after a failed on_schema, 
just release.
+                    Finish();
+                    return;
+                }
+
+                bool startLoop;
+                lock (_gate)
+                {
+                    _schemaDelivered = true;
+                    startLoop = TryClaimLoopLocked();
+                }
+                if (startLoop)
+                {
+                    StartLoop();
+                }
+            }
+
+            public void Request(long n)
+            {
+                bool startLoop;
+                lock (_gate)
+                {
+                    // After cancel, request must be a no-op.
+                    if (_cancelled || _finished)
+                    {
+                        return;
+                    }
+
+                    if (n <= 0)
+                    {
+                        SetPendingErrorLocked(EINVAL, $"request() called with 
invalid n = {n}; n must be positive.");
+                    }
+                    else
+                    {
+                        _demand = n > long.MaxValue - _demand ? long.MaxValue 
: _demand + n;
+                    }
+
+                    startLoop = TryClaimLoopLocked();
+                }
+                if (startLoop)
+                {
+                    StartLoop();
+                }
+            }
+
+            public void Cancel()
+            {
+                bool startLoop;
+                lock (_gate)
+                {
+                    // cancel must be idempotent.
+                    if (_cancelled || _finished)
+                    {
+                        return;
+                    }
+
+                    _cancelled = true;
+                    startLoop = TryClaimLoopLocked();
+                }
+                if (startLoop)
+                {
+                    StartLoop();
+                }
+            }
+
+            /// <summary>
+            /// Report an error to the consumer from outside the produce loop
+            /// (currently, a failed extract_data). Ignored once the stream is 
done.
+            /// </summary>
+            public void ReportError(int code, string message)
+            {
+                bool startLoop;
+                lock (_gate)
+                {
+                    if (_finished)
+                    {
+                        return;
+                    }
+
+                    SetPendingErrorLocked(code, message);
+                    startLoop = TryClaimLoopLocked();
+                }
+                if (startLoop)
+                {
+                    StartLoop();
+                }
+            }
+
+            private void SetPendingErrorLocked(int code, string message)
+            {
+                if (!_hasPendingError)
+                {
+                    _hasPendingError = true;
+                    _pendingErrorCode = code;
+                    _pendingErrorMessage = message;
+                }
+            }
+
+            /// <summary>
+            /// Returns true if the caller should start the produce loop, in 
which case
+            /// <see cref="_running"/> has been set and the caller owns 
starting it.
+            /// </summary>
+            private bool TryClaimLoopLocked()
+            {
+                if (_running || _finished || !_schemaDelivered)
+                {
+                    return false;
+                }
+
+                if (_cancelled || _hasPendingError || _demand > 0)
+                {
+                    _running = true;
+                    return true;
+                }
+
+                return false;
+            }
+
+            private void StartLoop()
+            {
+                // A separate task keeps request() from ever calling back into 
the
+                // handler synchronously.
+                Task.Run(() => RunProduceLoopAsync(this));
+            }
+
+            /// <summary>
+            /// Decide the loop's next step. When there's nothing to do, this 
marks
+            /// the loop as stopped under the same lock that request() takes, 
so a
+            /// concurrent request() either is seen here or starts a new loop.
+            /// </summary>
+            public Step TakeNextStep(out int errorCode, out string 
errorMessage)
+            {
+                errorCode = 0;
+                errorMessage = null;
+                lock (_gate)
+                {
+                    if (_hasPendingError)
+                    {
+                        errorCode = _pendingErrorCode;
+                        errorMessage = _pendingErrorMessage;
+                        return Step.Error;
+                    }
+
+                    if (_cancelled)
+                    {
+                        return Step.Release;
+                    }
+
+                    if (_demand == 0)
+                    {
+                        _running = false;
+                        return Step.Idle;
+                    }
+
+                    _demand--;
+                    return Step.Produce;
+                }
+            }
+
+            /// <summary>
+            /// Deliver a batch (or end of stream, if null) to the consumer. 
Returns
+            /// false if the stream is now finished.
+            /// Separated from the async loop because unsafe pointer operations
+            /// cannot appear in async methods.
+            /// </summary>
+            public bool DeliverBatch(RecordBatch batch)
+            {
+                if (batch == null)
+                {
+                    SignalEndOfStream();
+                    Finish();
+                    return false;

Review Comment:
   Fixed in 81be4ae. After each source read, the producer decides what to do 
next under the lock: a pending error, then cancellation, then end-of-stream or 
delivery. Once it commits to its final callbacks it sets `_completing`, so a 
later `request`, `cancel` or error report is ignored and can't be lost to 
end-of-stream. This one is a race between threads, so I haven't added a test 
for it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to