Repository navigation
fix(hosts): bound host downlink backlog and propagate capacity rejection (fixes #364) #563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -411,6 +411,16 @@ function sendDown(link: Link, body: Unsequenced<DownMessage>, key: string): void | |
| )) throw new HostOpError(426, "host_upgrade_required", "Update this host before using its application protocol"); | ||
| const line = encodeQueued(body); | ||
| const size = Buffer.byteLength(line); | ||
| // The downlink backlog is bounded (spec D6): a command that would take the | ||
| // queued plus unacknowledged bytes past downlinkBacklogBytes is rejected | ||
| // before anything is recorded, so its caller hears it at once instead of | ||
| // at the op timeout, and it is never delivered later. Control messages | ||
| // (kill, interest, host_tools) are small and keep liveness and cleanup | ||
| // working on a saturated link; they bypass the cap. | ||
| if ((body.type === "req" || body.type === "stdin") | ||
| && link.outboxBytes + link.sender.queuedBytes + size > LINK_LIMITS.downlinkBacklogBytes) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reserve the encoded part overhead before accepting a command. If the socket is backpressured, queued commands can fill this budget exactly. When
|
||
| throw new LinkLimitError(`downlink backlog exceeds ${LINK_LIMITS.downlinkBacklogBytes} bytes`); | ||
| } | ||
| // Ahead of it: what is queued or unacked on this link (counters, no scan); fairness may send it before some of that. | ||
| const sample = { at: Date.now(), t: performance.now(), bytes: size, bytesAhead: link.outboxBytes + link.sender.queuedBytes, session: sessionOf(link, body) }; | ||
| link.sender.push(key, { line, size, sample }); | ||
|
|
@@ -2122,6 +2132,14 @@ export function getHostCapacity(hostId: string): CapacitySnapshot | null { | |
| return hub().links.get(hostId)?.capacity ?? null; | ||
| } | ||
|
|
||
| /** Queued plus unacknowledged downlink bytes on `hostId`'s link, or null | ||
| * when it has no link. Tests use it to place a backlog exactly on the cap. */ | ||
| export function getHostDownlinkBacklog(hostId: string): { queuedBytes: number; outboxBytes: number } | null { | ||
| const link = hub().links.get(hostId); | ||
| if (!link) return null; | ||
| return { queuedBytes: link.sender.queuedBytes, outboxBytes: link.outboxBytes }; | ||
| } | ||
|
|
||
| export function getHostLinkProtocol(hostId: string): number | null { | ||
| return hub().links.get(hostId)?.hostVersion ?? null; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -590,16 +590,21 @@ test("a request omp opens while the web server stops reaches the next server onc | |
| assert.equal(results().length, 1, "one agent tool result"); | ||
| }); | ||
|
|
||
| test("a daemon that stops reading is paced, not closed: the hub sends its outbox as the socket drains, and each line reaches omp once", { skip: process.platform === "win32" && "SIGSTOP is POSIX-only" }, async () => { | ||
| test("a daemon that stops reading is paced, not closed: the hub sends its outbox as the socket drains, each line reaches omp once, and a line past the cap is rejected, not queued", { skip: process.platform === "win32" && "SIGSTOP is POSIX-only" }, async () => { | ||
| const { session } = await startRpcSession("", "", projectDir, undefined, false, null, undefined, hub.getHost(hostId)); | ||
| assert.equal(sockets.size, 1); | ||
| const [socket] = sockets; | ||
| // 48 MiB of stdin: three times the hub's 16 MiB backpressure limit. | ||
| // 24 MiB of stdin: past the hub's 16 MiB backpressure limit, so pacing is | ||
| // exercised, but under the 32 MiB downlink backlog cap, so every line is admitted. | ||
| const count = 8; | ||
| const pad = "x".repeat(6 * 1024 * 1024); | ||
| const pad = "x".repeat(3 * 1024 * 1024); | ||
| daemon.kill("SIGSTOP"); | ||
| try { | ||
| for (let n = 1; n <= count; n++) session.proc.transport.write(`${JSON.stringify({ t: "cmd", id: n, op: "pad", args: { n, pad } })}\n`, assert.ifError); | ||
| // One more line would take the backlog past the 32 MiB cap (24 + 9 MiB): | ||
| // it is rejected at once with 413 and never reaches omp. | ||
| const over = await new Promise((resolve) => session.proc.transport.write(`${JSON.stringify({ t: "cmd", id: count + 1, op: "pad", args: { n: count + 1, pad: "x".repeat(9 * 1024 * 1024) } })}\n`, (error) => resolve(error))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make the backlog probe exceed the cap without exceeding the line limit. The 9 MiB probe is larger than No single line can exceed the cap on an empty link, because
Update the admitted-line count, the byte-size comments, and the assertion that every admitted line reaches
|
||
| assert.ok(over instanceof Error && over.status === 413 && over.code === "request_too_large", `expected a 413 rejection, got ${over}`); | ||
| // Room for a close at the limit to show up. | ||
| await sleep(300); | ||
| assert.deepEqual([...sockets], [socket], "the socket stays open while the daemon reads nothing"); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep channel cleanup available when the link is full.
If a transport holds an editing lease when another channel fills the link,
RemoteRpcTransport.close()now loses itsreq {op: "close"}to this guard. It has already removed the transport locally and swallows the rejection. The daemon therefore keeps the membership and editing lease, so competing links receivesession_busyeven after capacity returns. Repeatedclose()calls cannot recover becauseclosedis already true. Reserve capacity for cleanup requests or narrowly exempt them from ordinary-work admission. (raw.githubusercontent.com)