Gracefully winds down libprocess in roughly the reverse order of initialization. NOTE: `finalize_wsa` controls whether libprocess also finalizes the Windows socket stack, which affects the entire process.
| 1259 | // NOTE: `finalize_wsa` controls whether libprocess also finalizes |
| 1260 | // the Windows socket stack, which affects the entire process. |
| 1261 | void finalize(bool finalize_wsa) |
| 1262 | { |
| 1263 | // The clock is only paused during tests. Pausing may lead to infinite |
| 1264 | // waits during clean up, so we make sure the clock is running normally. |
| 1265 | Clock::resume(); |
| 1266 | |
| 1267 | // This will terminate the underlying process for the `Route`. |
| 1268 | delete processes_route; |
| 1269 | processes_route = nullptr; |
| 1270 | |
| 1271 | // Close the server socket. |
| 1272 | // This will prevent any further connections managed by the `SocketManager`. |
| 1273 | synchronized (socket_mutex) { |
| 1274 | // Explicitly terminate the callback loop used to accept incoming |
| 1275 | // connections. This is necessary as the server socket ignores |
| 1276 | // most errors, including when the server socket has been closed. |
| 1277 | future_accept.discard(); |
| 1278 | |
| 1279 | delete __s__; |
| 1280 | __s__ = nullptr; |
| 1281 | } |
| 1282 | |
| 1283 | // Terminate all running processes and prevent further processes from |
| 1284 | // being spawned. This will also clean up any metadata for running |
| 1285 | // processes held by the `SocketManager`. After this method returns, |
| 1286 | // libprocess should be single-threaded. |
| 1287 | process_manager->finalize(); |
| 1288 | |
| 1289 | // Now that all threads except for the main thread have joined, we should |
| 1290 | // delete the one remaining `_executor_` pointer. |
| 1291 | delete _executor_; |
| 1292 | _executor_ = nullptr; |
| 1293 | |
| 1294 | // This clears any remaining timers. Because the event loop has been |
| 1295 | // stopped, no timers will fire. |
| 1296 | Clock::finalize(); |
| 1297 | |
| 1298 | // Clean up the socket manager. |
| 1299 | // Terminating processes above will also clean up any links between |
| 1300 | // processes (which may be expressed as open sockets) and the various |
| 1301 | // `HttpProxy` processes managing incoming HTTP requests. We cannot |
| 1302 | // delete the `SocketManager` yet, since the `ProcessManager` may |
| 1303 | // potentially dereference it. |
| 1304 | socket_manager->finalize(); |
| 1305 | |
| 1306 | // This is dereferenced inside `ProcessBase::consume(HttpEvent&&)`. |
| 1307 | // We can safely delete it since no further incoming HTTP connections |
| 1308 | // can be made because the server socket has been destroyed. This must |
| 1309 | // be deleted before the `ProcessManager` as it will indirectly |
| 1310 | // dereference the `ProcessManager`. |
| 1311 | delete authenticator_manager; |
| 1312 | authenticator_manager = nullptr; |
| 1313 | |
| 1314 | // At this point, there should be no running processes, no sockets, |
| 1315 | // and a single remaining thread. We can safely remove the global |
| 1316 | // `SocketManager` and `ProcessManager` pointers now. |
| 1317 | delete socket_manager; |
| 1318 | socket_manager = nullptr; |