close() waited for the peer's Close reply with a read timeout that applied to each read, so a peer that kept sending other frames, or sent one frame a byte at a time, held close() for as long as it liked. Bound the wait as a whole. close() sets the stream's max timeout, which shortens each wait to what is left, and stops reading frames at the deadline, which matters when the peer keeps the socket full and no wait happens at all. WebSocketSSLStream gains set_max_timeout(), the one stream a WebSocket uses that lacked it. A read() running on another thread when close() is called still waits for the reply up to its own read timeout, as documented. The docs now say how to bound it.
2.5 KiB
title, order, status
| title | order | status |
|---|---|---|
| S19. Shut Down Gracefully | 38 | draft |
To stop the server, call Server::stop(). It's safe to call even while requests are in flight, so you can wire it to SIGINT or SIGTERM for a graceful shutdown.
Basic usage
httplib::Server svr;
svr.Get("/", [](const auto &, auto &res) { res.set_content("ok", "text/plain"); });
std::thread t([&] { svr.listen("0.0.0.0", 8080); });
// wait for input on the main thread, or whatever
std::cin.get();
svr.stop();
t.join();
listen() blocks, so the typical pattern is: run the server on a background thread and call stop() from the main thread. After stop(), listen() returns and you can join().
Shut down on a signal
Here's how to stop the server on SIGINT (Ctrl+C) or SIGTERM.
#include <csignal>
httplib::Server svr;
// global so the signal handler can reach it
httplib::Server *g_svr = nullptr;
int main() {
svr.Get("/", [](const auto &, auto &res) { res.set_content("ok", "text/plain"); });
g_svr = &svr;
std::signal(SIGINT, [](int) { if (g_svr) g_svr->stop(); });
std::signal(SIGTERM, [](int) { if (g_svr) g_svr->stop(); });
svr.listen("0.0.0.0", 8080);
std::cout << "server stopped" << std::endl;
}
stop() is thread-safe and signal-safe — you can call it from a signal handler. Even when listen() is running on the main thread, the signal pulls it out cleanly.
What happens to in-flight requests
When you call stop(), new connections are refused, but handlers that are already running are allowed to finish. A response still being sent by a content provider (a streaming response, for example) is cut short, though. Once all workers drain, listen() returns. That's what makes it graceful.
Warning: There's a wait between calling
stop()andlisten()returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code.
Note:
stop()doesn't interrupt WebSocket handlers. A handler looping onread()keeps running as long as its peer stays connected, andlisten()doesn't return until it ends. Keep track of open WebSocket connections andclose()them on shutdown, e.g. withCloseStatus::GoingAway. The handler'sread()then waits for the peer's Close reply up to its read timeout (300 seconds by default). To shorten that, callset_read_timeout()in the handler before the loop;read()then also returnsTimeoutwhenever the peer stays idle that long, so the loop has to handle it.