-
-
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
Abort the request if close
is called while connecting
#956
Conversation
this.readyState = WebSocket.CLOSED; | ||
return; | ||
this._req.abort(); | ||
this.emit('error', new Error('closed while connecting')); |
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.
This is added to match the browser behavior. I'm not sure if we want this error.
I'm also not sure if the error message is ok, suggestions?
this._req = httpObj.get(requestOptions); | ||
|
||
this._req.on('error', (error) => { | ||
if (this._req.aborted) return; |
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.
This is to avoid "socket hang up" errors which are emitted when a request is aborted and a response is not received.
We can remove the check as the error would be swallowed anyway but I think it's better to be explicit here.
@websockets/admin please review when/if you can. |
Looks sane to me. |
this.readyState = WebSocket.CLOSED; | ||
return; | ||
this._req.abort(); | ||
this.emit('error', new Error('closed while connecting')); |
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.
Well it makes sense, so just keep it.
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.
What about the error message? Can you think of something better?
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.
Some alternatives:
- "closed before the connection is established"
- "closed before receiving a handshake response"
I have no strong opinions.
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.
closed before the connection is established
is a good one as well.
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.
Ok, I'll change it to that, merge this and release 2.0.0-beta.0
if there are no objections.
@lpinca This breaks engine.io - if you try to connect to a server which is down you get an infinite loop because engine.io calls close when you emit the error in close. |
Should this be fixed in Engine.IO? |
I guess Engine.IO is not the only one who calls |
@3rd-Eden Throwing while connecting deviates from the web API in which What's the proper way to abort a connection with |
@julien-f add a listener for the |
My bad, I'm mistaken, @lpinca Thanks for your answer and sorry for bothering you :) |
Currently
WebSocket.prototype.close()
only sets thereadyState
toCLOSED
if called before receiving a handshake response. Then the connection is eventually closed when the response is received.This is wrong as the
connection
event is still emitted on the server.This patch aborts the client request if
WebSocket.prototype.close()
is called before receiving a handshake response.Fixes #388