Skip to content

hub: fix race that drops sources subscribing at the same time - #97

Merged
maoueh merged 2 commits into
streamingfast:developfrom
YaroShkvorets:fix/hub-concurrent-subscribe
Nov 23, 2026
Merged

maoueh merged 2 commits into
streamingfast:developfrom
YaroShkvorets:fix/hub-concurrent-subscribe

Conversation

@YaroShkvorets

Copy link
Copy Markdown
Contributor

We've run into this issue where a consumer of a relayer stops receiving blocks after reconnecting, with no error on either side. The relayer is healthy and keeps serving its other clients; the affected one stays connected and silent until it is restarted.

What happens

ForkableHub.subscribe appends to h.subscribers from the CallWithBlocks* callbacks. Those hold the forkable's read lock, so sources created at the same time run subscribe concurrently and can overwrite each other's entry.

A source that loses its entry:

  • is sent the blocks the hub already had, since they are pushed before the append,
  • is never sent a live block, because broadcastBlock doesn't know it,
  • gets no error, so its stream stays open. On a relayer client the keepalive pings are still answered, so the client never reconnects.

How we saw it

One of two relayers went down. Its eight clients (tier1 and firehose) sent their block requests to the other relayer within 8 ms. Six carried on. Two received the initial blocks and then nothing, for hours, until they were restarted.

The same path serves every stream that joins a hub (SourceFromBlockNum, SourceFromCursor, SourceThroughCursor), so two requests arriving together on a firehose or tier1 can lose one the same way. We have not looked for that case in production.

Change

  • subscribers gets its own mutex, taken by subscribe, unsubscribe, broadcastBlock and the hub's shutdown.
  • subscribe still runs under the forkable's read lock, so no live block is broadcast between reading the initial blocks and registering the subscription.
  • No change to any exported API.

Tested

  • New TestForkableHub_ConcurrentSubscribe: 8 sources created at once, 200 rounds, each must receive the next live block. Without the fix it failed 10 runs out of 10, with the hub holding 4 to 7 subscribers instead of 8. With the fix it passes, also under -race.
  • go test ./... passes.

🤖 Made with Claude Code · 4c9654be-9023-454a-a3e6-4d57d0aa37f8

YaroShkvorets and others added 2 commits November 19, 2026 12:25
ForkableHub.subscribe appended to the list of subscribers from the
CallWithBlocks* callbacks, which only hold the forkable's read lock.
Sources created at the same time could overwrite each other's entry.
A lost one was sent the blocks the hub already had and then never a
live block, with no error, so its stream stayed open and silent.

The list now has its own mutex.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CKy2EkG5bfA6HNM1MZSHWz
Also drop the copy on append: appending past a snapshot's length does not
change what the snapshot holds.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CKy2EkG5bfA6HNM1MZSHWz
@YaroShkvorets
YaroShkvorets marked this pull request as ready for review November 19, 2026 17:33
@dfuse-bot
dfuse-bot requested a review from billettc November 19, 2026 17:34
@maoueh
maoueh merged commit 9969e93 into streamingfast:develop Nov 23, 2026
3 checks passed
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.

2 participants