Skip to content

AI-3506: one unreachable shard no longer stops sessions and bridges on the others - #2256

Merged
Loschcode merged 9 commits into
mainfrom
laurentschaffner/ai-3506-one-unreachable-shard-stops-new
Nov 24, 2026
Merged

Loschcode merged 9 commits into
mainfrom
laurentschaffner/ai-3506-one-unreachable-shard-stops-new

Conversation

@Loschcode

@Loschcode Loschcode commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Why

https://linear.app/stream/issue/AI-3506/one-unreachable-shard-stops-new-sessions-and-inbound-calls-on-every

On a deployment with peer shards, three paths asked every shard and failed when one did not answer, so one shard being down stopped work that belongs to the healthy ones.

What changed

  • Session create (session/manager.go): the id check (Sessions.Exists, plus the thread channel lookup) is skipped when Create generated the id itself. A UUIDv7 made a moment ago cannot be taken, and it can never start with thread-. A voice session with a generated id that holds its own call, named after that id, also skips the CallResources.Pin read, since no line can have been made for that call yet. A caller-chosen id is still checked, so it is still refused while a peer is down: that shard may hold the id.
  • CallBridges.Claim (store/call_bridge.go): the bridge is returned when any shard returned it, and each shard that did not answer is logged at warn (shard, error), at most once a minute per shard. Before this change, a bridge deleted on a healthy shard was thrown away with the error. The claim fails only when no shard returned the bridge and one did not answer. That failure is store.ErrShardUnanswered, and the plan fetch (answerPhoneCall) answers it with a 503, not a 404, so the vendor fetches again.
  • Inbound call hook (CallResources.Select): rows from any shard settle the question, and the shard that did not answer is logged at warn. A call the router placed on a healthy shard is now recognised as placed when a peer is down. When no shard has rows and one did not answer, the hook answers 503 (unavailable), as it already does for a join it could not record, so Stream delivers the event again. It does so only while the event is younger than 45 s (arrivingCallRetryFor, justified in a comment). An older one is dropped and logged as before, because a redelivery that lands after the shard recovers would send a worker to a call its caller has left. Nothing has been acted on by the time of the 503: the leg, the delivery record (acting) and the dispatch all come after it. A redelivery that arrives once every shard answers is therefore dispatched, and a test covers that.
  • Bounded peer reads (answeringShards): a read of another shard gets peerReadTimeout (2 s), except a bridge claim's, which gets claimReadTimeout (5 s). The claim's read deletes the bridge: a delete that commits on a peer just after the caller stops waiting spends the bridge, and the longer bound narrows that window to a peer that is effectively down. I chose the longer bound over letting the delete outlive the cap, because a late commit would lose the bridge either way. As soon as one shard's answer settles the read (rows found, or a bridge claimed), the remaining reads are cancelled, so a slow peer frees its pool slot (8 connections) at once. Only a shard that failed before the settle is logged, since one cut off by it is not known to be down. A down shard still fails the reads that do need it, and context.Canceled is never logged as a shard failure. A host that accepts a connection and never answers would otherwise hold the call for pgdriver's 10 s read timeout. everyShard is unchanged and keeps its fail-closed contract for list pages.
  • Not covered, for a decision (AI-3651):
    • Most common: an ordinary inbound call to a number has no call_resources row on any shard, so while any peer is down it is answered 503 for 45 s and then dropped. It is still not answered. Dispatching it means failing open, which is the open decision (commented on AI-3651).
    • answerWithAgent (callhooks.go, spec.ID = callID) opens the session of a number's agent under the call id that Stream rendered. Create sees that id as caller-chosen, so Exists (and Pin) still fail while a peer is down. Such a call still is not answered, even with the bounded reads and the 503. This needs a decision, not a skip: the id comes from Stream, not this router, so a session under it on the shard that is down cannot be ruled out.
    • deliverByPhone (scheduled_delivery.go) mints uuid.NewString(), places the call, and only then opens the session with that id. With a peer down, the user's phone rings and the session is refused. Campaign and outbound phone.Call sessions follow the same order. Fixing that means carrying a "router-minted" flag from api/phone into session.Spec. Pin also needs a different rule for these: it answers the newest row, so a result settled without the shard that is down could name the wrong Stream app.
    • anotherCustomersSession (api/phone.go) has the same Exists exposure.

No overlap with #2177: it edits OpenShards and Sessions.Upsert, and this PR edits neither.

Tests

The tests use a peer shard whose DSN points at 127.0.0.1:1, where nothing listens, or at a local listener that accepts connections and never answers.

  • store, in both StoreSuite and ShardedStoreSuite:
    • TestABridgeOnAShardThatAnswersIsClaimedWhileAnotherIsDown
    • TestABridgeNoAnsweringShardHoldsIsNotClaimedWhileAnotherIsDown (now ErrShardUnanswered)
    • TestACallPlacedOnAShardThatAnswersIsFoundWhileAnotherIsDown
    • TestACallNoAnsweringShardPlacedIsNotAnsweredWhileAnotherIsDown
    • TestACallPlacedOnAShardThatAnswersIsFoundWithoutWaitingOnOneThatDoesNot (silent listener)
    • TestABridgeNoShardHoldsIsRefusedOnceASilentShardRunsOutOfTime (silent listener; about 5.0 s)
    • TestOnlyAShardThatFailedBeforeTheReadSettledIsLogged (also covers the once-a-minute limit)
  • session, in ShardDownSuite:
    • TestASessionWithAnIdTheRouterGeneratesOpens
    • TestASessionWithAnIdTheCallerChoseIsNotOpened
    • TestAVoiceSessionWithAnIdTheRouterGeneratesOpensItsOwnCall (fails before the Pin fix with store: call pin: dial tcp 127.0.0.1:1: connect: connection refused)
  • api:
    • TestAnArrivingCallAShardThatIsDownMayHavePlacedIsDeliveredAgain: a young event gets a 503 from a node with a peer down and is not dispatched. The same delivery to a healthy node is dispatched.
    • TestAnOldArrivingCallAShardThatIsDownMayHavePlacedIsDropped: an event older than the window gets a 200, is not dispatched, and is logged.
    • TestAPlanFetchWhileAShardIsDownIsAnsweredForTheVendorToRetry: a 503 with a peer down, and a 404 with every shard answering.
  • Mutation check: putting back the old behaviour at each site fails the matching test (generated-id skip, Pin skip, early settle, peer timeout, hook 503, its age bound, plan fetch 503).
  • TestCallHooksSuite, TestCallStatusSuite, TestAppHooksSuite and TestLogAppSuite (api), and TestAppsSuite (phone), pass.
  • CI integration (api) on the first head failed only in tests that also fail on main (the three TestChannelsSuite cases, from AI-3575: keep internal error text from callers #2249's error text). It also failed in TestConnectorsSuite/TestAnAppNotMarkedInternalNeitherListsNorReadsNessy, where a fake connector leaked from a parallel test; that test passes locally.
  • Local full store runs hit failures from other test runs sharing the local Postgres: a SQL syntax error in TestWritingToAChannelReachesTheLastConversationOnIt and a SetupSuite deadlock. Both tests pass alone.
  • go vet and gofmt are clean.

🤖 Generated with Claude Code

Laurent Schaffner and others added 2 commits November 23, 2026 19:24
…others

A session with a generated id skips the cross-shard id check. A bridge
claim and a placed call's lookup settle on the shards that answered when
one of them returned the row, and log each shard that did not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

Thanks for asking Jeffery for review. I'll stay on this PR until I can approve it.

How it works

  • Findings come as inline comments, each starting with [Must Fix], [Should Fix], nit: or consider:.
  • Push fixes whenever you're ready. Once the branch has been quiet for 10 minutes I review the new head and resolve the threads it fixes. To skip the wait, comment /jeffery review.
  • If you disagree or have a question, reply in the thread. I'll answer there, and resolve it if you're right.
  • I approve once every must-fix and should-fix is closed and nobody has requested changes. CI isn't part of that; branch protection still holds the merge until it's green. From round 3 on, open should-fix findings stop blocking.
  • I don't approve release/* branches or changes to protected paths, and I'll say so when that applies.
  • If a must-fix is still open after round 8, I stop and hand the PR to a human. Draft PRs wait until they're marked ready.

Commands (each as a whole comment)

Command What it does
/jeffery review Review the current head now
/jeffery rereview Start over at round 1 on the current head and withdraw my approval. This redoes the whole review and costs as much as the first one, so use it only when you don't trust the earlier rounds. If nothing changed since my last round I say so instead; add force to redo it anyway
/jeffery pause / /jeffery resume Stop or restart automatic reviews on this PR
/jeffery status What I last reviewed, and why I have or haven't approved
/jeffery help This list

/review-agent or a mention of me works in place of /jeffery.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 1 on 8256c5e5e170 · 🆕 1 new

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved 8256c5e5e170 after 1 round · 1 dropped after discussion

Laurent Schaffner and others added 4 commits November 23, 2026 20:31
…ts lines

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…tles it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stream delivers it again, and nothing was acted on, so the delivery once
every shard answers is dispatched.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The bridge may be on the shard that did not answer, so the vendor is told
to fetch again rather than that the call is not waiting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 23, 2026 19:38

Withdrawing approval until 1cf18e8 is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 2 on 1cf18e8efb20 · 🆕 2 new

Comment thread acceleration/internal/store/shards.go Resolved
getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved 1cf18e8efb20 after 2 rounds · 1 dropped after discussion

Left open, your call:

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

/jeffery review

Nothing changed since round 2 on 1cf18e8efb20; comment /jeffery rereview force to redo the full review anyway.

A settled read returns without cancelling the other shards' reads, which
run on to their bound detached from the caller, so a shard that is down
is logged once it fails rather than cut off unseen.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Loschcode

Copy link
Copy Markdown
Contributor Author

Round 2: one point fixed (a late peer failure is now logged, ab6a0a0). The other is kept as a trade-off: a claim's delete cut off by the 2 s peer bound can still turn a retry into a 404, and bounding the read is what keeps a partitioned peer from failing every fetch. Both threads have the details.

@getstream-metis
getstream-metis Bot dismissed their stale review November 23, 2026 19:56

Withdrawing approval until ab6a0a0 is reviewed.

getstream-metis[bot]
getstream-metis Bot previously approved these changes Nov 23, 2026

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved ab6a0a0628fd after 3 rounds · 1 fixed · 2 dropped after discussion

Laurent Schaffner and others added 2 commits November 23, 2026 21:30
… a claim 5 s

A detached read held a peer pool connection for its whole bound. Only a
shard that failed before the settle is logged. A claim's delete on a peer
gets a longer bound, since one committed after the caller stopped waiting
spends the bridge.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… ringing

A call.session_started older than 45 s whose placement cannot be read is
dropped and logged, as before, rather than answered 503: a delivery that
lands once the shard is back would send a worker to a call nobody is on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@getstream-metis
getstream-metis Bot dismissed their stale review November 23, 2026 20:33

Withdrawing approval until 34692c4 is reviewed.

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Round 4 on 34692c43aeeb · 🆕 1 new
Still open: 1 should-fix. Full list on the checks tab.

// agent on it. While the caller may still be ringing, Stream is told to deliver it again,
// and nothing has been acted on yet. Past that, a delivery that lands once the shard is
// back would send a worker to a call its caller has left, so it is dropped as before.
if at := eventTime(event.CreatedAt); !at.IsZero() && time.Since(at) <= arrivingCallRetryFor {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

[Should Fix] Peer-shard outage answers 503 to every video call start
Issue: While a peer shard is down, every call.session_started in the app, video calls included, is answered 503 for 45 seconds after the call's created_at. Stream retries webhooks for calls no phone number reaches.
Cause: dispatchArrivingCall runs CallResources.Select at line 178, before the number lookup at line 203. The error branch at 184-188 returns 503. At ce41c39 a Select error was logged and answered 200.
Fix: Look up the number first and return the 503 only when a number reaches the call. Otherwise keep the log-and-200. Or: keep the order and accept the 503 on video calls, which costs retry traffic on calls that never involve telephony.

Evidence
  • callhooks.go:178 Select runs for every call.session_started
  • callhooks.go:184-188 a Select error within 45s of created_at returns false, which answers 503
  • callhooks.go:203-210 number lookup comes after Select
  • git show ce41c39: a Select error was logged and answered 200

@getstream-metis getstream-metis Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Approved 34692c43aeeb after 4 rounds · 1 fixed · 2 dropped after discussion

Left open, your call:

@Loschcode

Copy link
Copy Markdown
Contributor Author

/jeffery review

@getstream-metis

Copy link
Copy Markdown

/jeffery review

Nothing changed since round 4 on 34692c43aeeb; comment /jeffery rereview force to redo the full review anyway.

@Loschcode
Loschcode merged commit 1d8da03 into main Nov 24, 2026
17 of 20 checks passed
@Loschcode
Loschcode deleted the laurentschaffner/ai-3506-one-unreachable-shard-stops-new branch November 24, 2026 09:21
thesyncim added a commit that referenced this pull request Nov 24, 2026
…it cut off (#2379)

**Problem**
`CallBridges.Claim` and `CallResources.Select` read every shard at once
and return as soon as one shard's answer settles the read; the other
shards' reads are cancelled and not waited for (`answeringShards`,
#2256). `TestABridgeIsClaimedOnceFromAnyShardAndSweptOnceExpired` and
`TestCallResourceQueryBudgets` counted statements straight after the
call returned, so in the sharded suite the cut-off read was sometimes
not counted yet (1 for 2), and then landed in the next count (`Pin`, 3
for 2). Main's store job went red on it.

**Fix**
- The two budgets that wrap a settled read count with
`countSettledReads`, which waits, up to a deadline, until a statement
from every shard is in, then counts. No assertion changes: each still
expects one statement a shard.
- The code stays as it is. Waiting in `answeringShards` for the
cancelled reads was tried and brings back the stall #2256 removed:
pgdriver lets go of a read on a host that accepts and never answers only
at its deadline, not on cancel, and
`TestACallPlacedOnAShardThatAnswersIsFoundWithoutWaitingOnOneThatDoesNot`
failed with it (2.0s against a 2s bound).

**Tests**
- On main, `-count=30` of the two tests in the sharded suite failed in
at least five runs (the kept output does not hold the full count):
`Select` expected 2, got 1, followed by `Pin` expected 2, got 3, and
`Claim asks every shard once` expected 2, got 1. With the change,
`-count=30` of both tests in both store suites passed.
- A mutant that reads only this shard in `answeringShards` fails both
budgets ("a statement from every shard" never satisfied).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant