Skip to content

feat(sandbox-relay): configurable keepalive times, and a dead-peer test - #866

Merged
webchang merged 3 commits into
rossoctl:mainfrom
webchang:pr/i439-relay-keepalive-env
Nov 17, 2026
Merged

webchang merged 3 commits into
rossoctl:mainfrom
webchang:pr/i439-relay-keepalive-env

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #439

Where main stands on each point of the issue

  • Server keepalive in newServer(): DONE on main, before this PR. relayServerOptions() in packages/sandbox-relay/src/main.ts sets grpc.keepalive_time_ms 20 s and grpc.keepalive_timeout_ms 10 s on both listeners (SBX0 §3.4), so a dead peer is already dropped in at most 30 s.
  • Ping policy vs the worker's 30 s client keepalive: no code change needed, and now pinned by a test. grpc-js 1.14's server never reads grpc.http2.min_ping_interval_without_data_ms, and reads grpc.keepalive_permit_without_calls only on a client. It has no server-side ping-rate policy, so it never sends GOAWAY too_many_pings, and setting those options on the relay would do nothing.
  • Transport test (a client that stops acking is torn down, presence removed, within the bound): STILL OPEN on main. This PR adds it.

What changed and why

  • relayKeepalive(env) reads MOCA_RELAY_KEEPALIVE_TIME_MS and MOCA_RELAY_KEEPALIVE_TIMEOUT_MS. Unset or empty keeps the default (20000 / 10000). Anything else must be an integer of milliseconds from 1000 up to below 2147483647, or the relay refuses to boot. This follows SH_SANDBOX_MAX_INFLIGHT: the error names the variable, never the value. The floor refuses a value meant as seconds (20 would ping every 20 ms), and its error names the unit. The upper bound matters because grpc-js treats 2^31-1 as "keepalive off", and Node timers overflow above it. Keepalive cannot be turned off this way. The explicit keepalive option of startRelay is not bound by the floor; that is how the tests use smaller values. startRelay reads the variables first, so a bad value fails the boot before anything binds or touches Redis. An explicit keepalive option still overrides the env.
  • Behavior: both variables are new in this PR. A value below 1000 ms (for example 20, meant as seconds) or otherwise malformed refuses the relay's boot, naming the variable and the unit. Unset keeps today's 20 s / 10 s, so a deployment that sets neither is unaffected.
  • The keepalive comment in main.ts now gives both reasons for keepalive (silent Execs behind idle-closing load balancers, and dead P4 hosts), and records the grpc-js ping-policy facts above.
  • About the proxies in front of the relay: the passthrough Route forwards the TLS bytes untouched. The ghostunnel sidecar is an L4 TLS proxy: it terminates TLS and forwards the plaintext bytes to the relay without parsing HTTP/2. Neither proxy answers a PING itself, so the relay's PINGs reach the worker, and only the worker acks them. If the host is dead, nothing acks the PING, just as on a direct connection. The relay then destroys its connection to the sidecar, and the sidecar closes its upstream. The sidecars set no idle timeout of their own.
  • docs/reference/configuration.md: two new rows in the Sandbox relay table.

Tests

New file packages/sandbox-relay/test/keepalive-dead-peer.transport.test.ts. It uses real grpc-js transports and a TCP proxy whose freeze() drops every byte in both directions while keeping the sockets open (no FIN or RST). From the relay, that looks like a host whose network dropped.

  • With keepalive 300 ms / 300 ms, the frozen worker's session is torn down within the bound: presence remove runs, and the same sandboxId attaches again.
  • Negative control, keepalive off: after the same window the session is still held, remove was never called, and a reconnect with the same id is refused (no second presence put). This is the issue's symptom.
  • A client pinging every 100 ms with keepalive_permit_without_calls stays READY for 2 s, and its Attach stream does not error. This is 300 times the rate of the Go worker. The relay's own keepalive is off in this test, so it sends no PING for the client to ack. Every client-to-relay write the proxy counts in the idle window is therefore the client's own PING; at least 5 are required. With the client's pings disabled, the test fails with 0 writes. If a later grpc-js starts enforcing a ping rate, this test fails.

Added to packages/sandbox-relay/test/keepalive.transport.test.ts:

  • relayKeepalive defaults, overrides and whitespace handling.
  • Refusal of 0, -1, 1.5, 1e4, 20s, 0x10, 012, off and 2147483647 for each variable.
  • Refusal of 1, 20 and 999 as below the 1 s floor, with an error that names the variable and the unit. 1000 is accepted.
  • A bad value refuses startRelay.
  • startRelay with only the env setting a 1000 ms keepalive pings an idle Attach connection. With the env unread, the 20 s default would send nothing in that window.

Before and after, with src/main.ts reverted to main:

  • On main: 21 of the first commit's 27 tests in the two files fail. relayKeepalive is not a function (20 tests). The boot test reports promise resolved ... instead of rejecting. The env-wiring test reports expected 0 to be greater than 0. The three dead-peer tests pass on main, because main already has the keepalive. They are regression cover for the issue's behavior, not the fix. The 8 floor tests added later call relayKeepalive, which does not exist on main, so they fail there too.
  • With the change: all 35 tests in the two files pass (27 before the floor tests were added; those 27 passed 5 runs in a row on a loaded machine).

Whole package: pnpm test in packages/sandbox-relay passes 26 files and 329 tests; pnpm typecheck is clean; python3 scripts/check-doc-links.py reports 0 broken links.

Out of scope

  • 🐛 Sandbox worker can't reconnect after a stalled connection #839, the worker's stalled-connection reconnect: there the connection is healthy, so server keepalive cannot detect it.
  • No grpc.http2.min_ping_interval_without_data_ms or grpc.keepalive_permit_without_calls on the server. grpc-js's server ignores both, and options that do nothing would mislead readers.

Every commit is signed off (DCO).

Assisted-By: Claude Code

…dead peer is dropped

The relay already pings every connection (20 s, 10 s timeout), but nothing
showed that this tears down an Attach whose host vanished without a TCP close,
nor that the servers accept the workers' own client pings.

- MOCA_RELAY_KEEPALIVE_TIME_MS and MOCA_RELAY_KEEPALIVE_TIMEOUT_MS set the
  two times; unset or empty is the default, anything but a positive integer
  below 2^31-1 refuses the boot. Documented in the configuration reference.
- keepalive-dead-peer.transport.test.ts freezes a worker's connection (every
  byte swallowed, no FIN or RST): the relay tears it down, removes presence
  and admits the same id again within keepalive time + timeout. With
  keepalive off the frozen session is still held and the reconnect refused.
  A client pinging every 100 ms stays READY: grpc-js's server has no
  ping-rate policy, so the workers' 30 s pings never draw GOAWAY
  too_many_pings.

Closes rossoctl#439

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
- The worker-pings test ran with the relay's keepalive on, so the client's
  acks of the relay's PINGs alone could meet its write count. The relay's
  keepalive is off there now: every client->relay write is a client PING
  (and with the client's pings disabled the test fails, 0 writes).
- The comment and the docs row said the PINGs cross ghostunnel inside the
  TLS stream. ghostunnel terminates TLS; it is an L4 TLS proxy that forwards
  the bytes without parsing HTTP/2, so it answers no PING and the worker
  acks the relay's PINGs itself. The passthrough Route forwards TLS bytes.
- MOCA_RELAY_KEEPALIVE_TIME_MS / _TIMEOUT_MS now refuse values below 1000,
  so `20` meant as seconds no longer pings every 20 ms. The error names the
  variable and the unit. startRelay's explicit keepalive option is not bound
  by the floor; the env-wiring test uses 1000.

Refs rossoctl#439

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from pdettori as a code owner November 17, 2026 19:05
@pdettori pdettori added the ready-for-ai-review Request automated AI code review from clawgenti label Nov 17, 2026

@ingpaolodettori-dev ingpaolodettori-dev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review at 5a53d05

Adds MOCA_RELAY_KEEPALIVE_TIME_MS / _TIMEOUT_MS (validated integer ms in [1000, 2^31-1), boot refusal otherwise), reads them in startRelay before anything binds or touches Redis, and adds a dead-peer transport test plus a no-too_many_pings test. Verified against grpc-js 1.14.7 (the lockfile version): KEEPALIVE_MAX_TIME_MS = ~(1 << 31) is the server's "off" value, grpc.keepalive_permit_without_calls is read only on the client transport, and there is no server ping-rate policy, so the comment's claims hold. The worker's 30 s/10 s client keepalive (dial.go), the duplicate-id refusal in relay.ts, and the 5m Route timeout also match the comments and docs. The validation regex rejects the octal, hex, exponent and fractional forms the tests list.

Author: webchang (MEMBER — maintainer)
Areas reviewed: TypeScript (sandbox-relay src + tests), Docs
Agent/IDE config (.claude/.vscode): none
Commits: 2 commits, all signed-off: yes
CI status: all checks passing at submit time

Body-only nit: deploy/vm/env/relay.env.example lists the relay's optional knobs as commented lines (e.g. #SH_SANDBOX_MAX_INFLIGHT=2). Consider adding #MOCA_RELAY_KEEPALIVE_TIME_MS=20000 and #MOCA_RELAY_KEEPALIVE_TIMEOUT_MS=10000 there so VM operators can find them.

Drafted by an AI reviewer and approved by a human before posting.

Comment thread packages/sandbox-relay/src/main.ts Outdated
/**
* MOCA_RELAY_KEEPALIVE_TIME_MS and MOCA_RELAY_KEEPALIVE_TIMEOUT_MS: unset or empty is the default
* (RELAY_KEEPALIVE), anything else must be an integer of milliseconds from MIN_KEEPALIVE_MS up, or
* the relay does not boot. Keepalive

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This docstring has a stray line break (Keepalive / cannot be turned off here), and it says "from MIN_KEEPALIVE_MS up" without the upper bound keepaliveMs enforces (below MAX_TIMER_MS, 2^31-1, which grpc-js reads as keepalive off). Suggest reflowing to e.g. "...must be an integer of milliseconds from MIN_KEEPALIVE_MS up to below MAX_TIMER_MS, or the relay does not boot. Keepalive cannot be turned off here: ..."

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in c28395e: reflowed, and it now gives the upper bound ("from MIN_KEEPALIVE_MS up to below MAX_TIMER_MS (which grpc-js reads as keepalive off)"). The body nit is in the same commit: relay.env.example lists both knobs as commented lines with their defaults and bounds, and since env-parity reads commented lines too, both are in its NOT_ENV_ON_K8S with the reason.

…he upper bound

Review follow-up. relay.env.example lists the relay's optional knobs as
commented lines, so VM operators find MOCA_RELAY_KEEPALIVE_TIME_MS and
_TIMEOUT_MS there too, with their defaults and bounds. env-parity reads
commented lines as well, so both are listed in its NOT_ENV_ON_K8S with
their reason (a code default; no manifest sets them). relayKeepalive's
docstring now gives the upper bound keepaliveMs enforces, on one line.

Refs rossoctl#439

Assisted-By: Claude Code
Signed-off-by: Rong Chang <[email protected]>
@webchang
webchang requested a review from cwiklik as a code owner November 17, 2026 20:00
@webchang
webchang merged commit b179579 into rossoctl:main Nov 17, 2026
17 checks passed
webchang added a commit to webchang/moca that referenced this pull request Nov 18, 2026
Brings 25 upstream merges since 1aa8dd6, among them the sbx OIDC
identity provider (rossoctl#851), the Redis schema-version key and config
bundle format marker (rossoctl#868), the provisioner Redis ACL user (rossoctl#869),
the relay keepalive env (rossoctl#866), the per-source OIDC pending bound with
the forwarded-headers Route pin (rossoctl#874), the egress refusal hints and
control-plane index (rossoctl#864), the empty-workspace sweep (rossoctl#872) and the
orphan-sweep log split (rossoctl#849). No conflicts.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
@pdettori pdettori added this to the v0.11.0 milestone Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-ai-review Request automated AI code review from clawgenti

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sandbox-relay: server keepalive, so a P4 host whose network drops is detected before the router's 5 m timeout

3 participants