You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Http2Server.prototype.close Does Not Work as Expected #28214
let
http2=require('http2'),
server=http2.createServer()
server.listen(8000,'127.0.0.1',()=>{
let client=http2.connect('http://127.0.0.1:8000',()=>{
console.log('listener called')
server.close(()=>{
console.log('close')
})
})
})
Expected: server.close stops the server from accepting new connections.
Result: console.log('close') never get called.
I aware that HTTP/2 works different to HTTP/1.1. And I can and should call session.close() on every active session to stop them from accepting new requests.
server.close seems also wait until there is no active sessions to call the callback function, which is not mentioned in the documentation.
And, to make there is no active sessions, I must first of all stop the server from accepting new connections.
server.close seems also wait until there is no active sessions to call the callback function, which is not mentioned in the documentation
I guess it wasn't documented because the behavior is the same of net.Server.prototype.close(). The callback is not called until there is an open connection.
Oh, I see, it is documented. The callback is called when the server is closed, and the server waits, until there is no active connection, to close.
But I think the point is not whether it is documented or not:
An asynchronous operation usually call its callback function right after the operation is done.
As net.Server.prototype.close is documented, the operation is defined as "Stops the server from accepting new connections and keeps existing connections.". But the callback also waits until the server is closed to be called, rather than right after the operation is done.
Of course, it is not common does not mean it is bad. But think of the following situation:
Now I want to force a server to be closed with the following algorithm:
stops the server from accepting new connections
close all active connections
The second step must be executed after the first step. As the first step is an asynchronous operation, I would wait for its callback function to be called, to ensure that it is done. But the callback is called also implies that the second step is done. It is a dead lock.
Another approach:
close all active connections
stops the server from accepting new connections
It will not work, because once the server is still accepting new connections, there is no way to close all active connections in finite amount of steps.
No new connection is established after server.close() is called. @lpinca
net.Server.prototype.close:
Stops the server from accepting new connections and keeps existing connections. This function is asynchronous, the server is finally closed when all connections are ended and the server emits a 'close' event.
If the function is defined to "stop the server from accepting new connections and keep existing connections", and the work is done before it returns, I would rather say that it is synchronous instead of asynchronous; saying that it is asynchronous means that, only through waiting that callback, I can ensure the work is done.
However, the function is named close, and it, in a sense, closes the server; its current behavior still makes a sense (stop ..., and wait until all connections are ended). It can be explained in the documentation, that: The closing of a server, involves
stoping it from accepting new connection (synchronous), and
waiting until all connections are ended (asynchronous); and this function does the both.
Activity
lpinca commentedon Jun 30, 2019
I guess it wasn't documented because the behavior is the same of
net.Server.prototype.close(). The callback is not called until there is an open connection.cjihrig commentedon Jul 1, 2019
I think this is behaving as documented.
anliting commentedon Jul 3, 2019
Oh, I see, it is documented. The callback is called when the server is closed, and the server waits, until there is no active connection, to close.
But I think the point is not whether it is documented or not:
An asynchronous operation usually call its callback function right after the operation is done.
As
net.Server.prototype.closeis documented, the operation is defined as "Stops the server from accepting new connections and keeps existing connections.". But the callback also waits until the server is closed to be called, rather than right after the operation is done.Of course, it is not common does not mean it is bad. But think of the following situation:
Now I want to force a server to be closed with the following algorithm:
The second step must be executed after the first step. As the first step is an asynchronous operation, I would wait for its callback function to be called, to ensure that it is done. But the callback is called also implies that the second step is done. It is a dead lock.
Another approach:
It will not work, because once the server is still accepting new connections, there is no way to close all active connections in finite amount of steps.
lpinca commentedon Jul 6, 2019
There is no need to wait for the callback before closing connections. No new connection is established after
server.close()is called.anliting commentedon Jul 7, 2019
net.Server.prototype.close:If the function is defined to "stop the server from accepting new connections and keep existing connections", and the work is done before it returns, I would rather say that it is synchronous instead of asynchronous; saying that it is asynchronous means that, only through waiting that callback, I can ensure the work is done.
However, the function is named
close, and it, in a sense, closes the server; its current behavior still makes a sense (stop ..., and wait until all connections are ended). It can be explained in the documentation, that: The closing of a server, involvesdoc: clarify http2 server.close() behavior
doc: clarify http2 server.close() behavior