-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
How do I shut down a server cleanly? #1575
Comments
TCP ports that are bound often have a property of not fully closing when asked to, so that OS can handle any late packets inbound to that port. There is a socket option to tell the OS "Hey, it's really OK if I reuse the port right away". You could try setting that option to see if that's the issue (it could also just be a bug in hyper or tokio). And... I just checked, and hyper's built-in listener builder doesn't have a way to set that option, but you could build a custom one (and if it works, we can add that method into hyper directly!): extern crate net2;
extern crate tokio;
// hyper and stuff of course...
use tokio::net::TcpListener;
fn listener() -> io::Result<TcpListener> {
// whatever addr
let addr = SocketAddr::from(([127, 0, 0, 1], 3030));
net2::TcpBuilder::new_v4()?
.reuse_address(true)?
.bind(&addr)?
.listen(1024).and_then(|listener| {
TcpListener::from_std(listener, &Default::default())
})
}
// making a server with a custom listener
let incoming = listener()?.incoming();
let opts = hyper::server::conn::Http::new();
let server = hyper::server::Builder::new(incoming, opts)
.serve(new_service); PS, calling |
Thank you for the quick and in depth reply. I was aware of the existence of I am using Anyway, I tried using With this configuration the setup succeeds, however I find that I cannot connect to a server on a recreated socket. In other words, after running the server and then restarting it within the same process, it does not accept TCP connections. It also no longer accepts connections when bound on IPv6 addresses at all, even for the first creation of the socket. In conclusion I'm not convinced that the problem that is normally solved by using Thanks again for this great library, by the way. |
I see now that shutting down the server is a case of making the incoming stream end. I will see if that helps things. |
Since the There is a concept of graceful shutdown in hyper, specifically on individual |
Here's some infrastructure I built out for a graceful server shutdown: https://gist.github.com/sfackler/77c02d840a9ba08b58a435cab2901809 Each |
Likewise, since Conduit is currently using the I think something just like it could be ported to hyper's |
I have the same behavior with Windows (and Appveyor) and a server implementation based on https://github.com/actix/actix-web which uses tokio. Everything is fine on macOS and Linux (and Travis)! I tried to setup a socket with the socket2 crate (set_reuse_address) but without success. |
The server seems to be ok. I think it is the client (reqwest): tokio-rs/mio#776 |
This adds a "combinator" method to `Server`, which accepts a user's future to "select" on. All connections received by the `Server` will be tracked, and if the user's future finishes, graceful shutdown will begin. - The listener will be closed immediately. - The currently active connections will all be notified to start a graceful shutdown. For HTTP/1, that means finishing the existing response and using `connection: clone`. For HTTP/2, the graceful `GOAWAY` process is started. - Once all active connections have terminated, the graceful future will return. Closes #1575
After spawning a server on a tokio
Runtime
, then shutting down the runtime (usingshutdown_now
), recreating a server on the same IP and port with a newRuntime
panics, claimingIf I instead recreate the server by restarting the process, it works fine.
This behaviour is being observed on Windows 7. I have tried sleeping the thread for up to 10 seconds before recreating the server and the same error occurs.
Other than shutting down the
Runtime
, is there something else I must do to cleanly close a server? The documentation mentions the concept of "shutdown" in passing, but does not elaborate on what must be done to achieve that correctly.The text was updated successfully, but these errors were encountered: