0.3.81: TCP keepalive + lastSeen-aware stale detection in addPeer dedup#6
Merged
Merged
Conversation
Mirrors @sym-bot/sym v0.5.3 — same shape on the Swift side so cross-runtime peers (sym-swift ↔ sym-node) recover symmetrically from peer restarts. Two-part fix: 1. SymPeerSession.tcpParametersWithKeepalive() helper builds NWParameters with NWProtocolTCP.Options.enableKeepalive=true, keepaliveIdle=1, keepaliveInterval=1, keepaliveCount=3. Used by both outbound NWConnection inits and the inbound NWListener parameters in SymDiscovery. Dead remote ends reaped in ~4s instead of waiting macOS default TCP_KEEPALIVE=7200s. 2. SymNode.addPeer dedup now treats a peer entry with lastSeen older than staleAfterSeconds (10s) as stale, and the new dial replaces it regardless of dual-dial tie-break or same-direction-duplicate logic. Remote re-dialling is itself evidence its prior is dead. Field problem this fixes: iPhone↔Mac-Catalyst MeloMove peer flap after either side rebuilds. Old behavior — peer restart leaves dead-but- ESTABLISHED socket on survivor; addPeer rejects live redial against zombie; flap continues until OS keepalive eventually reaps. New behavior: keepalive reaps within ~4s, AND lastSeen-stale check unblocks redial within 10s of last activity. Recovery is seconds, not hours. 71/71 unit tests pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Mirrors `@sym-bot/sym` v0.5.3 on the Swift side so cross-runtime peers (sym-swift ↔ sym-node) recover symmetrically from peer restarts.
Field problem
iPhone ↔ Mac Catalyst MeloMove pair flap after either side rebuilds. The pattern:
```
[SYM] session: handshake complete with Hongwei (019dd87d)
[SYM] peer: connected: Hongwei (outbound, bonjour)
[SYM] session: disconnected: Connection closed
[SYM] peer: disconnected: Hongwei
```
Repeated indefinitely. Cause: peer restart leaves a dead-but-ESTABLISHED TCP socket on the survivor. The `addPeer` dedup logic in v0.3.80 keeps rejecting the live new dial against the zombie entry. Without TCP keepalive, the OS holds the dead socket in ESTABLISHED state for ~2 hours (macOS default `TCP_KEEPALIVE = 7200s`), so the flap continues that long.
Fix
1. TCP keepalive on every NWConnection
`SymPeerSession.tcpParametersWithKeepalive()` helper builds `NWParameters` with `NWProtocolTCP.Options.enableKeepalive = true`, `keepaliveIdle = 1`, `keepaliveInterval = 1`, `keepaliveCount = 3`. Used by:
Dead remote ends now reaped in ~4 seconds instead of ~2 hours.
2. lastSeen-aware stale-prior detection in `addPeer` dedup
Before applying the dual-dial tie-break or same-direction-duplicate rule, check if `existing.lastSeen` is older than `staleAfterSeconds` (10s, matching Node SDK's `_heartbeatInterval`). If stale, replace prior with new — the remote re-dialling is itself evidence its prior is dead.
Tests
71/71 existing unit tests pass.
Test plan
🤖 Generated with Claude Code