MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / runtime_worker_disconnect

Function runtime_worker_disconnect

src/daemon/runtime.c:1355–1429  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1353}
1354
1355static void runtime_worker_disconnect(cbm_daemon_runtime_worker_t *worker) {
1356 cbm_daemon_runtime_service_t *service = worker->service;
1357 cbm_daemon_client_id_t client_id = CBM_DAEMON_CLIENT_ID_INVALID;
1358 uint64_t shutdown_deadline = runtime_deadline_after(service->shutdown_timeout_ms);
1359 bool last_committed_left = false;
1360 bool linger_armed = false;
1361 atomic_store_explicit(&worker->disconnecting, true, memory_order_release);
1362 cbm_mutex_lock(&service->mutex);
1363 if (worker->admitted) {
1364 client_id = worker->client_id;
1365 worker->admitted = false;
1366 }
1367 if (worker->admission_committed) {
1368 worker->admission_committed = false;
1369 if (service->committed_clients > 0) {
1370 service->committed_clients--;
1371 }
1372 if (service->committed_clients == 0 && !service->permanent) {
1373 /* A HELLO whose application session is still opening is only a
1374 * provisional coordinator client. It cannot keep the generation
1375 * alive after the final fully committed frontend disconnects.
1376 * A permanent generation (`daemon start`) deliberately survives
1377 * this: only the stop/drain ops or a process kill end it. */
1378 last_committed_left = true;
1379 /* Cold-storm race (2026-09): if another connection is already
1380 * accepted and still mid-HELLO (not yet admitted) when the last
1381 * committed client leaves, it is a one-shot peer racing to commit —
1382 * retiring now strands it on a STOPPING daemon and forces a full
1383 * cold respawn ("cold-storm client failed (racing daemon spawn)").
1384 * Linger a bounded window for it; the accept loop retires the
1385 * generation once that racer drains without committing or the window
1386 * elapses, and a racer that does commit clears the linger
1387 * (runtime_worker_commit_admission). This deliberately lingers ONLY
1388 * for a pre-HELLO racer, never for an already-admitted provisional
1389 * session (HELLO done, app session opening): the contract that a
1390 * provisional coordinator client cannot keep a retiring generation
1391 * alive is preserved (the reject-blocked-provisional-session guard).
1392 * With no such racer — the common single-client case — retire
1393 * immediately, unchanged. (A cross-process cohort peer count is
1394 * unavailable: advisory locks expose presence, not a holder count,
1395 * and Windows LockFileEx has no non-owning probe.) */
1396 if (runtime_has_pending_hello_peer_locked(service, worker)) {
1397 service->ephemeral_linger_deadline_ms =
1398 runtime_deadline_after(runtime_ephemeral_linger_timeout_ms());
1399 linger_armed = true;
1400 } else {
1401 service->ephemeral_linger_deadline_ms = 0;
1402 runtime_service_begin_stopping_locked(service, shutdown_deadline, false,
1403 "last_committed_client_disconnected");
1404 }
1405 }
1406 }
1407 cbm_mutex_unlock(&service->mutex);
1408 if (client_id == CBM_DAEMON_CLIENT_ID_INVALID) {
1409 return;
1410 }
1411 /* Set the coordinator hold BEFORE releasing this client so its last-client
1412 * self-transition to STOPPING is suppressed while a racing peer is still

Tested by

no test coverage detected