Skip to content

Conversation

@sumleo
Copy link

@sumleo sumleo commented Feb 12, 2026

Summary

  • FromBorrowHandle for imported resources was incorrectly delegating to FromOwnHandle, which registers a GC finalizer via runtime.AddCleanup
  • This violates the Component Model specification: borrowed handles are not owned by the callee, so the callee must not drop them
  • When Go's garbage collector ran, it would invoke the finalizer and call resourceDrop on the borrowed handle, corrupting the host's resource table
  • Fixed FromBorrowHandle to create a handle object directly without registering a GC finalizer, preserving correct ownership semantics

Test plan

  • Added integration test tests/go_borrow_handle.rs that generates Go code for an imported resource with borrow<blob> parameter
  • Verified the generated FromBorrowHandle function does NOT contain FromOwnHandle delegation or AddCleanup calls
  • The existing FromBorrowHandle for exported resources (which correctly uses pointer-based rep without finalizer) is unchanged
  • All existing workspace tests pass

Add wit-bindgen-core, wit-bindgen-rust, wit-bindgen-go, and wit-parser
as workspace dev-dependencies to support integration test files.
FromBorrowHandle for imported resources was incorrectly delegating to
FromOwnHandle, which registers a GC finalizer via runtime.AddCleanup.
This violates the Component Model specification: borrowed handles are
not owned by the callee, so the callee must not drop them. When Go's
garbage collector ran, it would drop borrowed handles through the
finalizer, corrupting the host's resource table.

Fix FromBorrowHandle to create a handle object directly without
registering a GC finalizer, preserving the correct ownership semantics.
@sumleo sumleo force-pushed the fix/go-borrow-handle branch from 4820b50 to bb1d9f3 Compare February 12, 2026 13:48
@alexcrichton
Copy link
Member

Thanks! Would it be possible to move the test to something in tests/runtime/* to actually exercise the generated/compiled code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants