Skip to content

fix(runtime): read pod exec output in chunks, not byte by byte - #549

Merged
artempartos merged 3 commits into
developfrom
artempartos/makara
Oct 23, 2026
Merged

artempartos merged 3 commits into
developfrom
artempartos/makara

Conversation

@artempartos

Copy link
Copy Markdown
Contributor

Why

Sentry flagged a 1.6 s primary-key lookup on an mcp pod (PALAD-AI-RAILS-45). The database was fine; the Puma process was starved. Agents were attaching files with board_attach_asset, which copies the file out of the pod through KubernetesRuntime#exec_via_websocket, and the websocket client underneath, websocket-client-simple, reads the socket with getc and runs the frame parser on every byte. That is ~27 Ruby allocations per byte, all of it under the GVL, so every other request on the same process (health checks included) slowed down while it ran. Files over ~4 MB could not be read at all: the copy outlived the 30 s exec timeout.

The CPU burst also kicks the mcp HPA (separate infra PR), which brings up cold pods and makes it worse.

What

  • ContainerRuntime::ExecWebsocket: an app-owned exec websocket client with the same callback interface (connect block, on, send, close, closed?, handshake). It reads with readpartial(64 KiB) and hands whole chunks to the websocket gem's own handshake and frame parsers. It answers pings, treats a server close frame as a close, and reports a refused upgrade once.
  • exec_via_websocket uses it. The :message handler slices the channel byte off instead of bytes/shift/pack.
  • websocket-client-simple and its event_emitter dependency are dropped; websocket (already in the lock) becomes a direct dependency. License and notice files are updated to match.

Behaviour changes worth a look:

  • stdin goes out as binary frames. channel.k8s.io is a binary protocol, and the old client's text frames carried bytes that are not valid UTF-8. The API server tolerated it; a strict websocket parser rejects it.
  • A connection dropped without a close frame is an error (exit 1) right away, instead of the reader idling until the exec timeout. An error that arrives after the exit status no longer overrides the status.
  • An exec whose upgrade is never answered now closes its socket on timeout. The old code checked open?, which is false before the upgrade completes, so it never closed the connection, and the socket and its reader thread leaked.
  • TLS is unchanged: like the old client, the exec socket does not verify the API server certificate (kubeclient's REST calls do). Verifying it against the cluster CA is worth doing, but it is a separate change.

Verification

Tests. test/support/fakes/fake_kubernetes_exec_server.rb is a real TCP/TLS listener speaking channel.k8s.io, modelled on frames captured from a v1.34 API server: the 101, an empty stdout frame, output in frames of at most 32 KiB, the Status on channel 3, a close frame with code 1000, then the TCP close. KubernetesRuntime reaches it through its ordinary kubeconfig path; nothing vendor-owned is stubbed.

  • exec_websocket_test.rb: 1 MB byte for byte when the first frames share a read with the 101; frames split across reads; ping/pong; stdin with the channel-255 close; TLS; one error for a refused upgrade; EOF without a close frame; close from another thread.
  • kubernetes_runtime_test.rb: read_file 1 MB byte for byte, write_file over stdin (v5, mode kept), stdout/stderr/exit 3, a drop before or after the Status, a silent API server, and an allocation bound.
  • Run against the old code, the new runtime tests fail where they should: read_file of 1 MB allocates 28.4M objects against a bound of 100k; the never-answered exec leaks its connection; write_file's text frames are rejected as invalid UTF-8.
  • 20 consecutive runs, and 5 more with 8 CPU-burning processes in the same container: no flakes.

Real cluster. I ran one script against develop and against this branch, in-cluster on a local k8s v1.34 (service-account token, TLS, pod limit of 1 CPU):

develop this branch
read_file 100 KB 0.86 s, 3.1M allocations 0.03 s, 1.4k
read_file 1 MB 7.08 s CPU, 31.5M allocations 0.03 s, 4.2k
read_file 10 MB timed out (30 s), returned nil 0.07 s, 33k
10 ms sleep in another thread during the 1 MB read late by p50 84 ms / max 180 ms late by p50 0.6 ms
write_file 1 MB round trip 0.19 s 0.13 s

On both branches sha256 matched for every file read and written back, exit codes / stderr / UTF-8 output were correct, exec! on a deleted pod raised ContainerUnreachableError (404), and there were no leaked threads or file descriptors after 50 execs.

Locally I ran test/services/container_runtime, test/services/internal_tools, test/services/container_strategies, the container workflow integration test and the changelog test (748 runs, green), plus rubocop and brakeman (clean). The full suite is left to CI.

🤖 Generated with Claude Code

artempartos and others added 3 commits October 24, 2026 00:28
websocket-client-simple reads the exec socket with getc and re-runs the
frame parser on every byte. Copying a 1 MB file out of an agent pod cost
~30M allocations and 7-13 s of CPU under the GVL, so every other request
on the same Puma process stalled behind it (PALAD-AI-RAILS-45: a 1.6 s
primary-key lookup on an mcp pod), and a file over ~4 MB never finished
inside the 30 s exec timeout.

ContainerRuntime::ExecWebsocket replaces it with the same callback
interface over readpartial(64 KiB) and the websocket gem's own parsers.
On a real API server (k8s 1.34, in-cluster, 1 CPU) a 1 MB read_file
drops from 7.08 s / 31.5M allocations to 0.03 s / 4.2k, and 10 MB reads
in 0.07 s instead of timing out.

Also fixed on the way:
- stdin goes out as binary frames; text frames must be valid UTF-8
- an exec whose upgrade is never answered now closes its connection
  on timeout instead of leaking the socket and its reader thread
- a transport error after the exit status no longer overrides it

Signed-off-by: Artem Petrov <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…igate-sentry-issue-7808709096

* origin/develop:
  fix(admin): let a super admin sign out (#547)

# Conflicts:
#	CHANGELOG.md
@artempartos
artempartos merged commit c0cde6c into develop Oct 23, 2026
16 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.

1 participant