Make xhr transports resilient to onmessage errors#564
Make xhr transports resilient to onmessage errors#564jcheng5 wants to merge 4 commits intosockjs:mainfrom
Conversation
Without this fix, once a jsonp-polling message handler throws an uncaught exception, the receiver stops polling.
|
Looking around the code base there are so many places we I tried looking around for what is considered best practice for this and didn't find much. Have you found anything that recommends one way or another? |
|
As a library author, I totally understand that temptation. However, my guess is that the vast majority of the events thrown and caught in this library are internal; the only events exposed to user code are "Robustifying" in the sense of sockjs-client catching exceptions from users' handler code has one significant downside I can think of, which is that they'll no longer behave the same as unhandled exceptions. With a true WebSocket, unhandled errors in Would it help if I created a test suite to see what happens if open/message/close handlers throw, and test it across all the transports? I'm also happy to write further patches if those tests turn up any bad results. |
|
@brycekahle My offer stands here—happy to do some more work to address any concerns! |
|
Just a note here -- #563 has auto-closed, but we would like a path forward for this work. We have had quite a bit of success using this patch. |
There was a problem hiding this comment.
Pull Request Overview
This PR makes XHR and JSONP transports resilient to errors in message handlers by wrapping message emission in try-finally blocks. This prevents message handler exceptions from breaking the transport's internal state and ensures proper cleanup occurs.
Key changes:
- Wrap message emission in try-finally blocks for both XHR and JSONP receivers
- Ensure buffer position advances in XHR even when handlers throw exceptions
- Ensure cleanup occurs in JSONP even when handlers throw exceptions
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/transport/receiver/xhr.js | Wraps message emission in try-finally to ensure buffer position advances even when handlers throw |
| lib/transport/receiver/jsonp.js | Wraps message emission in try-finally to ensure cleanup occurs even when handlers throw |
| tests/lib/receivers.js | Adds unit tests for error resilience in both XHR and JSONP message handlers |
| return; | ||
| } | ||
| try { | ||
| expect(i).to.equal(3); |
There was a problem hiding this comment.
The variable 'i' is not defined in this scope. This will cause a ReferenceError and break the test.
Fixes #563
Update Sept 22, 2021: Discovered the problem was not fixed for jsonp-polling; that's fixed now as well. Also added the relevant unit tests for both xhr and jsonp.