Skip to content

Keep the iPhone connected over ace's relay: take full-size frames that arrive deflated - #2341

Open
Luseefor wants to merge 8 commits into
mainfrom
fix/1876-relay-drops
Open

Luseefor wants to merge 8 commits into
mainfrom
fix/1876-relay-drops

Conversation

@Luseefor

Copy link
Copy Markdown
Collaborator

Why the iPhone kept dropping over ace's relay

Every relayed channel from the phone ended a few seconds after its welcome. The phone's log said why, once per drop:

[app.ace.ios:relay] relay closed 1006 after 0.9s, secured: receive failed (NSPOSIXErrorDomain 40)

POSIX 40 is EMSGSIZE. RelaySocket set maximumMessageSize to exactly 65,535, the largest relay frame. Cloudflare's edge negotiates permessage-deflate with URLSession, and URLSession checks the limit against the message as it arrives, compressed. Encrypted frames don't compress, so a full 65,535-byte frame arrives a few bytes larger and the receive fails. Full frames appear as soon as a message is longer than one chunk, such as a long thread's snapshot. So each reconnect died while loading the thread, the backoff never reset, and long threads stayed on the skeleton.

I reproduced it on a Mac with a URLSession socket and a deflating peer:

  • limit 65,535, full frame deflated: NSPOSIXErrorDomain 40
  • limit 65,535, 65,000-byte frame: received
  • limit 65,535 + 4,096, every frame of a 1.1 MB message: received whole

On the daemon's side: 1000 Peer closed was the relay reporting that the phone went away. 1006 Socket closed was the same event, seen while the daemon was mid-send.

Changes

  • iPhone: RelaySocket takes up to 65,535 + 4,096 bytes per received message. The Noise layer still refuses any frame over 65,535 once it is read. This is the change confirmed live on the owner's phone: 0 drops in 8+ minutes, down from one every 1–5 s.
  • Relay framing: a test that a 1.1 MB message crosses the relay in frames of exactly 65,535 bytes, never more.
  • Close reasons on the Mac: after a drop, the client's next main hello carries lastClose: the code, its reason, whether it was ready, how long it had been up and down, and the attempt number. The daemon logs it as Client's last connection closed next to its own close record, so the next drop can be explained from ace.jsonl. A report that doesn't parse is dropped and the hello is still welcomed. Older daemons ignore the field.

Backoff needed no change. It resets once a connection stays up for healthyMs (reconnect-backoff.test.ts); it kept climbing only because no connection lasted that long.

Tests

  • apps/ios/Tests/RelayMessageLimitTests.swift: the daemon side seals a 1.1 MB message into full-size frames, a loopback WebSocket peer sends them permessage-deflated, and a task prepared by RelaySocket receives and reassembles them. With the old limit it fails with EMSGSIZE.
  • apps/relay/src/relay.process.test.ts: the frame bound.
  • packages/client/src/close-report.test.ts and apps/daemon/src/socket-close-log.server.process.test.ts: the report is sent, settled by a welcome, logged, and a malformed one is ignored.

Closes #1876

Luseefor and others added 8 commits November 19, 2026 18:15
Cloudflare's edge negotiates permessage-deflate with URLSession, which
checks maximumMessageSize against the compressed message. Encrypted
frames don't compress, so a full 65,535-byte Noise frame (every chunk of
a long thread's snapshot) arrived a few bytes over the limit and the
receive failed with EMSGSIZE (NSPOSIXErrorDomain 40), dropping the relay
channel within seconds of every welcome.

Give the socket 4 KiB of headroom; the Noise layer still refuses any
frame past 65,535 bytes once it is read.

Co-Authored-By: spade <bot@acehq.dev>
…5 bytes

The phone's relay socket sizes its receive limit from this bound.

Co-Authored-By: spade <bot@acehq.dev>
A hello may carry lastClose: the code, the client's own reason, whether
it was ready and for how long, and how long it was down. Through a relay
the daemon only learns "Peer closed", so this puts the far side's reason
in ace.jsonl. A report that doesn't parse is dropped, never the hello,
and older daemons ignore the field.

Co-Authored-By: spade <bot@acehq.dev>
The main socket's hello after a drop reports it, until a welcome settles
it.

Co-Authored-By: spade <bot@acehq.dev>
The generator can't state a .catch in JSON Schema, so the field says what
it reads and why a bad report never refuses the hello.

Co-Authored-By: spade <bot@acehq.dev>
The hello's lastClose carried the client's free-form reason, and the
daemon's log kept such text verbatim. It now carries a cause from a
fixed list (emsgsize, receive_failed, send_failed, handshake_timeout,
heartbeat, peer_closed, other) beside its numbers. The SDK reads a cause
from a missed heartbeat or a native socket's `relay:<cause>` reason, and
anything unknown is `other`; the daemon's schema strips any other field.

Co-Authored-By: spade <bot@acehq.dev>
…rts them

A receive the socket refused as too large closes as `1006 relay:emsgsize`
(also when the task ends before its receive callback says so); other
receive and send failures and a handshake timeout name their own cause.
Closes without a nameable cause keep the bare code.

Co-Authored-By: spade <bot@acehq.dev>

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.

iPhone over ace's hosted relay: connection drops every 10s–2min (1006) and long threads stay on the skeleton

1 participant