Skip to content

fix(exec): replace unwrap with proper error handling for client access#56

Closed
echobt wants to merge 1 commit intodocs/standardize-timeout-documentationfrom
fix/exec-runner-client-unwrap
Closed

fix(exec): replace unwrap with proper error handling for client access#56
echobt wants to merge 1 commit intodocs/standardize-timeout-documentationfrom
fix/exec-runner-client-unwrap

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Problem

The get_or_create_client method in cortex-exec/src/runner.rs line 198 contained an unsafe .unwrap() call:

Ok(self.client.as_ref().unwrap().as_ref())

This could panic at runtime if the client field is None, leading to an unrecoverable crash.

Solution

Replaced the unsafe .unwrap() with proper error handling that returns a descriptive CortexError::Internal when the client is not initialized:

self.client
    .as_ref()
    .map(|c| c.as_ref())
    .ok_or_else(|| CortexError::Internal("LLM client not initialized".to_string()))

This approach:

  • Prevents runtime panics
  • Provides a clear error message for debugging
  • Follows Rust best practices for error handling
  • Maintains the same return type (Result<&dyn ModelClient, CortexError>)

Testing

  • Verified compilation with cargo check -p cortex-exec

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
@echobt
Copy link
Contributor Author

echobt commented Feb 4, 2026

Consolidated into #73 - fix: consolidated error handling improvements

@echobt echobt closed this Feb 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant