Skip to content

fix(sandbox): close the client when the idle L7 upstream closes - #5

Open
kosaku-sim wants to merge 2 commits into
fix/basic-auth-credential-resolvefrom
fix/l7-upstream-idle-close
Open

kosaku-sim wants to merge 2 commits into
fix/basic-auth-credential-resolvefrom
fix/l7-upstream-idle-close

Conversation

@kosaku-sim

Copy link
Copy Markdown
Member

概要

L7 relay(relay_rest と relay_passthrough_with_credentials)は、クライアントの次のリクエストだけを待ち、その間に upstream が接続を閉じても気づかなかった。upstream が keep-alive の idle 接続を閉じても、proxy はクライアント側を開いたままにする。クライアントはその接続をまだ使えると見て次のリクエストを送り、proxy は応答を返さずに切断していた。

次のリクエストを待つ間は upstream も同時に読み、EOF・エラー・予期しないデータのどれかが来たら relay を終える。これでクライアント側も閉じる。クライアントはその接続を pool から外して新しく張り直すので、proxy を通さないときと同じ動きになる。

きっかけ

AutoDev の受入テストの証跡を gh pr edit --attach(42 件)で PR 本文に添付すると、Post "https://api.github.com/graphql": EOF で失敗していた。gh は最初に api.github.com へ接続して PR を読み、添付を uploads.github.com へ upload(約 65 秒)したあと、最初の接続を使い回して本文を更新する。api.github.com は idle 30 秒で接続を閉じる。proxy がそれをクライアントに伝えないので、最後の更新が応答なしで切られていた。Go の net/http は POST を送り直さない。

実測(sandbox から proxy 経由、api.github.com へ GET のあと N 秒空けて 2 本目を送る):

空き時間 修正前 修正後
20 秒 同じ接続で 200 同じ接続で 200
31 秒以上 2 本目を送った瞬間に応答なしで切断 30.1 秒で FIN が届く(GitHub の idle close がそのまま伝わる)

proxy を通さずに直接つなぐと、GitHub は 30.0 秒で FIN を送り、Go は次のリクエストを新しい接続で送る(reused=false)。

upstream(NVIDIA/OpenShell main、2026-11-05 時点)でも、この 2 つのループは同じ作りのまま。似た症状の NVIDIA#3724(NVIDIA#3772 で修正)は forward service の経路が対象で、egress の L7 relay は含まない。

変更内容

  • next_request_unless_upstream_closed: tokio::select! で parse_request(client) と upstream の 1 byte read を同時に待つ。upstream 側が先に終われば None を返す。どちらの read も cancel safe(AsyncReadExt::read / read_u8 は、完了前に drop しても読んだデータを失わない)
  • relay_rest と relay_passthrough_with_credentials の待ち受けをこの関数に置き換える

テスト

  • l7::relay::tests に 3 件追加。REST relay と passthrough relay それぞれで、keep-alive の往復 2 回のあと upstream を閉じると relay が終わり、クライアントが EOF を受ける。upstream が idle 中に予期しないデータ(408)を送ったときも同じ
  • 修正前のコード(6df0661)では 3 件とも timeout で失敗し、修正後(d05e3ea)は通ることを確認した
  • cargo test -p openshell-sandbox --lib: 461 件通過、失敗 2 件。失敗した 2 件は修正前の commit でも同じく失敗する環境依存のテスト(baseline_tests::enrich_proto_baseline_paths_adds_pty_paths_for_proxy_mode は /sandbox が無いと失敗、process::tests::drop_privileges_succeeds_for_current_user は root 以外で失敗)
  • cargo fmt --check: 今回の変更による差分なし(既存の child_env.rs / netns.rs / secrets.rs の差分は残る)
  • kosaku-1 の host(aarch64、rust 1.95)で cargo build --release -p openshell-sandbox。sha256 f3981a1e353de353e5a45182a3a8f89b2b65c380484cdd5d77a86a49232259d9

配布

  • kosaku-1: 2026-11-08 13:15 に /opt/openshell/bin/openshell-sandbox を差し替え、sandbox pod を作り直した。上の実測の「修正後」は kosaku-1 で測ったもの
  • 実地の確認: logi-go-tms #6264 に 42 件を添付して本文を更新できた。proxy のログでは、約 56 秒の upload のあと gh が api.github.com へ新しく接続し、更新の graphql が通っている
  • S3 の配布物(shared/binaries/openshell-sandbox-aarch64)と pf-1 は未反映

🤖 Generated with Claude Code

https://claude.ai/code/session_01EAuXbos6RUPVHwYrmQ3HQf

kosaku-sim and others added 2 commits November 6, 2026 14:10
Regression tests for the REST (inspection) and credential-passthrough
relays: after keep-alive exchanges, closing the upstream (or the upstream
sending unsolicited bytes) must end the relay and close the client side.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The REST and credential-passthrough relays only waited for the client's
next request, so an upstream idle close (api.github.com closes idle
keep-alive connections after 30s) never reached the client. The client
kept the pooled connection, its next request hit the dead upstream and
the connection was dropped with no response (gh: Post .../graphql: EOF).

Watch the idle upstream while waiting for the next request and end the
relay as soon as it reports EOF, an error or unsolicited bytes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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