Repository navigation
fix(runtime): read pod exec output in chunks, not byte by byte - #549
Merged
Merged
Conversation
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]>
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
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Sentry flagged a 1.6 s primary-key lookup on an
mcppod (PALAD-AI-RAILS-45). The database was fine; the Puma process was starved. Agents were attaching files withboard_attach_asset, which copies the file out of the pod throughKubernetesRuntime#exec_via_websocket, and the websocket client underneath,websocket-client-simple, reads the socket withgetcand 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
mcpHPA (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 (connectblock,on,send,close,closed?,handshake). It reads withreadpartial(64 KiB)and hands whole chunks to thewebsocketgem'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_websocketuses it. The:messagehandler slices the channel byte off instead ofbytes/shift/pack.websocket-client-simpleand itsevent_emitterdependency are dropped;websocket(already in the lock) becomes a direct dependency. License and notice files are updated to match.Behaviour changes worth a look:
channel.k8s.iois 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.open?, which is false before the upgrade completes, so it never closed the connection, and the socket and its reader thread leaked.Verification
Tests.
test/support/fakes/fake_kubernetes_exec_server.rbis a real TCP/TLS listener speakingchannel.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.KubernetesRuntimereaches 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_file1 MB byte for byte,write_fileover stdin (v5, mode kept), stdout/stderr/exit 3, a drop before or after the Status, a silent API server, and an allocation bound.read_fileof 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.Real cluster. I ran one script against
developand against this branch, in-cluster on a local k8s v1.34 (service-account token, TLS, pod limit of 1 CPU):developread_file100 KBread_file1 MBread_file10 MBwrite_file1 MB round tripOn both branches sha256 matched for every file read and written back, exit codes / stderr / UTF-8 output were correct,
exec!on a deleted pod raisedContainerUnreachableError(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