fix(engine): handle semaphore and init failures gracefully in async_utils#48
Closed
fix(engine): handle semaphore and init failures gracefully in async_utils#48
Conversation
…tils Fixes potential panics in async utilities: - ConcurrencyLimiter::execute now returns Result<T> instead of T - AsyncOnce::get_or_init now returns Result<T> with proper error handling - concurrent() function now returns Result<Vec<T>> - ConcurrencyLimiter::acquire in ratelimit.rs now returns Result All semaphore acquire calls now use map_err instead of unwrap() to provide descriptive error messages when semaphores are closed. Fixes #5205, #5201
Greptile OverviewGreptile SummaryThis PR eliminates potential panic points in async utilities by replacing Key Changes:
All error cases now return descriptive Confidence Score: 5/5
|
| Filename | Overview |
|---|---|
| src/cortex-engine/src/async_utils.rs | Replaced unwrap() calls with proper error handling using Result types for semaphore operations |
| src/cortex-engine/src/ratelimit.rs | Added error handling for semaphore acquire operation in ConcurrencyLimiter |
| src/cortex-agents/src/mention.rs | Added UTF-8 boundary validation for string slicing operations to prevent panics |
Sequence Diagram
sequenceDiagram
participant Caller
participant ConcurrencyLimiter
participant Semaphore
participant ErrorHandler
Note over Caller,ErrorHandler: Before: Panics on semaphore closure
Caller->>ConcurrencyLimiter: execute(fn)
ConcurrencyLimiter->>Semaphore: acquire().await
alt Semaphore closed (old behavior)
Semaphore-->>ConcurrencyLimiter: Err
ConcurrencyLimiter->>ConcurrencyLimiter: unwrap() → PANIC!
else Semaphore open (old behavior)
Semaphore-->>ConcurrencyLimiter: Ok(permit)
ConcurrencyLimiter->>Caller: fn().await result
end
Note over Caller,ErrorHandler: After: Graceful error handling
Caller->>ConcurrencyLimiter: execute(fn)
ConcurrencyLimiter->>Semaphore: acquire().await
alt Semaphore closed (new behavior)
Semaphore-->>ConcurrencyLimiter: Err
ConcurrencyLimiter->>ErrorHandler: map_err → CortexError::Internal
ErrorHandler-->>Caller: Result::Err
else Semaphore open (new behavior)
Semaphore-->>ConcurrencyLimiter: Ok(permit)
ConcurrencyLimiter->>Caller: Result::Ok(fn().await)
end
echobt
added a commit
that referenced
this pull request
Feb 4, 2026
This PR consolidates the following error handling fixes: - #48: Handle semaphore and init failures gracefully in async_utils - #54: Improve error handling in session storage operations (includes TOCTOU race fixes) - #55: Add validation for threshold, ratio, and token count fields - #56: Replace unwrap with proper error handling for client access - #57: Use unwrap_or_default for SystemTime operations - #61: Handle invalid request-id header values gracefully - #65: Improve error handling for timestamp and JSON operations in streaming Key changes: - Added graceful handling for semaphore and init failures - Bound ToolResponseStore size and cleanup consumed entries - Eliminated TOCTOU races in MCP server and plugin registry - Replaced unwrap() with proper error handling throughout - Added validation for config fields - Improved error propagation in middleware
Contributor
Author
|
Consolidated into #73 - fix: consolidated error handling improvements |
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.
Summary
Fixes #5205 and #5201 - Potential panics in async utilities.
Problem
Semaphore acquire and AsyncOnce init use unwrap() that can panic:
ConcurrencyLimiter::executein async_utils.rs panics if semaphore is closedAsyncOnce::get_or_initpanics if value is missing after init flag is setconcurrent()function panics if semaphore is closedConcurrencyLimiter::acquirein ratelimit.rs panics if semaphore is closedSolution
Added proper error handling with Result return types:
ConcurrencyLimiter::executenow returnsResult<T>instead ofTAsyncOnce::get_or_initnow returnsResult<T>with descriptive error messagesconcurrent()function now returnsResult<Vec<T>>ConcurrencyLimiter::acquirein ratelimit.rs now returnsResult<ConcurrencyPermit>All semaphore acquire calls now use
map_errinstead ofunwrap()to provide descriptive error messages when semaphores are closed unexpectedly.Testing
cargo test -p cortex-engine async_utilsandcargo test -p cortex-engine ratelimit