Skip to content

gateway: end a stream whose open is not recorded, and wait for its writer - #275

Merged
avi-papineni merged 2 commits into
mainfrom
gateway/stream-open-and-writer-exit
Nov 28, 2026
Merged

avi-papineni merged 2 commits into
mainfrom
gateway/stream-open-and-writer-exit

Conversation

@avi-papineni

Copy link
Copy Markdown
Contributor

Two defects in the sync handler (internal/gateway/sync.go), both about a stream's writes not matching the stream's life.

A stream whose open is not recorded is ended, not served (#264)

A replica records a stream's open, naming the stream on the workload's record, while it holds the workload; the offline mark is written only while the record names the ending stream (#109). When the open write failed, the handler logged it and served the stream: the stream's acknowledgments and heartbeats landed, but its offline mark found another stream's name, so the workload read as connected with no stream.

Now, when the open write fails, the handler:

  • takes the session out of the in-process registry (sessions.withdraw) while the workload is still held for the open, so no other stream of it registers in between, and a retirement watching the workload sees a stream leave;
  • writes the same conditioned offline mark any ending stream writes. Where the record names another stream, nothing is written and that stream's state stands (its own mark records its end, or it is still live on another replica). Where it names none, no stream is served and offline is what is true. Where the open landed though its write reported failure, the record names this stream and the mark undoes it. A workload the registry does not hold has no record and is not marked;
  • releases the hold, and ends the stream before anything is sent on it, before its HelloAck. The status is the Hello's for the same finding: the opaque refusal for a retired workload (ADR-0023), PermissionDenied for one the registry does not hold, and otherwise Unavailable with recording stream open, naming nothing of the store's error. A stream ended meanwhile through EndStream returns that status instead.

The agent treats a stream that ends before HelloAck as a failure: it redials with exponential backoff and full jitter, and since the stream was never established the attempt counter is not reset (internal/agent/sync/daemon.go, Run and runSession). Nothing in the agent changes.

The handler waits for its writer, and makes the snapshot stamp itself (#260)

The writer goroutine stamped last_snapshot_sent_at after each snapshot it sent, and the handler did not wait for it, while shutdown waits only for handlers before it closes the store.

A send blocked on flow control returns only once the stream is done: grpc-go's server SendMsg blocks in the stream's write quota until there is credit or the stream's context ends, and that context ends when the agent goes away, when Server.Stop closes the transports, or when the handler returns. An unbounded wait before return could therefore hang on a stream whose agent has stopped reading. So:

  • the writer touches no store. It reports each snapshot it sends on a one-slot channel, and the handler's own goroutine stamps it. Once the session has ended, the writer sends nothing more, so at most one send is in progress when the handler exits;
  • the handler's exit cancels the session, waits for the writer for at most five seconds (writerExitTimeout), and logs sync stream writer still in a send at the handler's exit; it ends with the stream at warning level if the bound is reached. That send then fails as the handler returns, and nothing it does touches the store;
  • it then stamps a snapshot sent before the stream ended, and only then takes the session out of the registry and writes the offline mark, so the stamp never lands after the mark, and the workload is held for the mark alone, not for the writer's wait or the stamp;
  • the stamp and the mark share one five-second bound (offlineWriteTimeout), the mark's bound before. After Stop every send returns at once, so a handler still returns within shutdown's five-second handler wait, and a stop still takes at most fifteen seconds; the compose stop_grace_period of twenty seconds is unchanged. Outside a stop, a handler whose writer is stuck in a send can take up to ten seconds to return.

Tests

  • stream_open_integration_test.go (Postgres): the open write fails through a registry wrapper. The stream ends with Unavailable and the store's text is not in it; the replica no longer holds it; the record reads as the previous stream's end left it; an open that landed but reported failure reads offline; a stream served by another replica keeps its synced state; the unknown and retired findings answer as the Hello does; and in each case the next stream opens and its end is recorded offline. A held snapshot stamp delays the stream's end, and the stamp lands before the mark.
  • writer_exit_test.go: over a stream whose sends block, the handler does not return while its writer is in a send and returns once the send does, and a send that never returns bounds the wait with the warning, after which the offline mark is written.

Docs

ARCHITECTURE.md §4.2 says a stream whose open cannot be recorded is not served, and that the handler makes every write a stream causes. docs/troubleshooting.md gains an entry for an agent stream that ends with recording stream open. CHANGELOG entries for both issues. ADR-0018 is unchanged: nothing in it says a stream is served whatever its open record, and its statement that the stored state describes the stream that registered last now holds when the open write fails.

No schema, migration, proto, sqlc, agent, REST, or console change.

Fixes #264.
Fixes #260.


Generated by Claude Code

Tests ahead of the fixes for #264 and #260, so they fail first.

A stream whose open record fails must end with Unavailable before its
HelloAck, naming nothing of the store's error, leave the registry and
the workload's hold, and leave the record truthful: the previous
stream's offline mark stands, an open that landed though it reported
failure is marked offline, and a stream another replica serves keeps
its state. The next stream opens at once and its end is recorded
offline. An open that finds the workload unknown or retired is answered
as the Hello is.

The handler must not return before its writer: a send in progress when
the stream ends is waited for, a send that cannot return while the
handler waits bounds the wait with a warning, and a snapshot stamp in
flight lands before the offline mark and before the stream ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Claude <noreply@anthropic.com>
…iter

A stream whose open record fails is no longer served (#264). Served,
its acknowledgments and heartbeats landed while the record named an
earlier stream, and its offline mark, written only while the record
names the ending stream, was skipped: the workload read as connected
with no stream. The session now leaves the registry while the workload
is still held for the open, the same conditioned mark any ending stream
writes records its end, the hold is released, and the stream ends
before its HelloAck: Unavailable for a store failure, naming nothing of
the error, so the agent redials with its backoff; PermissionDenied for
an unknown workload and the opaque refusal for a retired one, as the
Hello answers them.

The handler now waits for its writer (#260). The writer touches no
store: it reports each snapshot it sends and the handler stamps it, so
every write a stream makes is the handler's, and shutdown's wait for
handlers covers it. On its way out the handler waits at most five
seconds for the writer, since a send without flow-control credit
returns only once the stream is done, then stamps a snapshot sent
before the end, then records the end. The stamp and the mark share the
mark's five-second bound, so a stop still takes at most fifteen
seconds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Claude <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown

No ADR contradictions found (diff-anchored review).

Scope

The diff touches ARCHITECTURE.md, CHANGELOG.md, docs/troubleshooting.md, internal/gateway/sync.go, and two new test files (internal/gateway/stream_open_integration_test.go, internal/gateway/writer_exit_test.go). It does not touch docs/adr/ or CLAUDE.md, so this is a diff-anchored review against the ADRs the change bears on: ADR-0002 (streaming desired-state sync), ADR-0015 (agent wire contract), ADR-0018 (policy rendering and delta versioning), and ADR-0023 (workload retirement).

Findings

None of the two fixes contradicts an Accepted ADR's core decision, and none adds one that would need a superseding ADR.

  • Ending a stream whose open is not recorded (internal/gateway/sync.go, openFailed, withdraw). ADR-0018 decision 3 (as amended 2026-11-26) says the stored convergence state "describes the workload's stream that registered last" and that the offline mark "is written only while the stored identifier still names the ending stream, or names none." Nothing in the ADR's text requires serving a stream whose open write failed — the prior behavior (log and serve) was a gap between the ADR's described invariant and the code, not a decision the ADR made. The fix (s.openFailed) withdraws the session while the workload is still held for the open, writes the stream's end through the same conditioned MarkStreamOffline call every other stream end uses, and only then releases the hold — which is exactly the mechanism decision 3 describes for ordering competing writes. The PR's own description states this correctly: "ADR-0018 is unchanged: nothing in it says a stream is served whatever its open record." ARCHITECTURE.md §4.2 is amended in place (not an ADR, no supersession machinery needed) to state the invariant precisely. No contradiction.
  • Retired/unknown handling in openFailed. The refusal for a retired workload reuses the existing refuseRetired helper (internal/gateway/retire.go:29), which returns the one fixed Unauthenticated status ADR-0023 decision 3 requires ("Every refusal of a retired identity, at any door, reaches the agent as Unauthenticated with one fixed message that names no reason"). The unknown-workload case reuses the same PermissionDenied status the Hello path already returns elsewhere in sync.go, and skips the offline mark ("A workload the registry does not hold has no record and is not marked"), consistent with decision 3's scoping of refusals to registry-held workloads. No contradiction.
  • Writer/handler synchronization for last_snapshot_sent_at (internal/gateway/sync.go, awaitWriter, recordSnapshotSent). ADR-0018 decision 4 (as amended) ties the snapshot instant to "whatever caused it," written beside the persisted sync state — it does not mandate which goroutine performs the write, only that it is recorded. Moving the store write from the writer goroutine into the handler, and bounding the wait for the writer with writerExitTimeout, is an implementation change inside the gateway process; it doesn't touch ADR-0017's statelessness constraint (nothing durable is held in the replica between calls) and doesn't add polling (ADR-0002). No contradiction.
  • Agent side. No file under internal/agent/ is touched, and the PR description confirms the agent's backoff-and-redial behavior (ADR-0002, ADR-0015) is unchanged by a stream ending before HelloAck.

Consistency / dangling references

  • All ADR- citations added in this diff (ADR-0018 as amended, ADR-0023, ADR-0002, ADR-0015, ADR-0016 as amended) refer to existing, Accepted ADRs; none is dangling.
  • docs/troubleshooting.md's new entry and the CHANGELOG.md entries quote log strings (recording stream open, recording stream open; ending the stream so its agent redials) and status codes that match what internal/gateway/sync.go actually emits.
  • No comparison to a competing product or origin-story reference was introduced; vocabulary (workload, agent, control plane, sync stream) matches project terminology.

No superseding ADR is present, and none is needed.

@avi-papineni
avi-papineni merged commit 5d43f11 into main Nov 28, 2026
35 checks passed
@avi-papineni
avi-papineni deleted the gateway/stream-open-and-writer-exit branch November 28, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants