Skip to content

fix: stop NetworkTransport heartbeats from dropping or mangling messages - #400

Merged
jeanmatthieu merged 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/network-heartbeat-framing
Nov 29, 2026
Merged

jeanmatthieu merged 1 commit into
modelcontextprotocol:rel/1.0.0from
canarik:oss/network-heartbeat-framing

Conversation

@canarik

@canarik canarik commented Nov 28, 2026

Copy link
Copy Markdown

Motivation and Context

NetworkTransport sends a heartbeat as 12 bytes (4 magic bytes and an 8-byte timestamp)
without a newline, and the receive loop decided whether bytes were a heartbeat by looking at
the first 4 bytes of each read. TCP does not keep the boundaries between sends, so with
heartbeats on (the default, every 15 seconds) a heartbeat and a message regularly arrive in
the same read. A read that started with a heartbeat was dropped together with the messages
after it, so a request could wait forever for its response. A heartbeat at the end of a read,
or one split across two reads, ended up in front of the next message, which then failed to
decode. The timestamp can contain 0x0A, which split such a heartbeat into a bogus message, and
a read that started at U+1F493 inside a message was dropped, because that character encodes
to the magic bytes.

  • Every read is appended to the buffer. A heartbeat is recognized only where a message would
    start (at the start of the stream, after a message or after another heartbeat) and is
    consumed by its fixed length, so a 0x0A in its timestamp cannot split it.
  • The 12 bytes count as a heartbeat only when the two high bytes of the timestamp are zero,
    as for every date before the year 10000. JSON text has no zero bytes, so a short line that
    starts with the magic bytes no longer takes the start of the next message with it.
  • Heartbeat uses one length constant for its wire size instead of repeated literals.

How Has This Been Tested?

Extends NetworkTransportTests with a heartbeat in the same read as messages, reads that end
at every byte of a stream of messages and heartbeats (including a timestamp that contains
0x0A and a message that contains U+1F493), one byte per read, short lines that start with the
magic bytes, bytes that only start like a heartbeat, and the heartbeat recognition helpers.
Without the fix, the first tests fail: a message is dropped and the next one arrives with the
heartbeat's bytes in front of it. swift test passes.

Breaking Changes

None in the API. The heartbeat bytes on the wire are unchanged, so peers on earlier releases
still interoperate, but a receiver on an earlier release still mishandles heartbeats; setting
HeartbeatConfiguration.disabled on the peer that sends to it avoids that.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

@canarik
canarik requested a review from a team as a code owner November 28, 2026 21:34
@jeanmatthieu

Copy link
Copy Markdown
Contributor

Thanks @canarik, this is a careful write-up. The tests that end a read at every byte of the stream, with a 0x0A in the timestamp and U+1F493 inside a message, show the problem clearly.

Before 1.0.0 we plan to remove the heartbeat from NetworkTransport entirely, rather than make it parse more carefully:

  1. It isn't part of MCP. The transport is otherwise newline-delimited JSON-RPC, and the heartbeat sends 12 raw bytes in the middle of that stream. Any peer that isn't this SDK, such as the TypeScript, Python or Go SDKs or a hand-written client over TCP, receives bytes it can't parse. This PR makes our own receiver robust, but the bytes still go out to everyone.
  2. MCP already has a liveness check. ping works the same way on every transport.
  3. It's on by default. Every connection pays for it, and, as the PR notes, receivers on earlier releases still mishandle it.
    Reconnection is going too. NetworkTransport reconnection calls connect() again on an NWConnection it has already cancelled, and a reconnect hides the new session from Client. Both features would be replaced by init(connection:maximumMessageLength:logger:). Until 1.0.0 is tagged, rel/1.0.0 can take that removal without a deprecation cycle.

So I'd rather not merge this into rel/1.0.0, where the code is about to go. We should keep the framing test though!

@canarik
canarik force-pushed the oss/network-heartbeat-framing branch from 98c065c to b70151a Compare November 29, 2026 06:27
@jeanmatthieu

Copy link
Copy Markdown
Contributor

Thanks @canarik, this is the right fix, and the tests (a read ending at every byte of the stream, 0x0A in the timestamp, U+1F493 inside a message) pin the problem down well.

For context on where this goes next: before 1.0.0 we plan to stop sending heartbeats from NetworkTransport and remove the public heartbeat configuration (HeartbeatConfiguration, heartbeatConfig:). The stream is otherwise newline-delimited JSON-RPC, so the 12 raw bytes reach any peer that isn't this SDK, and MCP already has ping for liveness.

The receive path in this PR stays, though. Peers on 0.12 send heartbeats by default every 15 seconds, so a 1.0 receiver keeps skipping them with exactly the recognition rule you wrote here: only where a message would start, consumed by its fixed length, and only with zero high bytes in the timestamp. That lets one side upgrade at a time. We'd drop that compatibility in 2.0.

So I'd like to merge this into rel/1.0.0 as is; the follow-up that removes the sending side builds on it and keeps your tests.

Would you also be open to a backport to main for a 0.12.x patch? Two 0.12 peers still hit the original bug with each other, and since this changes neither the API nor the wire format, it fits a patch release.

@jeanmatthieu jeanmatthieu left a comment •

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.

Let's rebase and address the CI issues first than we can merge

`NetworkTransport` sends a heartbeat as 12 bytes, 4 magic bytes and an
8-byte timestamp, with no newline, and `receiveLoop()` decided whether
bytes were a heartbeat by looking at the first 4 bytes of each read.
TCP keeps the bytes of a stream but not the boundaries between sends,
so a heartbeat and a message often arrive in the same read. A read
that started with a heartbeat was dropped together with the messages
after it, a heartbeat at the end of a read was prepended to the next
message, and a heartbeat split across two reads left some of its bytes
in front of the next message. The timestamp can also hold 0x0A, which
cut such a heartbeat in two, and a read that started at U+1F493 inside
a message was dropped, because that character encodes to the magic
bytes.
Fix: append every read to the buffer and recognize a heartbeat only
where a message would start, consuming it by its fixed length.

A line from the peer that starts with the magic bytes but is shorter
than a heartbeat would then be consumed together with the start of
the message after it.
Fix: take the 12 bytes for a heartbeat only when the two high bytes of
the timestamp are zero, which holds for every date before the year
10000 and never for JSON text.

The heartbeat bytes on the wire and the public `Heartbeat` API do not
change; `Heartbeat` uses one length constant instead of repeated
literals.

Adds `NetworkTransportTests` for a heartbeat in the same read as
messages, for reads that end at every byte of a stream of messages and
heartbeats, for a short line that starts with the magic bytes, for
bytes that only start like a heartbeat, and for the heartbeat
recognition helpers. Without the fix, the first three fail.
@canarik
canarik force-pushed the oss/network-heartbeat-framing branch from b70151a to 16faafe Compare November 29, 2026 15:42
@jeanmatthieu
jeanmatthieu merged commit f5d8830 into modelcontextprotocol:rel/1.0.0 Nov 29, 2026
14 checks passed
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.

2 participants