Skip to content

Conversation

@xxdavid
Copy link
Contributor

@xxdavid xxdavid commented Jul 15, 2025

Since 5d8193e, the BroadcastDispatcher can send demand upstream even if its consumers did not request that many events. Because waiting is set to 0 when a new subscriber subscribes, the demand is repeated when the new subscriber asks for events.

I am fixing this by introducing a new counter in state - the number of requested but not yet delivered events. The dispatcher now does not send upstream demand if the demand can be satisfied by the requested events (and sends a lower demand if it can be satisfied partially).

Fixes #318.

Feel free to close this pull request if you come up with a better solution.

Since 5d8193e, the BroadcastDispatcher can send demand upstream even if
its consumers did not request that many events. Because `waiting` is set
to 0 when a new subscriber subscribes, the demand is repeated when the
new subscriber asks for events.

I am fixing this by introducing a new counter in state - the number of
requested but not yet delivered events. The dispatcher now does not send
upstream demand if the demand can be satisfied by the requested events
(and sends a lower demand if it can be satisfied partially).

Fixes elixir-lang#318.

{:ok, 10, disp} = D.ask(10, {pid2, ref2}, disp)
assert disp == {[{0, pid2, ref2, nil}], 10, expected_subscribers}
{:ok, 0, disp} = D.ask(10, {pid2, ref2}, disp)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that here (and also at line 155) I am reverting the IMO incorrect change in assertions in 5d8193e.

@josevalim josevalim merged commit c8814d9 into elixir-lang:main Jul 15, 2025
2 checks passed
@josevalim
Copy link
Member

Thank you, I believe this is indeed the correct fix and it was really well done. I will play with a couple more things locally and then do a new release

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate demand from BroadcastDispatcher

2 participants