amlaban65 opened a new pull request, #1575:
URL: https://github.com/apache/arrow-go/pull/1575

   ### Rationale for this change
   
   Fixes #1573.
   
   If the `on_schema` callback of a handler from 
`CreateAsyncDeviceStreamHandler` fails, the handler's `release` callback panics 
afterwards with `runtime/cgo: misuse of an invalid Handle`. `on_schema` fails 
when the producer's device type isn't CPU or when `ImportCArrowSchema` returns 
an error. The spec says a producer should then call `release` rather than 
`on_error`, so any conforming producer hits this. arrow-go's own 
`exportAsyncProducer` does the same, since it always calls `release` in a defer.
   
   The cause is in `asyncStreamOnSchema`. It defers `C.free(self.private_data)` 
and `h.Delete()`, so they run on the failure paths too. Those paths return 
`EINVAL` without clearing `self.private_data`, so `asyncStreamRelease` then 
reads the handle from freed memory and would free `private_data` a second time.
   
   ### What changes are included in this PR?
   
   - `asyncStreamOnSchema` sets `self.private_data = nil` right after deferring 
the cleanup of the old state. The deferred calls already captured the old 
pointer and handle. On success `private_data` is replaced with the stream 
handler's handle as before, so that path is unchanged.
   - `asyncStreamRelease` handles `private_data == nil`: the state is already 
gone, so it only marks the handler as released (`release = nil`).
   
   This PR doesn't touch #1572, the separate issue about `request()` being 
called after release.
   
   ### Are these changes tested?
   
   Yes. I added `TestAsyncReleaseAfterOnSchemaFailure` to 
`arrow/cdata/cdata_test.go`, plus small helpers in `cdata_test_framework.go`. 
The test acts as a C producer: it calls the handler's `on_schema` and then 
`release`, as the spec requires after a failure, for both failure paths:
   
   - a producer whose device type is CUDA
   - a CPU producer whose schema has the format `~bogus`, which 
`ImportCArrowSchema` rejects
   
   It checks that the consumer gets the error, that `on_schema` returns 
`EINVAL`, and that `release` marks the handler as released.
   
   - Without the fix, both subtests crash with `panic: runtime/cgo: misuse of 
an invalid Handle` in `asyncStreamRelease`, matching the trace in the issue.
   - With the fix, `go test -tags test,assert -short ./arrow/cdata/...` passes, 
and so does `go test -race -tags test -count=5 -run TestAsync ./arrow/cdata/`. 
golangci-lint (staticcheck, repo config) reports nothing new on the touched 
files. I ran these on Linux amd64 with Go 1.26.11.
   
   I didn't run the full module test suite or the `noasm` pass.
   
   ### Are there any user-facing changes?
   
   No API changes. A handler whose `on_schema` failed can now be released 
without crashing the process.
   


-- 
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