dicej requested pchickey for a review on PR #15178.
dicej opened PR #15178 from dicej:wasi-hooks-and-permits to bytecodealliance:main:
This adds a new
WasiHookstrait, modeled loosely on the existingWasiHttpHookstrait. For now, this new trait has only one interesting method,acquire, which is called any time the guest wants to open a host file descriptor. That method allows the embedder to apply a semaphore-like policy which may approve, delay, or reject the allocation, e.g. to keep the process-wide total under a certain limit and/or ensure equitable allocation across multiple guests. Approval is represented as aBox<dyn WasiPermit>, whichwasmtime-wasiwill drop immediately after closing the associated file descriptor, thereby notifying the embedder that it was released, possibly unblocking another pending request.The other
WasiHooksmethod,owned, returns a "clone" ofselffor use in'staticcontexts. This is needed so that e.g.TcpListenStreamandTcpSocketare able to acquire additional permits on demand for any accepted sockets, and they must remaing'staticso they can be stored in the resource table.Note that I haven't added any test coverage for this feature yet; I'd like to get feedback on the overall approach first.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease review the Bytecode Alliance's AI tool usage policy at
https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.mdPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
github-actions[bot] added the label wasi on PR #15178.
dicej updated PR #15178.
dicej updated PR #15178.
:memo: alexcrichton submitted PR review:
Personally I'm a bit hesitant about this style of design. This feels quite heavyweight and tacked on to me, for example:
- This has quite a lot of
Arc,Box, dynamic dispatch, dynamic dispatch on drop, etc. While WASI isn't exactly the most streamlined implementation, and I'm not saying that these costs should all be zero, this feels very heavyweight to me.- Inherently using ownership here is another cost in terms of runtime performance as it sort of forces the use of extra allocations behind the scenes to implement anything nontrivial.
- This feels a bit tacked on to me as it's changing a quite-visible change to a core type,
WasiCtxView, which pretty much every embedding uses. This additionally takes a different route ofWasiSocketsCtx::socket_addr_checkwhich is pretty similar in spirit to this at least.I don't have a great fix per se for all of this, but I think it might be worth exploring and/or brainstorming about other possible designs. One example is that having both
*Ctxand*Hookis kind of a bummer, pretty much everything you might configure throughWasiCtxis more-or-less also suitable throughWasiHooks. This is also true for the preexisting HTTP bits as well. In theory it might be reasonable to delete all the*Ctxstructures entirely, have per-proposal*Hookstraits, and then the*Viewtypes would have the table +dyn *Hooks. Whether or not something like that makes sense I'm not sure, but it would unify the nature of preexisting callbacks we have with this new callback.Cost-wise one possible alternative would be a trait method for acquisition and a trait method for dropping. That's more difficult to plumb around since ownership can't be used, but I believe it should be feasible nonetheless. Given how few locations we have that create fds, coupled with how few locations we drop fds (basically resource destructors), this seems relatively tractable to me. One downside of such an approach though is that it requires manual annotation of "this is an fd that should get limited", whereas the type-based approach here makes it much harder to forget.
Mostly though I want to highlight how while I can see how this solves the problem I think this is being inserted in such a way that doesn't mesh well with the rest of the system, and I think comes a bit too high of a cost.
dicej updated PR #15178.
Last updated: Nov 19 2026 at 20:05 UTC