Split shim and runner response errors into status and body - #4261
Merged
Conversation
Follow-up to #4251, which separated API errors from connection errors but left three loose ends. `ShimHTTPError`/`RunnerHTTPError` were misnamed. We raise them only for status codes, while "HTTP error" suggests anything about the protocol, including transport. Rename them under a parent that says what the family means -- the peer answered, the answer is unusable, and repeating the request is not expected to help: ShimError |-- ShimAPIVersionError # our bug, stays loud `-- ShimResponseError |-- ShimResponseStatusError # 4xx/5xx as API error codes `-- ShimResponseBodyError # the body cannot be read Call sites catch the `*ResponseError` parent: none of them cares which leaf it was. Build the errors from the `Response` instead of wrapping the one from `raise_for_status()`. Its message carried a reason phrase that Go derives from the status code alone, a client/server split that 4xx vs 5xx already says, and a URL whose authority is always localhost or a percent-encoded socket path -- but not the body, which is where shim and runner put the actual message. Before: 404 Client Error: Not Found for url: http+unix://%2Ftmp%2F.../api/tasks/abc After: GET /api/tasks/abc: 404: Task not found Parse response bodies with pydantic instead of `Response.json()`. Besides saving a decode and an intermediate dict, this fixes a misclassification: `Response.json()` raises `requests.JSONDecodeError`, which is a `RequestException`, so a peer sending garbage was reported as a transport failure and retried. Both malformed JSON and a schema mismatch now raise `ValidationError`, wrapped as `*ResponseBodyError`. Nothing inside a client method raises `RequestException` any more except genuine transport, which is what `runner_ssh_tunnel` already documents. This also closes the `pydantic.ValidationError` leak noted in #4251: an unparsable response body no longer escapes the pipeline tasks.
un-def
added a commit
that referenced
this pull request
Sep 7, 2026
Follow-up to #4251 and #4261, which separated the errors reported by a peer's API from connection failures but left the two halves of that signal travelling by different mechanisms. `runner_ssh_tunnel` returned `Union[Literal[False], R]` -- `False` if the peer could not be reached, the wrapped function's value otherwise. The API half is already a pair of exception families, so callers had to translate one channel into the other by hand: except client.ShimResponseError as e: # Same outcome as a connection error, # `_handle_instance_unreachable()` below, but the cause is now # logged instead of being silently indistinguishable. logger.warning(...) shim_state = False if shim_state is not False: Raise `PeerConnectionError` instead, declared next to the families it complements. It composes with them in a single `except` clause, carries the reason `False` could not, and chains the underlying `SSHError` or `RequestException` as `__cause__`. The sentinel had also leaked into the signatures of the functions the decorator wraps: four of them annotated their own return type as `Union[X, Literal[False]]`, because they used the channel to say "treat this as unreachable". They now return `X`. `_RunnerAvailability` is gone. #4251 introduced it to carry `UNREACHABLE`, a second encoding of the state `False` already meant. Without that member it is a two-member enum, so `_get_runner_availability()` becomes `_is_runner_available() -> bool` and lets `RunnerResponseError` propagate. Two defects the sentinel had produced: * `stop_runner()` wrapped its call in `except SSHError`, which nothing could reach: the decorator caught `SSHError` itself in one branch, and `instance_connection_pool.get_or_open()` swallowed it in the other. * Both metrics collectors tested `isinstance(res, bool)`, since their wrapped functions return `Optional[...]`, where `if not res` cannot tell `None` from `False`. Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Follow-up to #4251, which separated API errors from connection errors but left three loose ends.
ShimHTTPError/RunnerHTTPErrorwere misnamed. We raise them only for status codes, while "HTTP error" suggests anything about the protocol, including transport. Rename them under a parent that says what the family means -- the peer answered, the answer is unusable, and repeating the request is not expected to help:Call sites catch the
*ResponseErrorparent: none of them cares which leaf it was.Build the errors from the
Responseinstead of wrapping the one fromraise_for_status(). Its message carried a reason phrase that Go derives from the status code alone, a client/server split that 4xx vs 5xx already says, and a URL whose authority is always localhost or a percent-encoded socket path -- but not the body, which is where shim and runner put the actual message.Before:
After:
Parse response bodies with pydantic instead of
Response.json(). Besides saving a decode and an intermediate dict, this fixes a misclassification:Response.json()raisesrequests.JSONDecodeError, which is aRequestException, so a peer sending garbage was reported as a transport failure and retried. Both malformed JSON and a schema mismatch now raiseValidationError, wrapped as*ResponseBodyError. Nothing inside a client method raisesRequestExceptionany more except genuine transport, which is whatrunner_ssh_tunnelalready documents.This also closes the
pydantic.ValidationErrorleak noted in #4251: an unparsable response body no longer escapes the pipeline tasks.