Files
cpp-httplib/docs-src/pages/en/cookbook/s19-graceful-shutdown.md
T
yhirose 9bfd2ddf1c Fix README and docs pages that no longer match the code
Bring the READMEs and the docs pages (en and ja) in line with the
current header:

- Fix the client sample that does not compile, the Transfer-Encoding
  statement for default headers, the verifier callback comment, the
  thread pool spawn rule, the sanitize_filename description, and the
  inverted Accept-Encoding sentence.
- Describe recent behaviour: TLS errors behind Read/Write failures,
  Host validation, Connection: close for a body without length,
  content_reader rules, refused CONNECT, WebSocketClient proxies,
  CPPHTTPLIB_INTERIM_RESPONSE_MAX_COUNT, the SSE event size limit, and
  the payload limit of the Stream API.
- Complete the API references of WebSocket, WebSocketClient and the
  stream:: helpers, and the SSE reconnect rules.
- Fix cookbook pages: keychain loading on every backend, received Close
  status, a multipart sample that trusted the client's file name, an
  echo sample that sent binary as text, SNI, pre-request handler scope,
  stop() with WebSocket handlers, and the thread pool notes.
- Restore the mutex in the LLM app's Chapter 5 code.
2026-10-18 10:13:57 -04:00

2.2 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() and listen() 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 on read() keeps running as long as its peer stays connected, and listen() doesn't return until it ends. Keep track of open WebSocket connections and close() them on shutdown, e.g. with CloseStatus::GoingAway.