Repository navigation
fix: stop NetworkTransport heartbeats from dropping or mangling messages - #400
Conversation
|
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:
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! |
98c065c to
b70151a
Compare
|
Thanks @canarik, this is the right fix, and the tests (a read ending at every byte of the stream, For context on where this goes next: before 1.0.0 we plan to stop sending heartbeats from 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 Would you also be open to a backport to |
`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.
b70151a to
16faafe
Compare
f5d8830
into
modelcontextprotocol:rel/1.0.0
Motivation and Context
NetworkTransportsends 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.
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.
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.
Heartbeatuses one length constant for its wire size instead of repeated literals.How Has This Been Tested?
Extends
NetworkTransportTestswith a heartbeat in the same read as messages, reads that endat 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 testpasses.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.disabledon the peer that sends to it avoids that.Types of changes