-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Make xhr transports resilient to onmessage errors #564
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
base: main
Are you sure you want to change the base?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
@@ -226,14 +252,15 @@ describe('Receivers', function () { | |||
return; | |||
} | |||
try { | |||
expect(i).to.equal(3); |
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.
The variable 'i' is not defined in this scope. This will cause a ReferenceError and break the test.
Copilot uses AI. Check for mistakes.
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.