Repository navigation
Conversation
…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.
[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
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.SummaryAdds optional
DiagramsequenceDiagram
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
Reviews (1) · Last reviewed commit: "fix(wire): ignore topic frames from an a..." · Reviewed by Greptile |
| abandon(topic, current.attachId); | ||
| abandon(topic, current.serverAttachId); |
There was a problem hiding this 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.
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.There was a problem hiding this comment.
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.
Description
update,topic-gapandtopic-errorframes 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 staleupdateortopic-gap, and as a reopenedReplicaLogthat stops receiving output after a stale non-retryingtopic-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 optionalattachId, validated inisWireMessage.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 wasundefined).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
attachIdbehave 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:
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
packages/wire/src/api/stale-topic-frames.test.tsrun the realserve,connect,LiveLogSourceandReplicaLogover 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 unchangedmain, 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,oxlintandoxfmtare clean forpackages/wire.mainwithgit 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 optionalonActivityhook next toWireTransportinprotocol.ts.