-
Notifications
You must be signed in to change notification settings - Fork 3
fix: consolidated error handling improvements #73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
Greptile OverviewGreptile SummaryThis PR consolidates 7 error handling improvements into a single cohesive change, significantly improving the robustness of the Cortex codebase. Major Improvements
Notes
Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| src/cortex-agents/src/mention.rs | Added UTF-8 boundary-safe string slicing helpers to prevent panics with multi-byte characters |
| src/cortex-engine/src/async_utils.rs | Converted unwrap() to proper error handling for semaphore operations, changing signatures to return Result |
| src/cortex-engine/src/tools/response_store.rs | New module implementing bounded tool response storage with automatic cleanup and TTL |
| src/cortex-exec/src/runner.rs | Replaced unwrap() with proper error handling for client access and added timeout documentation |
| src/cortex-mcp-server/src/server.rs | Fixed TOCTOU race condition in initialization by using atomic check-and-set with single write lock |
| src/cortex-plugins/src/registry.rs | Fixed TOCTOU race condition in plugin registration using Entry API for atomic check-and-insert |
Sequence Diagram
sequenceDiagram
participant Client
participant Middleware
participant Runner
participant AsyncUtils
participant MCP_Server
participant Plugin_Registry
participant ResponseStore
Note over Client,ResponseStore: Error Handling Flow Improvements
Client->>Middleware: HTTP Request with request-id
alt Invalid request-id header
Middleware->>Middleware: HeaderValue::from_str fails
Middleware->>Client: Return "invalid-request-id" fallback
else Valid request-id
Middleware->>Runner: Process request
end
Runner->>Runner: get_client()
alt Client not initialized
Runner->>Client: Return CortexError::Internal
else Client initialized
Runner->>AsyncUtils: execute task
end
AsyncUtils->>AsyncUtils: semaphore.acquire()
alt Semaphore closed
AsyncUtils->>Runner: Return CortexError::Internal
else Semaphore available
AsyncUtils->>AsyncUtils: Execute task
AsyncUtils->>Runner: Return Ok(result)
end
Runner->>ResponseStore: store(call_id, tool_name, result)
ResponseStore->>ResponseStore: Check capacity (MAX_STORE_SIZE=500)
alt At capacity
ResponseStore->>ResponseStore: Evict oldest entry
end
ResponseStore->>ResponseStore: Store response with TTL
ResponseStore->>Runner: Confirm stored
Note over MCP_Server: TOCTOU Race Prevention
Client->>MCP_Server: Initialize request (concurrent)
MCP_Server->>MCP_Server: Acquire write lock atomically
alt Already initialized
MCP_Server->>Client: Return error
else Uninitialized
MCP_Server->>MCP_Server: Set state to Initializing
MCP_Server->>MCP_Server: Complete initialization
MCP_Server->>Client: Return success
end
Note over Plugin_Registry: TOCTOU Race Prevention
Client->>Plugin_Registry: Register plugin (concurrent)
Plugin_Registry->>Plugin_Registry: Acquire write lock
Plugin_Registry->>Plugin_Registry: Use Entry API
alt Plugin already exists
Plugin_Registry->>Client: Return AlreadyExists error
else Plugin new
Plugin_Registry->>Plugin_Registry: Insert atomically
Plugin_Registry->>Client: Return success
end
Note over ResponseStore: Automatic Cleanup
ResponseStore->>ResponseStore: Periodic cleanup (60s interval)
ResponseStore->>ResponseStore: Remove expired entries (TTL=5min)
ResponseStore->>ResponseStore: Update stats
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 files reviewed, no comments
- Remove unused 'warn' import from response_store.rs - Fix rustfmt formatting issues across multiple files - Fixes CI format and clippy checks
|
Closing this PR to consolidate into a single mega-PR combining all bug fixes. The changes will be included in a new consolidated PR. |
This PR consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change. ## Categories ### Security Fixes - Path traversal prevention in MCP and session storage - Shell injection prevention in restore scripts - Secure random temp files for external editor - TOCTOU race condition fixes ### TUI Improvements - Overflow prevention for u16 conversions - Cursor positioning fixes in selection lists - Unicode width handling for popups - Empty section handling in help browser ### Error Handling - Graceful semaphore and init failure handling - Improved error propagation in middleware - Better client access error handling - SystemTime operation safety ### Memory and Storage - Cache size limits to prevent unbounded growth - File lock cleanup for memory leak prevention - fsync after critical writes for durability - Bounded ToolResponseStore with automatic cleanup ### Protocol Robustness - Buffer size limits for StreamProcessor - ToolState transition validation - State machine documentation ### Numeric Safety - Saturating operations to prevent overflow/underflow - Safe UTF-8 string slicing throughout codebase ### Tools - Parameter alias support for backward compatibility - Handler name consistency fixes ## Files Modified Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common, cortex-protocol, cortex-storage, cortex-mcp-server, and other crates. Closes #69, #70, #71, #73, #75, #80, #82, #87, #88
This PR consolidates all bug fixes and security improvements from PRs #69-88 into a single cohesive change. ## Categories ### Security Fixes - Path traversal prevention in MCP and session storage - Shell injection prevention in restore scripts - Secure random temp files for external editor - TOCTOU race condition fixes ### TUI Improvements - Overflow prevention for u16 conversions - Cursor positioning fixes in selection lists - Unicode width handling for popups - Empty section handling in help browser ### Error Handling - Graceful semaphore and init failure handling - Improved error propagation in middleware - Better client access error handling - SystemTime operation safety ### Memory and Storage - Cache size limits to prevent unbounded growth - File lock cleanup for memory leak prevention - fsync after critical writes for durability - Bounded ToolResponseStore with automatic cleanup ### Protocol Robustness - Buffer size limits for StreamProcessor - ToolState transition validation - State machine documentation ### Numeric Safety - Saturating operations to prevent overflow/underflow - Safe UTF-8 string slicing throughout codebase ### Tools - Parameter alias support for backward compatibility - Handler name consistency fixes ## Files Modified Multiple files across cortex-tui, cortex-engine, cortex-exec, cortex-common, cortex-protocol, cortex-storage, cortex-mcp-server, and other crates. Closes #69, #70, #71, #73, #75, #80, #82, #87, #88
Summary
This PR consolidates 7 error handling fixes into a single, cohesive change.
Included PRs:
Key Changes:
unwrap()calls with proper error handling throughoutFiles Modified:
src/cortex-agents/src/mention.rssrc/cortex-app-server/src/auth.rssrc/cortex-app-server/src/config.rssrc/cortex-app-server/src/middleware.rssrc/cortex-common/src/http_client.rssrc/cortex-engine/src/async_utils.rssrc/cortex-engine/src/ratelimit.rssrc/cortex-engine/src/tools/handlers/grep.rssrc/cortex-engine/src/tools/mod.rssrc/cortex-engine/src/tools/response_store.rs(new)src/cortex-exec/src/runner.rssrc/cortex-mcp-server/src/server.rssrc/cortex-plugins/src/registry.rssrc/cortex-tui/src/cards/*Closes #48, #54, #55, #56, #57, #61, #65