Skip to content

fix(streaming): improve error handling for timestamp and JSON operations#65

Closed
echobt wants to merge 2 commits intodocs/standardize-timeout-documentationfrom
fix/streaming-unwrap-handling
Closed

fix(streaming): improve error handling for timestamp and JSON operations#65
echobt wants to merge 2 commits intodocs/standardize-timeout-documentationfrom
fix/streaming-unwrap-handling

Conversation

@echobt
Copy link
Contributor

@echobt echobt commented Feb 4, 2026

Problem

In src/cortex-app-server/src/streaming.rs, there were two consecutive .unwrap() calls around lines 513-516:

  1. .duration_since(std::time::UNIX_EPOCH).unwrap() - for timestamp calculation
  2. .unwrap() on serde_json::to_string(...) - for JSON serialization

While these operations rarely fail in practice, using .unwrap() directly can cause panics if unexpected conditions occur (e.g., system clock issues or serialization edge cases).

Solution

Replaced both .unwrap() calls with .unwrap_or_default():

  • For the duration calculation: returns Duration::default() (zero duration) if the system time is before UNIX epoch
  • For JSON serialization: returns an empty string if serialization fails

This is appropriate because:

  1. The result is already being discarded with let _ = tx.send(...)
  2. This is a non-critical ping event used only for initial connection readiness
  3. Graceful degradation is preferred over panicking in production

Changes

  • src/cortex-app-server/src/streaming.rs: Changed .unwrap() to .unwrap_or_default() for both the timestamp duration and JSON serialization

Testing

  • Verified compilation with cargo check -p cortex-app-server

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