Termination Handling
Problem Statement
The current shutdown path is designed around a main server process that may register backend child
processes and propagate SIGTERM to them.
The main server PID receives
SIGTERMand flipsRunServertofalseAny child PIDs registered through
Server::addChildPID()are stored inServer::ChildPIDsServer::terminateChildren()sendsSIGTERMto those PIDs in reverse registration orderThe shutdown flow is safe even when no child processes were spawned
Solution
Implemented proper termination handling where the main server process tracks all child PIDs and propagates SIGTERM signals to them.
How It Works
Process Hierarchy
Main Server (root, then drops privileges)
└── ASProcessHandler children (optional, when backend workers are enabled)
Termination Flow
User sends
kill -TERM <main_pid>orpkill falcon-asMain server process receives SIGTERM
Signal handler
Server::terminate()is invokedServer::terminateChildren()sendsSIGTERMto all registered child PIDsEach child process receives
SIGTERMand its handler setsRunServer = falseAll active loops exit cleanly
If no children were registered, the main server simply exits its own loop
Key Design Decisions
Static Members:
ChildPIDsis static to be accessible from the signal handler contextGlobal Function:
registerChildPIDToServer()breaks the directServer/ASProcessHandlerdependencyPID Registration: Done in parent process immediately after fork succeeds
Signal Order: Children receive SIGTERM before parent exits
Testing
A comprehensive test suite validates:
Parent tracks child PIDs correctly
SIGTERM propagates from parent to registered children
The server exits cleanly without SIGKILL-only shutdown logic
Processes don’t terminate prematurely without SIGTERM
Benefits
Clean Shutdown: Server responds to standard SIGTERM signal
No Orphan Processes: All children are properly terminated
Resource Cleanup: Allows processes to clean up resources before exit
Standard Behavior: Follows Unix/Linux process management conventions
Systemd Compatible: Works correctly with systemd service management
Usage
# start server
./usr/local/bin/falcon-as
# stop server (now works correctly)
pkill falcon-as
# or
kill -TERM <pid>