Skip to content

Pass channel sender along to prevent deadlocks - #2229

Open
soundofspace wants to merge 2 commits into
lycheeverse:masterfrom
soundofspace:pass-sender-along
Open

soundofspace wants to merge 2 commits into
lycheeverse:masterfrom
soundofspace:pass-sender-along

Conversation

@soundofspace

@soundofspace soundofspace commented Jun 3, 2026 •

Copy link
Copy Markdown

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

@soundofspace
soundofspace marked this pull request as draft June 3, 2026 18:57
@soundofspace

soundofspace commented Jun 3, 2026 •

Copy link
Copy Markdown
Author

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

@soundofspace
soundofspace marked this pull request as ready for review June 3, 2026 19:08

@katrinafyi katrinafyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@mre

mre commented Jul 5, 2026

Copy link
Copy Markdown
Member

Thanks, @soundofspace. Change looks good to me. Do you have any thoughts on @katrinafyi's comment above?

Comment thread lychee-bin/src/commands/check.rs Resolved
@soundofspace

Copy link
Copy Markdown
Author

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:

lifetime

A single channel that tracks liveness and handles recursion

ReceiverStream::new(recursive_channel_recv)

vs
a channel that lives forever but is closed by an external future, which is also backed by another sender/receiver pair internally

ReceiverStream::new(recursive_channel_recv).take_until(waiter.wait())

splitting responsibilities

A channel that is responsible for liveness and for recursion

  • recursive_channel_recv / RequestQueue which does both

vs
two independant parts

  • WaitGuard that tracks liveness
  • recursive_channel_recv for recursion

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 katrinafyi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Comment thread lychee-bin/src/commands/check.rs Resolved
Comment thread lychee-bin/src/commands/check.rs Resolved
Comment thread lychee-bin/src/commands/check.rs Resolved
@katrinafyi

Copy link
Copy Markdown
Member

Updating with main should fix these CI issues.

@soundofspace

Copy link
Copy Markdown
Author

I have a few minor changes suggested which I can easily push to the branch if you don't mind, just let me know!

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!

@katrinafyi

Copy link
Copy Markdown
Member

Awesome! Looks good to me now, I'll let @mre do the final look and merge.

@mre

mre commented Sep 4, 2026

Copy link
Copy Markdown
Member

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?

@mre

mre commented Sep 4, 2026

Copy link
Copy Markdown
Member

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, enqueue().await can block while for_each is still processing that response's child vector. Because the receiver is upstream in the same future, it cannot be polled to free capacity. A page with more than max_concurrency discovered links is quite a realistic scenario.

I'd like to note that this structure predates the PR, and recursive_uris is currently hardcoded to vec![], so it is not a new regression. However, the PR should not be interpreted as demonstrating that channels now prevent all recursion-related deadlocks. Before we can enable recursion, we should add a test for that case and ensure that it passes. I'm wondering if we should add a (disabled) test for that case before merging this PR, although it will likely fail or require some refactoring to pass.

And there is a chance that I'm missing something and that it's all fine, sorry.

@katrinafyi

Copy link
Copy Markdown
Member

@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.

@mre

mre commented Sep 20, 2026

Copy link
Copy Markdown
Member

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?

@katrinafyi

Copy link
Copy Markdown
Member

I can make some committs which fix the bounded recursion issue later this week. It should be very small :)

@katrinafyi

Copy link
Copy Markdown
Member

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 max_concurrency links per file, but it is not testing anything meaningful unless recursion is implemented.

Do you think it should go in this PR or a new one on top of this PR?

@mre

mre commented Sep 28, 2026

Copy link
Copy Markdown
Member

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. 😅

@katrinafyi

Copy link
Copy Markdown
Member

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 😌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants