Repository navigation
Pass channel sender along to prevent deadlocks - #2229
soundofspace wants to merge 2 commits into
Conversation
dcfc4a3 to
971ecd7
Compare
|
This is PR is not ready for a full merge and could use some more cleanup / polishing and nicer UX. But I want to already request a review just to talk about this design in general. It also seems tests are flacky in github ci? Doing the exact same tests locally work without any issue. Also this only address this specific issue, it does not attempt to solve or handle any of the other difficulties related to recursing |
katrinafyi
left a comment
There was a problem hiding this comment.
I'm happy with this in principle, thanks for the advice!
Re the pending structs, it would be nice if they could be combined into one generic struct somehow, but this is hard since the sender needs to take a (pending) request. I guess this is one thing that would've been easier with WaitGuard, since it doesn't have the type parameter. Do you have thoughts?
I feel like passing the sender around is also somewhat less semantic. It's harder to know at a glance why it's being propagated so much and it gives the owner more power (anyone could send more requests at any time).
Re using Option to reduce the size of the struct, I don't think it matters too much at all.
|
Thanks, @soundofspace. Change looks good to me. Do you have any thoughts on @katrinafyi's comment above? |
251480b to
d0605ea
Compare
|
Hey, sorry I lost track of this! I updated code to incorporate changes from latest main branch. As can be seen: passing a waitguard or a typed RequestQueue (could be the sender directly, but feels like this deserves a newtype) is almost exactly the same. At this point it's mostly a trade off between these two: lifetimeA single channel that tracks liveness and handles recursion ReceiverStream::new(recursive_channel_recv)vs ReceiverStream::new(recursive_channel_recv).take_until(waiter.wait())splitting responsibilitiesA channel that is responsible for liveness and for recursion
vs
I personally would argue that a single channel that does both here, makes more sense, since that maps quite nicely to how people expect channels to work in rust (and waitgroup is basically another sender/receiver pair). But I can also see why someone would want to split this. So I'm leaving this PR here mostly to showcase that this is possible and how it would work. |
katrinafyi
left a comment
There was a problem hiding this comment.
Happy with this! I now agree that this is a better way to combine the responsibilities.
Thanks again for the insight. At the time, using senders in channels for recursion didn't occur to me, but it is quite obvious in hindsight :)
I have a few minor changes suggested which I can easily push to the branch if you don't mind, just let me know!
|
Updating with main should fix these CI issues. |
b330c59 to
091850c
Compare
I added all the feedback you left, since I wanted to do a small refactor around the queue clone. If you have any other changes you want to push, definitely go for it, no need for an extra ping pong here. And thanks for looking at this! |
|
Awesome! Looks good to me now, I'll let @mre do the final look and merge. |
|
Hey, sorry for dropping the ball on this one. I have one more question before merging: is this PR intended strictly as a liveness simplification before recursion work lands, or as the chosen foundation for recursive fan-out? I think it's the latter, in which case I would require a test where one response creates more children than the channel capacity before relying on this architecture. Could you add one? |
|
To elaborate a bit on what I mean, I believe bounded recursive fan-out can still deadlock. Even with the new design, the data flow remains a bounded cyclic pipeline:
If one response eventually produces more children than the channel’s available capacity, I'd like to note that this structure predates the PR, and And there is a chance that I'm missing something and that it's all fine, sorry. |
|
@mre I think your worry is correct. I think the recursive channel might just have to be unbounded, I don't see another way to avoid that deadlock. A test would definitely be good. |
|
So I'm a bit confused and not sure how to make progress here. @soundofspace, did you find the time to look into the comments? In the meantime, there's a new PR at #2283, which looks like it aims to address the issue, but I have not looked into that yet. Any thoughts on how to continue? |
|
I can make some committs which fix the bounded recursion issue later this week. It should be very small :) |
|
I've made the change and added a test case at 091850c...rsc-s:lychee:pass-sender-unbound. The test case sets up a file structure with more than Do you think it should go in this PR or a new one on top of this PR? |
It can be a new PR as long as we're certain that the tests pass before merging this work. It's kind of a big change, so I'm a bit worried we might miss something, which could cause a regression or a move into the wrong direction. Hope that makes sense. 😅 |
|
Very fair! I've opened #2312 so we can check the CI tests on the 2 PRs when combined. When that succeeds, we can merge both one after the other 😌 |
Hi, I just read both blog posts about "Five Years of Trying to Add Recursion to lychee". Thanks for those they were an interesting read.
The one thing I don't agree with is "Channels are the wrong tool for cyclic pipelines". They have served me really well, but the points you made about how they work in rust are really valid. So they could be the right tool if they are used in a slightly different way.
The easiest solution to make everything work with channels is not to store a single recursive/global sender, but to actually pass it along with inputs/requests/responses. By doing this the feedback loop's lifecycle is now tied to the actual work. This prevents deadlocks and effectively implements what is being doing here with a waitgroup and a guard. The guard here basically becomes the sender and works almost essentially the same: passing it along and relying on drop to signal work is finished