A novel approach to async scoped tasks?

I've made a small library demonstrating what I think represents a previously unexplored point in the "async task that borrow from their parent scope" API space. The basic idea is to use Future::poll as a way of verifying that the borrows of child tasks remain valid, and then wake the child tasks so they can try to do some work while their borrows are still valid. We then block inside the parent task's poll method until all the child tasks have yielded. This enables the parent future to be somewhat non-blocking. I think the idea is sound, but I haven't really talked to anyone else about it before now. I'm also not sure my implementation is sound. The API also isn't the prettiest, but that doesn't matter that much for now (the tests are honestly also not great).

Here's the implementation, along with slightly some more detailed notes on how it works (it's ~800 lines including comments): async_scoped_task/src/lib.rs at master · maroider/async_scoped_task · GitHub

Any and all feedback is appreciated :‍)

Afaik this has been tried before, but Tokio's work-stealing scheduler makes it impossible to do this safely. At any time a future can be polled from a completely different thread, which invalidates the idea of "scoped" tasks.

This is sound, but will probably lead to deadlocks. Imagine that you have N cpus and then N different scopes get polled. They all wake up their child futures, but no child futures can run because all workers are occupied by the parent futures.

(Or it can be made sound - I have not looked at your concrete implementation.)

2 Likes

The deadlock issue is something I'm aware of, but I left implementing a workaround for "future me".

You could perhaps add timeouts, but a similar problem exists on single-threaded runtimes where you can never run the scoped tasks.

Yeah, that's another limitation I'm aware of (I really should've made my post more detailed). I've been contemplating making the scope work-stealing, but I'm a little hesitant of going that route, seeing as my main use-case for this is running it on top of tokio, a work-stealing runtime :upside_down_face:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.