fix: replace panicking unwrap() calls with proper error handling in WebSocket code#1352
fix: replace panicking unwrap() calls with proper error handling in WebSocket code#1352
Conversation
…ebSocket code
- WebSocketConnector::started/stopped used .unwrap() on router.get("connect"/"close")
which would panic if the handler map was misconfigured. Now logs an error instead.
- execute_ws_function had a chain of 5+ unwrap() calls that could panic on handler
dispatch, future resolution, or result extraction. All replaced with match + log::error.
- sync_send_to/async_send_to used Uuid::parse_str().unwrap() on user-provided IDs.
Now returns early with an error log (sync) or PyValueError (async).
- All println!() calls in WebSocket send/broadcast methods replaced with log::debug/error.
Made-with: Cursor
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
📝 WalkthroughWalkthroughTwo files updated to replace panic-prone Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/websockets/mod.rs (1)
78-88: Remainingunwrap()onPy::new()at line 86.While out of scope for the changed lines, there's still a panicking
unwrap()onPy::new()that could fail if Python object allocation fails. For consistency with the PR's goal of removing panic-prone code, consider handling this case:♻️ Suggested improvement
self.message_channel = Python::with_gil(|py| { - Some( - Py::new( - py, - WebSocketChannel { - receiver: Arc::new(tokio::sync::Mutex::new(rx)), - }, - ) - .unwrap(), - ) + match Py::new( + py, + WebSocketChannel { + receiver: Arc::new(tokio::sync::Mutex::new(rx)), + }, + ) { + Ok(channel) => Some(channel), + Err(e) => { + log::error!("Failed to create WebSocketChannel: {}", e); + None + } + } });🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/websockets/mod.rs` around lines 78 - 88, The assignment to self.message_channel uses Py::new(...) inside Python::with_gil(...) and currently calls unwrap() on Py::new, which can panic; change this to handle the Result from Py::new instead of unwrapping: call Py::new(...).map(|pyobj| Some(pyobj)).or_else(|err| { /* log or convert err and return None or propagate */ }) inside the Python::with_gil closure, or propagate an error from the surrounding function; ensure you reference Py::new, WebSocketChannel, Python::with_gil and self.message_channel when implementing the non-panicking error handling (log or return a Result) so allocation failures don’t cause a panic.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/websockets/mod.rs`:
- Around line 78-88: The assignment to self.message_channel uses Py::new(...)
inside Python::with_gil(...) and currently calls unwrap() on Py::new, which can
panic; change this to handle the Result from Py::new instead of unwrapping: call
Py::new(...).map(|pyobj| Some(pyobj)).or_else(|err| { /* log or convert err and
return None or propagate */ }) inside the Python::with_gil closure, or propagate
an error from the surrounding function; ensure you reference Py::new,
WebSocketChannel, Python::with_gil and self.message_channel when implementing
the non-panicking error handling (log or return a Result) so allocation failures
don’t cause a panic.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 487d22cc-d81e-4443-a6b9-e08ad54e291e
📒 Files selected for processing (2)
src/executors/web_socket_executors.rssrc/websockets/mod.rs
Summary
WebSocketConnector::started/stoppedused.unwrap()onrouter.get("connect"/"close")— panics if handler map is misconfigured. Now logs an error and continues.execute_ws_functionhad a chain of 5+.unwrap()calls that could panic on handler dispatch, future resolution, or result extraction. All replaced withmatch+log::error.sync_send_to/async_send_tousedUuid::parse_str().unwrap()on user-provided IDs. Now returns early with an error log (sync) or raisesPyValueError(async).println!()calls in WebSocket send/broadcast methods replaced withlog::debug/log::error.Test plan
Made with Cursor
Summary by CodeRabbit