-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
cgo: Taking a reference to a cgo Handle, as documented, appears to return bad handles. #71566
Comments
Maybe the system works exactly as intended, but it's just the documentation that shouldn't suggest taking a reference to a handle? |
Related Issues
Related Code Changes
Related Documentation Related Discussions (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
I don't think we can judge what's wrong without seeing a complete example, though the error suggests you're not properly pinning memory. given the reported panic, I think this is a question on how to pass memory, rather than a bug report. Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only. For questions please refer to https://github.com/golang/go/wiki/Questions |
The example code at https://pkg.go.dev/runtime/cgo#Handle looks somewhat misleading. The approach works fine provided the C code does not hold on to the pointer. It's impossible to tell for sure without a complete example, but I suspect that your C code want to hold on to the pointer. |
Change https://go.dev/cl/647095 mentions this issue: |
Thanks, that's what I suspected, and yes. I do hold on to the pointer. I can see the updated comment clarifies the case :) |
For #71566 Change-Id: I6dc365dd799d7b506b4a55895f1736d3dfd4684b Reviewed-on: https://go-review.googlesource.com/c/go/+/647095 Reviewed-by: Alan Donovan <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Commit-Queue: Ian Lance Taylor <[email protected]>
Go version
go version go1.23.6 darwin/arm64
Output of
go env
in your module/workspace:What did you do?
Using v8go, I store a reference to a Go object as an "external" in an internal field, a mechanism in V8 intended to store pointers to objects in the host. The API takes a C
void*
as argument.The v8go version is my own fork, using the following branch: https://github.com/stroiman/v8go/tree/go-dom-support
The cgo package documentation suggests that you can take a reference to a
cgo.Handle
.So to store a reference to a Go object as a V8 external, I
I read the handle again using:
What did you see happen?
I get erratic panics
After a lot of trial and error, I logged
cgo.Handle
after conversionvoid*
value retrieved from the external valueDuring a single test, the same value is read many times from the same object, and I get the following output
So I get the same
void*
from the v8 external, but suddenly, it translates to a different value. I assume that this is because GC has kicked in?I can trigger an even harder crash by calling
runtime.GC()
before reading the handle.What did you expect to see?
I expected that the same
void*
/unsafe.Pointer
should consistently be converted to the samecgo.Handle
when using the documented approach.I tried to create a minimal example in the playground, create a handle, convert to unsafe ptr, GC, and convert back, but that resulted in build errors in the playground.
I will be happy to try to reproduce a more minimal example, but it will not be this week.
As a workaround, I'll try to treat the handles as uint32 values, which is also possible to store in a v8 object.
The text was updated successfully, but these errors were encountered: