Repository navigation
Add block_on_detached and maintain separate task queues per executor - #1794
tschneidereit wants to merge 2 commits into
Conversation
`rust_spawn` now pushes the task onto a queue in the `SharedTaskState` its `ptr` argument points to, and each executor drains only its own queue. Previously all executors of one `wit-bindgen` copy shared a static queue, so a `block_on` call ran tasks that the enclosing task had spawned earlier in the same poll.
`block_on_detached` is the same as `block_on`, except that tasks spawned with `spawn_local` during the call are spawned onto the task that encloses the call, through that task's `rust_spawn` hook. That task's executor runs them after the call returns, where `block_on` would drain them inline.
|
For context, I need something like this for debugging support in StarlingMonkey, where tasks spawned from under the debugger shouldn't go on the debugger's executor's queue. Also, I'm happy to split this into separate PRs if that seems better. The first one didn't seem very strongly motivated without the second one. |
alexcrichton
left a comment
There was a problem hiding this comment.
Trying to think through this, would it perhaps make sense to delete block_on_detached and have that just be the default behavior of block_on? I sort of wish we could get rid of spawn_local given how fundamentally broken it is in relation to block_on... Another alternative would to to move to something more Tokio-like where executors are more explicitly created and managed, and in such a scenario block_on would be forbidden within an async task because you would be disallowed from reentrantly entering an executor.
|
If you think that making this the default behavior will not cause meaningful breakage, then yes, that'd make sense to me! I'll look into it and update the PR if that seems to work out. (Whereas I don't think I have the expertise needed to make bigger changes along the lines of what you described.) |
|
Yeah if anything I'd expect it to help more than it hurts because that way |
|
This isn't entirely straightforward a change, unfortunately. Might still make sense, but has some downsides:
On the general idea of forbidding Looking back at this, it all seems reasonable to me, though the third issue seems kinda gnarly. WDYT? |
|
Hm... could you detail a bit more about the rationale for this? Why is it required to get recursive invocations working? For example it sounds like |
|
No, I want spawned tasks to be polled by the outer executor—hence the need for Conversely, if we panic instead of forwarding, doing things like evaluating JS that calls |
|
To clarify, my understanding is that this PR as-is behaves as:
Additionally, in my mind, You say you want spawned tasks to be polled by the outer executor, can you clarify if you need that during the |
That's pretty much it, yeah. One slight correction on
Only after, so things like And yeah, fundamentally I do need a nested executor / event loop here. I know those suck, but they're needed here. The same is true in browser devtools as well, and leads to a bunch of complexity in engines. (Here's as an example V8's version of this). This requirement follows from the fact that the debugger itself is implemented in the same engine as the debuggee, and needs an event loop of its own, all the while the outer event loop is paused and the stack can't be unwound until the debugger continues. If you're interested in more details for SpiderMonkey specifically: The way debugging works in SpiderMonkey is that there's a The code implementing the debugger's server side is implemented in JS itself, and can use async features. And has to, because it communicates via a socket, so must be able to do IO. |
|
Hm ok, well somehow I still feel that something needs to change here but I'm still not entirely sure how. Already today Originally I was thinking we could just fold In the V8 header you linked, that mentions reference to what I was wondering about as well -- could threads be used here? Instead of trying to use Otherwise, another question, why does debugger/debuggee work need to spawn a task which is disconnected from |
|
Yeah, I do see your point on how confusing this is all getting. And I think threads would probably work for this. I guess I can depend on a fork of wit-bindgen until coop threads are enabled by default. I first thought I'd also be blocked on a Rust release with threads support, but I think I can make it work with just WASI-SDK.
The debugger doesn't ever spawn tasks, but the debuggee might: since JS debuggers support calling code, or more general However, I'm very glad you're pushing on this, because I finally realized that what I'd get with I can do all this with threads, but also now wonder if there is a reasonable semantics for
I think that'd resolve your bad scenarios b-d: for (b), there's no more panic; for (c), tasks are always polled by something; for (d) If you think that might be reasonable, I'm happy to work through it in more detail. But otherwise, feel free to close this for now, and I'll focus on the threads-based approach in the long run, and use a fork short-term. |
|
Chalk up another use case for threads... But yeah I was also trying to figure out if there's something to do with
Overall this is where I still would like to (eventually) push on forbidding nested executors. If you're able to use threads that'd already decouple them to fit within that requirement I think, and the concerns here are kind of all downstream of allowing nested executors at all. Disallowing nested executors wouldn't fix all my concerns (e.g. |
|
Okay, that all makes sense, and I think we can start to wrap this up here, I think :) One question on this PR: do you think landing the first commit still makes sense? It seems to me that it does, and improves the situation, albeit not by that much. On the long-term goal of forbidding nested executors, I think once coop threads are stable, that does become viable. I had a clanker do an audit of existing uses of Cases it found include some in Golem projects [1,2,3,4], and one in Lann's polymorph-iroh. All of these should port pretty easily to a threads-based approach, I think. |
|
Oh, right, yes! I'm always a fan of deleting And ok sounds like we "just" need to wait a bit on the disallowance... |
|
Okay, I opened #1795 for that and will close this PR. |
The first commit here fixes issues with task queue management, by maintaining separate queues per executor. Building on that, the second one introduces a new function,
block_on_detached, which blocks on a single task, but doesn't use the inner executor's queue for tasks spawned while processing that task.