Skip to content

fix(wire): ignore topic frames from an attachment the client already left - #3447

Open
yashgix wants to merge 2 commits into
generalaction:mainfrom
yashgix:fix/wire-ignore-stale-topic-frames
Open

yashgix wants to merge 2 commits into
generalaction:mainfrom
yashgix:fix/wire-ignore-stale-topic-frames

Conversation

@yashgix

@yashgix yashgix commented Nov 25, 2026 •

Copy link
Copy Markdown

Description

update, topic-gap and topic-error frames carry only the topic name. If a viewer closes a topic and a new viewer attaches to the same topic while frames for the old attachment are still in flight, the client applies those frames to the new attachment. In the tests this shows up as one extra snapshot resync for a stale update or topic-gap, and as a reopened ReplicaLog that stops receiving output after a stale non-retrying topic-error.

The client looks the entry up by topic alone (connect.ts), and the server never says which attachment a frame belongs to.

Change

  • protocol.ts: the three frames get an optional attachId, validated in isWireMessage.
  • serve.ts: the server stamps each frame with the attach id of the attachment that produced it, and returns that id in the attach result (it was undefined).
  • connect.ts: the connection remembers, per topic, the attach ids it left and drops a frame stamped with one of them. That covers its own attach id and, for a shared attachment, the id the server reported in the attach result, including the late result of an attach that was cancelled.

Topic sharing is unchanged. Two connections on one transport still share one server attachment, a frame stamped with the other connection's id is delivered, and a detach still removes the shared attachment as before. Frames without attachId behave as they do today, and clients ignore a missing or non-string attach result, so old and new peers still work together. An old server stamps nothing, so it gets no protection.

Limits

This does not give complete stale-frame protection. A frame that arrives before the late result of a cancelled attach is not recognised, because the client has not learned the shared id yet. The remembered ids are also bounded, because nothing in the protocol bounds how long a queued frame can wait:

  • 32 ids per topic. A frame delayed past 32 further attach and detach cycles of the same topic on the same connection is not recognised.
  • 4096 topics per connection. After that, the ids of the least recently changed topic are evicted, and a stale frame for that topic arriving afterwards is not recognised.

A first version kept one set of 256 ids per connection, and a test that leaves 300 other topics showed that activity on other topics could evict an id. Retention is now per topic. Removing the bound would need an acknowledgement for detach, which is a new frame type.

Related issues

Fixes #3446. It is separate from #3426.

Testing

  • 18 new tests in packages/wire/src/api/stale-topic-frames.test.ts run the real serve, connect, LiveLogSource and ReplicaLog over an in-memory transport whose server-to-client direction can be held and released in order. They cover each stale frame kind, rapid reopen cycles, reconnects, two connections sharing a topic (including a held frame stamped with the shared attachment's id that must be dropped after one connection reopens), a shared attach cancelled before its result arrives, and the two delay boundaries (300 other topics, 20 reopen cycles).
  • @emdash/wire: 371 tests pass. On unchanged main, 13 of the 18 new tests fail. The other 5 are guards that pass on both: legacy unstamped frames, two connections sharing a topic, and three detach-ordering cases.
  • tsgo --noEmit, oxlint and oxfmt are clean for packages/wire.
  • The patch applies to current main with git apply, and the branch merges cleanly into fix: stop remote reconnect loop on busy hosts and slow links (health probe congestion + resync convergence) #3444, which adds an optional onActivity hook next to WireTransport in protocol.ts.

…left

update, topic-gap and topic-error frames carry only the topic, so a frame still in
flight for a closed attachment was applied to a newer attachment of the same topic.
The server now stamps the attach id on these frames and returns it in the attach
result. The client remembers, per topic, the ids it left (its own and the shared
attachment's) and drops frames stamped with one of them. Frames from a shared
attachment, and frames without an id, behave as before.
@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change affects clients and servers that exchange live-topic frames.

Fix the canceled shared-attach case before merging; a delayed error can still stop a reopened viewer.

Findings

  1. P1 Old errors stop reopened viewers ▶
Fix with agent prompt
### Issue 1
packages/wire/src/api/connect.ts:575-576
Canceling a shared attach before its result arrives leaves `serverAttachId` undefined here. For example, connection A attaches, then connection B requests the same topic. The server queues B’s result containing A’s id, followed by a non-retrying `topic-error`. If B cancels and reopens before those messages arrive, it remembers only its own request id and ignores the canceled request’s result. The old error then passes `isAbandoned` and deletes B’s new attachment, stopping its updates.

Preserve the shared id from a canceled attach result, or otherwise reject these old frames before delivering them. Add a regression test that cancels the shared attach before its result arrives.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Adds optional attachId stamps to topic frames and remembers recently left attachments so delayed frames can be ignored.

  • Live topic frames stay tied to the attachment that sent them.

Diagram

sequenceDiagram
  participant B as Connection B
  participant S as Server
  S->>S: Topic already attached by A
  B->>S: Attach shared topic
  S-->>B: Shared result with A's id (delayed)
  S-->>B: Old topic-error with A's id (delayed)
  B->>S: Cancel and detach
  B->>S: Reopen topic
  Note over B: Only B's old request id was remembered
  S-->>B: Deliver delayed shared result
  Note over B: Canceled result is ignored
  S-->>B: Deliver delayed topic-error
  Note over B: New attachment is deleted
Loading

Reviews (1) · Last reviewed commit: "fix(wire): ignore topic frames from an a..." · Reviewed by Greptile

Comment on lines +575 to +576
abandon(topic, current.attachId);
abandon(topic, current.serverAttachId);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P1 Old errors stop reopened viewers

Canceling a shared attach before its result arrives leaves serverAttachId undefined here. For example, connection A attaches, then connection B requests the same topic. The server queues B’s result containing A’s id, followed by a non-retrying topic-error. If B cancels and reopens before those messages arrive, it remembers only its own request id and ignores the canceled request’s result. The old error then passes isAbandoned and deletes B’s new attachment, stopping its updates.

Preserve the shared id from a canceled attach result, or otherwise reject these old frames before delivering them. Add a regression test that cancels the shared attach before its result arrives.

Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/wire/src/api/connect.ts
Line: 575-576

Comment:
**Old errors stop reopened viewers**

Canceling a shared attach before its result arrives leaves `serverAttachId` undefined here. For example, connection A attaches, then connection B requests the same topic. The server queues B’s result containing A’s id, followed by a non-retrying `topic-error`. If B cancels and reopens before those messages arrive, it remembers only its own request id and ignores the canceled request’s result. The old error then passes `isAbandoned` and deletes B’s new attachment, stopping its updates.

Preserve the shared id from a canceled attach result, or otherwise reject these old frames before delivering them. Add a regression test that cancels the shared attach before its result arrives.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Good catch! I reproduced this with a regression test. The client now retains the shared attachment ID from a late attach result, even when the original request has been cancelled. The new test passes, and all 371 wire tests pass.

A stale frame arriving before the attach result remains a limitation, which I've documented in the PR.

A shared attach that is cancelled before its result arrives never told the client
the id of the attachment the server shared with it. The result is now read when it
arrives late, so a frame stamped with that id is dropped after the topic is reopened.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

wire: stale topic frames can affect reopened attachments

1 participant