C Collections

chttpserver · Section 3

chttpsvr_engine_wait

chttpsvr_engine_wait - wait for the engine thread to exit

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
void chttpsvr_engine_wait(void);

Blocks the calling thread until the shared reactor of chttpserver
has stopped completely, whether the stop started before this call,
at the same time or some time later.  If nothing ever stops the
reactor, the call blocks forever.  The stop may come, for example,
from an external SIGTERM handler that calls chttpsvr_engine_stop()
at some unknown later time.
 
When the last reference to the reactor goes, which happens when you
destroy the last chttpsvr with chttpsvr_destroy, an asynchronous
teardown starts, and it doesn't block the caller that started it.
This function is what a program calls when it has to synchronize
with that shutdown, or with a chttpsvr_engine_stop() that came from
an external signal.  The reactor is separate from the engine of
chttpclient, so this function has nothing to do with the async work
of chttpclient in the same process.
 
More than one thread may call it at the same time.  Each caller
blocks until the library confirms that the reactor has stopped
completely, whichever caller started the stop and whether it started
through a release or through chttpsvr_engine_stop().
 
Do not call it from a request handler or a middleware that runs on
the worker pool of ANY chttpsvr instance.  That is a FATAL error: The
library calls ccol__fatal_err(), which means abort() and SIGABRT, and
reports it at once instead of hanging.  The worker pool of that
server can never finish draining while the request in flight is
blocked here waiting for the engine to exit, and that would deadlock
the shutdown of the whole shared engine, not just of one server.  An
admin or shutdown endpoint that wants a full drain before it answers
calls chttpsvr_engine_stop() and returns, and a separate thread calls
this function.
 
Once this function returns, the fallback engine logger is closed.  A
logger installed with chttpsvr_set_engine_logger stays in place, and
the engine that a later chttpsvr_start brings up logs through it
again.
 
Calling it before the reactor has ever started is safe and does
nothing.
 
On OpenBSD, do not call it from an exit handler that a constructor
registered with atexit(3).  OpenBSD runs such a handler after the
dynamic loader has taken a lock that it holds until the process ends,
and the teardown of the reactor joins threads, which needs that lock,
so the call never returns.  An exit handler registered by main() is
not affected.

chttpsvr_engine_stop(3), chttpsvr_start(3), chttpsvr_set_engine_logger(3)