Skip to content

Add an idle timeout to GrpcStreamBroadcaster and stop abandoned streams - #281

Draft
shsms wants to merge 4 commits into
frequenz-floss:v0.x.xfrom
shsms:stream-idle-timeout
Draft

shsms wants to merge 4 commits into
frequenz-floss:v0.x.xfrom
shsms:stream-idle-timeout

Conversation

@shsms

@shsms shsms commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

A GrpcStreamBroadcaster whose server stream went silent, or whose receivers were all dropped, kept its call open and could retry forever. This branch adds an opt-in idle timeout, stops a stream once every receiver it handed out is gone, and starts it again for the next receiver.

Changes

  • The retry count resets when a message arrives, so a retry_strategy limit counts the retries since the last message.
  • idle_timeout (opt-in): a call that receives no message for that long is cancelled and retried like a failed call, with a StreamIdleTimeoutError.
  • A refactor moves the per-run state into a private _Run, with no behavior change, so one run can stop while a new one starts.
  • Four fixes, one commit each:
    • receivers end on an unexpected error or when the call is cancelled from outside;
    • stop() passes on its caller's cancellation;
    • a late receiver with events ends at once;
    • stop() and event-loop shutdown no longer hang on a call that turns its cancellation into a gRPC error or into the end of the stream. Another error raised while the run is being cancelled is logged as a warning, not sent as a StreamFatalError.
  • Receiver tracking: new_receiver() returns a wrapper. When every receiver is closed or garbage-collected, the run is cancelled, also while it is stalled or retrying. A broadcaster that never handed out a receiver keeps running.
  • Restart: a later new_receiver() starts a new run with a fresh copy of retry_strategy, unless stop() was called or the stream ended by itself.
  • Where to look:
    • GrpcStreamBroadcaster.new_receiver(): it counts the receiver before creating it, sets handed_out, and restarts the run.
    • _ReceiverCounter and _TrackedReceiver.
    • The tests pin garbage collection while a receiver is created, receivers dropped from another thread, and the cancellation races.

Breaking

  • new_receiver() now returns a wrapper Receiver: len() and Merger.stop() no longer work.
  • The stream stops once every receiver it handed out is closed or garbage-collected, and is_running is then False. Keep a receiver referenced while you read from it.
  • The retry count resets on every message, so a retry_strategy limit no longer caps the total number of retries.
  • StreamFatalError.exception can now be an error that is not a gRPC error.

@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:code Affects the code in general labels Oct 20, 2026
@shsms
shsms force-pushed the stream-idle-timeout branch from da79257 to 1604390 Compare October 21, 2026 15:04
shsms added 4 commits November 3, 2026 11:29
The retry count of GrpcStreamBroadcaster was never reset, so a retry
limit counted every retry over the life of the stream. A long-running
stream with a limit gave up after that many lost connections, even when
it had recovered from each one.

Reset the count when a message arrives, so a limit counts the retries
since the last message.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
A server stream that stays open without sending blocked its consumers
forever. Cancel such a call and retry it like a failed one.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Abandoned streams kept retrying forever, so abandoned broadcasters piled
up calls during outages. Track the receivers handed out and stop once
all are closed or dropped, also while stalled or retrying.

Also end the receivers on any exit of the stream: an unexpected error
left them waiting forever. Now they end, and receivers with events get a
StreamFatalError. And let stop() pass on a cancellation of its caller,
which it used to swallow.

A receiver with events requested after the stream stopped now ends at
once too. If no receiver with events existed before, it used to wait on
a new event channel that was never closed.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Some clients reuse a cached broadcaster without checking is_running, so
a stream stopped for lack of receivers would hand them a receiver that
ends without data. Start a new run instead. Each run already closes only
its own channels, so the old run winding down cannot end the new run's
receivers.

A stream that ended, gave up or failed by itself is not restarted.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@shsms
shsms force-pushed the stream-idle-timeout branch from 1604390 to cacddab Compare November 3, 2026 10:30
@shsms shsms changed the title Add an idle timeout to GrpcStreamBroadcaster and stop it with its receivers Add an idle timeout to GrpcStreamBroadcaster and stop abandoned streams Nov 13, 2026

This branch has not been deployed

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

Labels

part:code Affects the code in general part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant