Skip to content
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

Ownership of resource handles #148

Open
bkolobara opened this issue Feb 4, 2022 · 4 comments
Open

Ownership of resource handles #148

bkolobara opened this issue Feb 4, 2022 · 4 comments
Labels
resources Issues with component resources and using them

Comments

@bkolobara
Copy link

I'm would like to use wit to generate host/wasmtime and guest/rust bindings for lunatic. However, I'm still not sure how to correctly model a specific use case.

In lunatic some of the host resources can be sent between different instances. This means that they are consumed by some host functions and removed from the resource table. This doesn't work well with the current Canonical ABI proposal that requires all resources to expose a clone and drop API and implies reference counting.

One workaround is to keep the host resource inside of an Option<T> and remove it once it needs to be sent to another instance. Then on every invocation of a host function dealing with the resource check if the value is Some(T). This works okish.

The bigger issues here is that the guest side generated code also generates a Clone and Drop implementation. We would like to avoid exposing the Clone functionality to developers though. It would make the API better reflect the underlaying properties of the resource. Is there a way of disabling this Clone option, and ideally not even use reference counting inside of the host in this case?

I guess my question i more general. How to handle this specific scenario with interface types, when the function "consumes" the resource?

I also feel like I didn't completely understand the difference between a resource and a resource handle. Maybe the automatic cloning can be avoided if using resources directly, instead of handles?

@alexcrichton
Copy link
Member

I think this is probably best addressed at the interface-types layer rather than wit-bindgen here. There's not really a way to simply tweak codegen to remove Clone impls since they're reflecting how interface types handles/resources are expected to work.

I believe what you want for your use case is a Rust-like owned resource and perhaps a borrowed resource as well. We've discussed this internally in the past but they haven't ever gotten over the finish line to the point of being spec'd and implemented. For now I'd recommend opening an issue on the interface-types repository to track the feature request.

@alexcrichton alexcrichton added the resources Issues with component resources and using them label May 3, 2022
@ricochet
Copy link

own and borrow types will soon be added to the component-model. I believe this will handle your use-case WebAssembly/component-model#129

@juntyr
Copy link
Contributor

juntyr commented Jan 28, 2023

I am currently working on a small side project that will need the new own and borrow types in wit-bindgen (I wrote an original prototype when the old resource implementation was still around). Is there a rough timeline on when they will be implemented, or some way I could help contribute with their implementation?

@alexcrichton
Copy link
Member

Resources are pretty high up on the list of things to do, but I don't have a timeline for when they'll be done. Work on resources will span many repositories and many aspects of the design an implementation, so unfortunately I don't know of a great way to have the work spread out amongst many at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resources Issues with component resources and using them
Projects
None yet
Development

No branches or pull requests

4 participants