Skip to content

[Rust] Correct object ownership and callback threading contracts - #770

Closed
tlopex wants to merge 7 commits into
apache:mainfrom
tlopex:fix/rust-thread-local-callbacks
Closed

[Rust] Correct object ownership and callback threading contracts#770
tlopex wants to merge 7 commits into
apache:mainfrom
tlopex:fix/rust-thread-local-callbacks

Conversation

@tlopex

@tlopex tlopex commented Sep 8, 2026

Copy link
Copy Markdown
Member

This PR updates Rust object and callback safety using the existing APIs, without introducing thread-local functions or new C ABI interfaces.

  • Remove the blanket Send + Sync bounds from Function::from_packed and Function::from_typed. These constructors become unsafe: callers must preserve the captures’ threading requirements, including calls and final release through native copies.
  • Remove unconditional Send/Sync implementations for Function. Make global registration unsafe, since registered functions become accessible across threads.
  • Remove ObjectArc::DerefMut, which could overwrite a live reference-count header even under unique ownership. Keep the common object header free of thread-confinement markers, without inferring that arbitrary object handles are thread-safe.
  • Make reflected field getters accept owning handles and update stubgen accordingly.
  • Fix Rust-allocated arrays to release their live elements correctly, including after native mutation or partial initialization.
  • Handle empty shapes without constructing slices from null pointers.

These are Rust source-compatibility changes. The C ABI and object layouts remain unchanged.

Destroy initialized elements when the last strong reference is released, while preserving weak-reference allocation cleanup. Track initialized length during construction and cover native mutation and foreign-thread callback cleanup.
…e#770)

Separate the neutral ABI header from the full-object thread-safety contract. Keep shareable Functions restricted to Send + Sync captures, remove TLS callback APIs, and require handles for reflected field reads.

Remove generic ObjectArc DerefMut so safe replacement cannot overwrite a live intrusive header. Update container initialization and generated accessors while retaining the independent array and weak-owner lifetime fixes.
@tlopex tlopex changed the title [Rust][FEAT] Add owner-thread local Function callbacks [Rust] Correct object ownership and callback threading contracts Sep 9, 2026
Comment thread rust/tvm-ffi/src/collections/array.rs
Comment thread rust/tvm-ffi/src/macros.rs Outdated
@tlopex tlopex closed this Sep 9, 2026
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.

3 participants