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

Function runtime_worker_handle_stop

src/daemon/runtime.c:1518–1592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1516}
1517
1518static void runtime_worker_handle_stop(cbm_daemon_runtime_worker_t *worker, const uint8_t *payload,
1519 uint32_t length) {
1520 cbm_daemon_runtime_service_t *service = worker->service;
1521 char requested_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = {0};
1522 bool peer_verified =
1523 runtime_control_request_decode(payload, length, requested_build) &&
1524 runtime_activation_peer_matches_claim(service, worker->peer_process_id, requested_build);
1525 uint8_t response[CBM_DAEMON_STOP_RESPONSE_SIZE];
1526 memset(response, 0, sizeof(response));
1527 bool accepted = false;
1528 if (peer_verified) {
1529 uint8_t count = 0U;
1530 uint32_t pids[CBM_DAEMON_CONTROL_CLIENT_CAP] = {0};
1531 uint64_t deadline = runtime_deadline_after(service->shutdown_timeout_ms);
1532 cbm_mutex_lock(&service->mutex);
1533 response[0] = 1U;
1534 uint16_t committed = service->committed_clients > UINT16_MAX
1535 ? UINT16_MAX
1536 : (uint16_t)service->committed_clients;
1537 runtime_control_collect_clients_locked(service, worker, &count, pids);
1538 if (service->committed_clients > 0) {
1539 /* Refuse-if-busy: the caller reports these pids to the user as
1540 * the processes that must exit before a graceful stop. */
1541 response[1] = 0x02U;
1542 } else if (service->state == CBM_DAEMON_RUNTIME_SERVICE_RUNNING) {
1543 /* Explicit stop is the sanctioned exit for PERMANENT generations
1544 * as well — begin_stopping here overrides the permanence gate.
1545 * Mirror the activation-shutdown ACK protocol: the stopping
1546 * teardown must not consume this requester's response slot before
1547 * the acceptance byte leaves. */
1548 runtime_service_begin_stopping_locked(service, deadline, false,
1549 "daemon_stop_requested");
1550 /* Same exit criterion as an activation drain: a permanent
1551 * coordinator never reports client-terminal, so resource
1552 * emptiness must be allowed to complete this stop promptly. */
1553 service->activation_shutdown_requested = true;
1554 service->activation_response_inflight = true;
1555 worker->final_response_inflight = true;
1556 response[1] = 0x01U;
1557 accepted = true;
1558 }
1559 response[2] = (uint8_t)(committed >> 8);
1560 response[3] = (uint8_t)(committed & 0xFFU);
1561 response[4] = count;
1562 for (size_t index = 0; index < CBM_DAEMON_CONTROL_CLIENT_CAP; index++) {
1563 size_t offset = 8U + index * 4U;
1564 response[offset] = (uint8_t)(pids[index] >> 24);
1565 response[offset + 1U] = (uint8_t)(pids[index] >> 16);
1566 response[offset + 2U] = (uint8_t)(pids[index] >> 8);
1567 response[offset + 3U] = (uint8_t)(pids[index] & 0xFFU);
1568 }
1569 cbm_mutex_unlock(&service->mutex);
1570 }
1571 if (accepted) {
1572 char requester_pid[32];
1573 (void)snprintf(requester_pid, sizeof(requester_pid), "%llu",
1574 (unsigned long long)worker->peer_process_id);
1575 cbm_log_info("daemon.stop_requested", "requester_pid", requester_pid, "requester_build",

Callers 1

Tested by

no test coverage detected