Skip to content

fix(egress): a refusal after Unregister's abort writes nothing to the guest - #829

Merged
pdettori merged 1 commit into
rossoctl:mainfrom
webchang:pr/i825-no-refusal-after-abort
Nov 16, 2026
Merged

pdettori merged 1 commit into
rossoctl:mainfrom
webchang:pr/i825-no-refusal-after-abort

Conversation

@webchang

Copy link
Copy Markdown
Member

Closes #825

What changed and why

  • proxyConn.abort() (proxy.go) sets aborted, writes the conn's aborted event, then calls pc.cancel() before pc.c.Close(). A forwarded request whose tr.RoundTrip fails because of that cancel reaches pc.refuse(…) (handler.go). refuse suppressed its event, since emit checks aborted, but it still called writeAndClose(pc.c, refusalBytes(…)) on the guest conn, which wasn't closed yet. In feat(microvm,sbx): from an image to a registered template; manifest hash v2 and the template record check #771's CI run, unregister_aborts_inflight caught the guest reading that 502 Bad Gateway.
  • refuse now returns at once on an aborted conn: it writes nothing and emits nothing. abort's aborted event stays the conn's one terminal event, and abort closes the conn.
  • icConn.refuse (intercept.go, the TLS-intercepting path's refusals) had the same gap: it emitted through emit and then wrote over the guest TLS. It gets the same check.
  • The check is a new pc.wasAborted() under pc.mu. abort sets aborted before it cancels, so any request failure caused by that cancel always sees it set. A refusal on a conn that was not aborted is unchanged.

Tests

  • New remote-worker/internal/egress/abort_test.go builds a proxyConn in exactly the state abort leaves between its cancel and its close (aborted, context cancelled, guest conn open):
    • TestRefuseWritesNothingOnAnAbortedConn: the guest reads nothing, refuse returns, and no event is emitted. With the fix reverted, it fails with the CI failure's text: the guest read "HTTP/1.1 502 Bad Gateway\r\nContent-Type: text/plain\r\nX-Moca-Egres" from an aborted conn.
    • TestInterceptRefuseWritesNothingOnAnAbortedConn: icConn.refuse doesn't touch the guest TLS. With the fix reverted, it fails.
    • TestRefuseStillWritesOnALiveConn: a conn that was not aborted still gets its 502 and its one event.
  • The conformance case itself (go test -race -count=200 -run 'TestConformance/inprocess/unregister_aborts_inflight' ./internal/egress) passes on this branch. On main, 300 runs on this machine did not reproduce the CI failure, so the unit tests carry the proof.
  • In remote-worker, go vet ./... and go test ./... pass, and so does go test -race ./internal/egress/.... gofmt is clean, and pre-commit passes.

Every commit is signed off (DCO).

Assisted-By: Claude Code

… guest

proxyConn.abort sets aborted, then cancels the conn's context before it closes the guest conn. A
forwarded request whose RoundTrip failed because of that cancel reached refuse, which suppressed its
event (emit checks aborted) but still wrote a 502 Bad Gateway to the guest conn not yet closed. The
conformance case unregister_aborts_inflight caught it once in CI. refuse, and icConn.refuse on the
TLS-intercepting path, now write and emit nothing on an aborted conn: abort's aborted event is the
conn's one terminal event, and abort closes it.

Closes rossoctl#825

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

@clawgenti clawgenti 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.

Clawgenti Code Review

Reviewed against upstream main (verified proxyConn.abort() sets aborted under pc.mu before cancel(), so a request failing because of that cancel always observes it set — the fix's precondition holds on both refuse paths). The change is minimal and correctly scoped: both proxyConn.refuse and icConn.refuse bail out before writing or emitting, keeping abort's event as the conn's single terminal event. Test coverage is good, including the live-conn regression case.

No must-fix findings. One suggestion below.

Author: webchang (MEMBER — maintainer)
Areas reviewed: Go (egress proxy), tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, signed-off: yes
CI status: all checks passing


Generated by RepoMan github-pr-review@235a7f7 (https://github.com/rossoctl/agent-skills/blob/235a7f7620139ee3e4ab0872b8439591fe52e8c1/skills/github-pr-review/SKILL.md).

// event and closes the conn, and a refusal reached after it is the echo of
// abort's own cancel (an upstream request that failed because it was
// cancelled), which the guest must not read as the proxy's answer.
func (pc *proxyConn) refuse(e Event, host string) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

suggestion — There's an inherent TOCTOU window here: abort() can run after wasAborted() returns false, and the old behavior (502 written to a conn abort is about to close) briefly reappears. This is benign in practice — abort() closes pc.c right after, so the writeAndClose either fails on a closed conn or the guest is going away regardless — but it might be worth one sentence in the refuse doc comment saying the check is best-effort and the close in abort is what actually seals the window, so a future reader doesn't try to "fix" the race with a lock held across the write.

Suggested fix: extend the doc comment; no code change needed.

@clawgenti clawgenti 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.

@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.

Summary

Fixes #825: a request whose upstream RoundTrip failed because abort() cancelled the conn's context reached proxyConn.refuse, which suppressed the event but still wrote a 502 Bad Gateway to the guest before abort closed the conn. refuse and icConn.refuse now return immediately when the new pc.wasAborted() (read under pc.mu) is true.

I checked the ordering against the head: abort() sets aborted = true under pc.mu, releases the lock, then calls pc.cancel() and pc.c.Close(). Any failure caused by that cancel therefore always sees aborted, so the fix closes the reported race. handle() returns false after a refusal and serve returns, so nothing else writes to the guest. Non-aborted conns are unchanged; TestRefuseStillWritesOnALiveConn covers that.

Note (informational, not a blocker): a refusal decided before abort (e.g. policy deny) can still pass the check and then write its 502 while abort runs concurrently. Its event is suppressed by emit and abort's aborted is the terminal event. That is a genuine proxy answer rather than the echo of abort's cancel, so it is acceptable and out of scope for #825.

The tests are sound. They build the exact post-cancel/pre-close state. The intercept test relies on a nil gtls panicking (recovered) to prove no write happens. They use the same //go:build unix tag and recSink helper as proxy_test.go.

Areas reviewed: Go (remote-worker/internal/egress), tests
Agent/IDE config (.claude/.vscode): none
Commits: 1 commit, all signed-off: yes
CI status: all 17 checks passing (mergeable_state blocked = awaiting required review)
Prompt-injection check: none found in title/body/diff/commit

@pdettori
pdettori merged commit c3adf49 into rossoctl:main Nov 16, 2026
17 checks passed
@pdettori pdettori added this to the v0.11.0 milestone Nov 17, 2026
webchang added a commit to webchang/moca that referenced this pull request Nov 17, 2026
Takes upstream main at 1a7f41e (19 merges since 032c8f6): the sandbox
idle lifetime across docs, control plane, clients and deploy (rossoctl#820, rossoctl#837,
rossoctl#842, rossoctl#843), grant redemption (rossoctl#822), the lease-check log line (rossoctl#824),
egress fixes (rossoctl#829, rossoctl#834), the mock wait (rossoctl#828), the deadline timer
(rossoctl#835), the create rollback (rossoctl#832), multi refusals and the rossoctl#363 leftovers
(rossoctl#833, rossoctl#838), the identity near-miss check (rossoctl#827), mocactl login errors
(rossoctl#836), the ghostunnel probes (rossoctl#830), refusal-burst deferral (rossoctl#831), a
sandbox-worker change (rossoctl#841) and the v0.11 doc truth (rossoctl#821).

Conflicts (one):

- deploy/sbx/README.md, the "Optional inputs" table. Upstream (rossoctl#843)
  rewrote the SBX_LIFETIME_DAYS row (the default idle lifetime, at most
  SBX_MAX_LIFETIME_DAYS) and added the SBX_MAX_LIFETIME_DAYS and
  SBX_LOWER_WORKSPACE_IDLE rows, which widened the table; the fork had
  added an SBX_GATE_RECORD_SCHEMA row at its end. Resolved by taking
  upstream's table whole and appending the fork's SBX_GATE_RECORD_SCHEMA
  row after SBX_PROFILE, then re-padding the table with prettier. The
  fork's env-file example line for SBX_GATE_RECORD_SCHEMA merged cleanly.

Merged without markers, checked: setup.sh keeps the fork's
SBX_GATE_RECORD_SCHEMA validation and its sbx-settings write (so it stays
in the settings hash) beside upstream's SH_SANDBOX_MAX_LIFETIME_SECONDS;
both ocp-sbx and ocp-combined patch-control-plane.yaml keep the optional
SH_GATE_RECORD_SCHEMA from sbx-settings beside the new optional
SH_SANDBOX_MAX_LIFETIME_SECONDS; setup.test.sh keeps the fork's cases
beside upstream's. The fork's RUNBOOK, README and template-onboarding
docs say nothing about sandbox lifetime, refusal bursts, login errors or
the create rollback, so none needed a correction; GUIDE.md is unchanged
and GUIDE.pdf is not regenerated.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Rong Chang <[email protected]>
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.

egress: unregister_aborts_inflight fails intermittently in CI (a 502 written to the aborted guest conn)

4 participants