-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Unhandled 'error' event when connect followed by close #1835
Comments
|
@lpinca thanks for the quick answer
Ok, so it has the
Yes, I started by adding exactly that in libp2p/js-libp2p-websockets#118 to try it out, but it did not solve the issue. Finally, I wrapped the close around a setTimeout to move to the next event loop and it just worked. |
Can you write a test case using only const WebSocket = require('ws');
const ws = new WebSocket('ws://[2604:1380:45f1:3f00::1]:4002');
ws.on('error', console.error);
ws.close(); |
@lpinca I think you are right. The problem seems to be to wrap the error handler inside a promise. I will keep you posted, thanks |
Actually, it did not solve to extract out of the promise. I ran this in my local machine now and I got the same issue: const WebSocket = require('ws');
async function main () {
const ws = new WebSocket('ws://[2604:1380:45f1:3f00::1]:4002');
ws.on('error', console.error);
ws.close();
}
main()
If I change this into: const WebSocket = require('ws');
async function main () {
const ws = new WebSocket('ws://[2604:1380:45f1:3f00::1]:4002');
ws.on('error', console.error);
setTimeout(() => {
ws.close();
}, 0)
}
main() THe error is addressed as expected and logged with console.error |
Ok I can reproduce on macOS with Node.js 15.5.1. It seems a Node.js issue as this Meanwhile here is a test case using only Node.js built-in modules: const http = require('http');
const req = http.get('http://[2604:1380:45f1:3f00::1]:4002');
req.on('error', console.error);
req.abort(); |
I've opened nodejs/node#36931. I don't have time to investigate at the moment. |
Thanks for your help @lpinca I will be following the issue. |
I'm closing this as the issue has been fixed on Node.js master. It will be included in the next Node.js 15.x release and backported to 14.x soon but it will certainly not be backported to EOL versions like 8. I think there is not much to do about that. |
OS: MacOS
Version: 7.4.2
Node Version: Both
14.15.4
and15.5.1
In libp2p-websockets module we do a
websocket.close()
once an abort signal is done as you can see in https://github.com/libp2p/js-libp2p-websockets/blob/v0.15.0/src/index.js#L80. If this happens immediately after the instantiation of the Websocket client, the following error happens:I did some debugging on this, as it seemed firstly related to Node itself. The code stack looks like this:
ws/lib/websocket.js
Line 220 in a2c0d44
ws/lib/websocket.js
Line 224 in a2c0d44
ws/lib/websocket.js
Line 713 in a2c0d44
ws/lib/websocket.js
Line 720 in a2c0d44
Being this last place where this fails. If I remove line 720 the error does not happen. I was trying to debug further on this, but got blocked. According to JSDocs in 720
stream
could be anet.Socket
, butnet.Socket
does not seem to have an abort function. Furthermore, looking at Node's tests, they include a test for net-connect-immediate-destroy.js.I also noticed that if the STATE was not CONNECTING, the destroy would be wrapped by a setTimeout. Would this be desirable for 720 as this would also fix the issue?
Let me know what you think about this issue and I can help fixing this if you like.
Related to:
The text was updated successfully, but these errors were encountered: