chttpserver · Section 3
chttpsvr_destroy
NAME
chttpsvr_destroy - destroy a chttpsvr handle and set it to CHTTPSVR_NULL
LIBRARY
C Collections (libccollections, -lccollections)
SYNOPSIS
#include <ccollections/chttpserver.h> chttpsvr_destroy(name);
DESCRIPTION
Destroys the server that name holds and then sets name to CHTTPSVR_NULL. It drains every in-flight request and closes every idle connection that is left. Connections that wait without a thread are ended too: a parked body or response is closed, a request that waits for body memory is closed, and a streaming request that waits in the streaming queue gets 503 Service Unavailable. If this is the last active server, the shared engine is stopped as well. The listener socket is closed along the way, so you don't have to call chttpsvr_stop first. The destroy frees everything that the server allocated except for a small part, about 100 bytes, of each sub-router that chttpsvr_subrouter(3) created. That part stays until the process exits, so that a pointer to the sub-router is still safe to pass after the destroy. A server that a destructor or an atexit handler of the application destroys after the exit-time cleanup of the library has run frees that memory too, together with the bookkeeping of the library, and its engine closes the logger of chttpsvr_set_engine_logger(3) when it stops. Once the teardown starts, the server takes no new request. The connections that wait idle for their next request are closed. A request that arrives later, on a keep-alive connection or pipelined behind another request, runs no handler and gets 503 Service Unavailable with Connection: close. The response of a request whose handler is already running carries Connection: close and ends its connection. The wait for the in-flight requests therefore ends when the handlers that already run return, however many requests the clients keep sending. If handlers are still running after 30 s, the library closes the connections they read from or write to, so that a handler blocked on its client returns. A second call on another copy of the same handle value is a FATAL ERROR, whether it runs at the same time or later, after the first call has completed. The same holds for a call from inside a request handler or a middleware of this server. The library reports the error at once with abort() and SIGABRT instead of a silent double free or a hang. To shut a server down from inside one of its own handlers, destroy it from another thread or call chttpsvr_engine_stop(). The macro is safe to call whether or not the shared engine is still running. It frees the engine reference of name, and if that was the last reference an asynchronous engine stop can start. It is also safe when chttpsvr_engine_stop() has already forced the engine down while name was started: That engine stop has then already quiesced this server, so the call finds the teardown done and frees what is left of the server. If such a forced stop is still tearing this same server down on its own background thread, the call waits for that teardown to finish instead of racing it. This macro only STARTS that asynchronous engine stop. The destroy of the reactor, the stop of the idle sweep and the close of the fallback engine logger all run on a separate reaper thread after this call returns, and every guarantee of this call is about name alone: Its listener is closed, its connections are closed and its worker pool is drained. Call chttpsvr_engine_wait() after it whenever that final teardown has to finish before your next step. Always call it before you return from main() and before you unload this library from an image that dlopen() loaded. Call it as well before you tear down a custom allocator installed with chttpsvr_set_engine_mem_mgmt_procs(), because the reaper frees the memory of the reactor and of the fallback engine logger through that allocator. The reaper thread itself doesn't need such a call. An application that destroys its last server and then calls neither chttpsvr_engine_wait() nor a later chttpsvr_start() leaves no unjoined thread and no thread stack mapping behind.
NOTES
The macro evaluates name exactly once. name must be a modifiable lvalue, such as a variable or an array element, and with an expression that has a side effect, such as handles[i++], exactly one element is destroyed and cleared.
SEE ALSO
chttpsvr_stop(3), chttpsvr_engine_stop(3), chttpsvr_engine_wait(3)