Repository navigation
Conversation
…-closed The request middleware decided a request was client-closed (499) from a bare errors.Is(cancelCause, context.Canceled). But context.Canceled only means some context in the chain was cancelled, not that the client disconnected. The API server roots every request at a base context whose cancel() fires on process teardown, so an in-flight request cut by shutdown saw plain context.Canceled and was logged/recorded as 499 — mislabeling the server's own teardown as client behaviour. - shared/middleware: add ErrServerShuttingDown, a cancel cause a server sets on the parent of every request context on teardown, alongside the existing ErrRequestTimeout. Add IsClientDisconnect / IsServerShutdown classifiers that check the server-side causes first (both satisfy errors.Is(_, Canceled) once they propagate as a cancellation). - logging / metrics / tracing middleware: classify via the helpers. A request cut by server teardown keeps its real status and is tagged server_shutdown rather than being counted as a 499 client disconnect. - api/main: root the request context with context.WithCancelCause and cancel it with ErrServerShuttingDown, so the cause propagates through http.Server BaseContext and the per-request timeout wrapper into the middleware. Fixes e2b-dev#3685
AdaAibaby
requested review from
ValentaTomas,
dobrac and
jakubno
as code owners
October 16, 2026 10:19
This branch has not been deployed
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.
Problem
Fixes #3685.
The request middleware decided a request was "client closed" (499) purely from
errors.Is(cancelCause, context.Canceled). Butcontext.Canceledonly means some context in the chain was cancelled — not that the client disconnected. The API server roots every request at a base context (main.go,http.Server.BaseContext) whosecancel()fires on process teardown. When that happens, in-flight requests see plaincontext.Canceled(notErrRequestTimeout), so the server's own teardown was logged/recorded/traced as a 499 client closed.The timeout path already had a dedicated cause (
ErrRequestTimeout→ 408); server-initiated cancellation had none, so it fell into thecontext.Canceledcatch-all across all three middleware sites (logging.go,otel/metrics,otel/tracing).Changes
shared/pkg/middleware/timeout.go: addErrServerShuttingDown(a cancel cause a server sets on the parent of every request context on teardown), plusIsClientDisconnect/IsServerShutdownclassifiers. The server-side causes are checked first because both satisfyerrors.Is(_, context.Canceled)once they propagate as a cancellation.server_shutdowninstead of being counted as a 499 client disconnect. Client disconnect behaviour is unchanged.api/main.go: root the request context withcontext.WithCancelCauseand cancel it withErrServerShuttingDown, so the cause propagates throughBaseContextand the per-request timeout wrapper into the middleware.Scope note: this PR covers the server-vs-client misattribution (part 1 of the issue). The second facet — handler-level
ReportCriticalErrorERROR logs / span exception events not being reconciled with the 499 reclassification — is left for a follow-up, since #3683/#3684 already removed the main offender on the sandbox-create path.Tests
TestClassifyCancelCause: table test forIsClientDisconnect/IsServerShutdownacross nil, client disconnect (incl. wrapped), request timeout, server shutdown (incl. wrapped), deadline-exceeded and unrelated errors.TestServerShutdownCausePropagatesToOuterMiddleware: end-to-end — a request whose parent context is cancelled withErrServerShuttingDownsurfaces that cause through theRequestTimeoutwrapper to an outer middleware, classified as server shutdown, not client disconnect. (I separately verified via a throwaway net/http probe that aBaseContextcancel-cause does reachr.Context().)go test ./pkg/middleware/...passes;go vetandgolangci-lintclean on the changed packages; bothpackages/sharedandpackages/apibuild.