fix: guard_sync raises GuardError instead of TimeoutError#41
Merged
Conversation
…sage TimeoutError from concurrent.futures was leaking to users when guard_sync was called outside MCP context. Now catches it and raises GuardError with a helpful message explaining the likely cause.
|
✅ Integration tests passed! capiscio-core gRPC tests working. |
|
✅ Integration tests passed! capiscio-core gRPC tests working. |
There was a problem hiding this comment.
Pull request overview
This PR standardizes guard_sync error behavior to match the async @guard decorator by converting timeouts from future.result(timeout=30.0) into a GuardError with DenyReason.INTERNAL_ERROR, improving consistency for callers of the public API.
Changes:
- Wraps
asyncio.run_coroutine_threadsafe(...).result(timeout=30.0)in a timeout handler. - Re-raises timeouts as
GuardError(reason=INTERNAL_ERROR)with a descriptive detail message.
Comments suppressed due to low confidence (1)
capiscio_mcp/guard.py:701
- This change alters the public failure mode of
guard_sync(mapping a timeout intoGuardError). There’s existing test coverage forguard_sync, but no test asserting that a timeout is converted intoGuardError(reason=INTERNAL_ERROR)with the expected detail. Adding a targeted unit test would prevent regressions and confirm the intended behavior.
try:
result = future.result(timeout=30.0)
except (TimeoutError, concurrent.futures.TimeoutError):
raise GuardError(
reason=DenyReason.INTERNAL_ERROR,
detail=(
"Guard evaluation timed out. This usually means the "
"guard is being called outside of an MCP request context "
"or the gRPC core server is not responding."
),
)
|
✅ Integration tests passed! capiscio-core gRPC tests working. |
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.
DX Audit Fix
Found during a hands-on DX walkthrough of the getting-started docs with real API calls.
Changes
guard_syncraises GuardError instead of TimeoutError (Bug #5)When
guard_syncwas called outside an MCP request context (or the gRPC server wasn't responding),future.result(timeout=30.0)raised a rawTimeoutError. This is inconsistent with the async@guarddecorator which raisesGuardError.Fix: Wrap the
future.result()call in a try/except that catchesTimeoutErrorandconcurrent.futures.TimeoutError, re-raising asGuardError(reason=DenyReason.INTERNAL_ERROR)with a descriptive message.Breaking Changes
Technically changes the exception type from
TimeoutErrortoGuardError. However,GuardErroris the documented and expected error type — the async@guardalready raises it. Any code catchingTimeoutErrorwas relying on undocumented bug behavior.Testing
@guard_sync(min_trust_level=2)called outside MCP context raisesGuardError(reason=BADGE_MISSING)✅TimeoutErrorto callers ✅