Skip to content

Commit 30b4def

Browse files
committed
fix(main): guard the seam CALL SITES instead of stubbing the seams
cppcheck blocked the release run, correctly: src/main.c:283: Condition '!main_test_worker_project_lock_marker(mutation)' is always false src/main.c:2049: Condition '!worker_start_watchdog_test_descendant()' is always false Compiling the seams out left behind stubs that returned true unconditionally, so negating them was provably constant. Refactored rather than suppressed (repo rule): the stubs are deleted and the CALL SITES are guarded, so a release build does not contain the call at all -- which is what "the seam is not in the artifact" should have meant in the first place. The worker path needed care. The probe was one term in a five-term `||` chain, and its POSITION is load-bearing: it must fork AFTER the isolated process group exists (it inherits that group) and BEFORE the parent-death watchdog thread starts, because forking a multithreaded process is the bug that ordering avoids. Hoisting the call out of the chain would compile, pass tests, and silently break that invariant. So the chain is split into three ordered steps -- process group, then (test builds only) the probe, then the watchdog -- sharing one worker_containment_unavailable() failure path so every step still fails identically, with write()/_exit() rather than stdio and atexit handlers. Verified in BOTH directions, since a guard that is always off is as wrong as one always on: a TEST_SEAMS=1 build still contains the probe and tests/test_worker_watchdog.sh passes against it, while the release build carries no seam string. `make -f Makefile.cbm lint-ci` now passes locally -- the step skipped before the last push, and the reason CI found this instead of me. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 72bcc34 commit 30b4def

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

‎src/main.c‎

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,6 @@ static bool main_test_worker_project_lock_marker(const main_local_cli_mutation_t
249249
return close(marker) == 0 && written;
250250
#endif
251251
}
252-
#else
253-
static bool main_test_worker_project_lock_marker(const main_local_cli_mutation_t *mutation) {
254-
(void)mutation;
255-
return true;
256-
}
257252
#endif
258253

259254
static bool main_local_cli_mutation_begin(void *context, const char *project) {
@@ -280,13 +275,15 @@ static bool main_local_cli_mutation_begin(void *context, const char *project) {
280275
held->lease = lease;
281276
held->next = mutation->leases;
282277
mutation->leases = held;
278+
#ifdef CBM_ENABLE_TEST_SEAMS
283279
if (!main_test_worker_project_lock_marker(mutation)) {
284280
mutation->leases = held->next;
285281
main_project_lock_release_fully(&held->lease);
286282
free(held->project);
287283
free(held);
288284
return false;
289285
}
286+
#endif
290287
return true;
291288
}
292289
main_project_lock_release_fully(&lease);
@@ -405,6 +402,20 @@ static bool worker_prepare_process_group(void) {
405402
return (setpgid(0, 0) == 0 || getpgrp() == process_id) && getpgrp() == process_id;
406403
}
407404

405+
/* A worker that cannot contain its own process tree must not index: on failure
406+
* it would keep running as an orphan with no supervisor able to reap it. Shared
407+
* by the ordered containment steps in the worker path so all of them fail
408+
* identically — write() and _exit() rather than fprintf/exit, because this runs
409+
* after fork-sensitive setup and must not touch stdio locks or atexit handlers.
410+
*/
411+
static void worker_containment_unavailable(void) {
412+
static const char message[] =
413+
"CBM index worker could not start: process-tree containment unavailable\n";
414+
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
415+
(void)kill(-getpid(), SIGKILL);
416+
_exit(EXIT_FAILURE);
417+
}
418+
408419
/* Test-only crash-orphan probe used by tests/test_worker_watchdog.sh. It is
409420
* created before the watchdog thread so fork never occurs in a multithreaded
410421
* worker, and inherits the worker's isolated process group.
@@ -457,10 +468,6 @@ static bool worker_start_watchdog_test_descendant(void) {
457468
}
458469
return written;
459470
}
460-
#else
461-
static bool worker_start_watchdog_test_descendant(void) {
462-
return true;
463-
}
464471
#endif
465472

466473
static bool worker_start_parent_watchdog(pid_t initial_ppid) {
@@ -2045,14 +2052,28 @@ int main(int argc, char **argv) {
20452052
invocation.marker_file, invocation.quarantine_file,
20462053
invocation.memory_budget_bytes);
20472054
#ifndef _WIN32
2055+
/* Split into three ordered steps rather than one condition, because the
2056+
* ORDER is load-bearing and the middle step only exists in test builds:
2057+
* 1. establish the isolated process group,
2058+
* 2. (test builds) start the crash-orphan probe, which must inherit
2059+
* that group and must fork BEFORE the watchdog thread exists —
2060+
* forking a multithreaded process is the bug this ordering avoids,
2061+
* 3. start the parent-death watchdog thread.
2062+
* Keeping the probe inside a single `||` chain made its call
2063+
* unconditional in the source, so with the seam compiled out cppcheck
2064+
* correctly reported `!probe()` as always false. Guarding the STEP, not
2065+
* stubbing the function, means release builds simply do not have it. */
20482066
if (!worker_prepare_process_group() || process_initial_ppid <= 1 ||
2049-
getppid() != process_initial_ppid || !worker_start_watchdog_test_descendant() ||
2050-
!worker_start_parent_watchdog(process_initial_ppid)) {
2051-
static const char message[] =
2052-
"CBM index worker could not start: process-tree containment unavailable\n";
2053-
(void)write(STDERR_FILENO, message, sizeof(message) - 1);
2054-
(void)kill(-getpid(), SIGKILL);
2055-
_exit(EXIT_FAILURE);
2067+
getppid() != process_initial_ppid) {
2068+
worker_containment_unavailable();
2069+
}
2070+
#ifdef CBM_ENABLE_TEST_SEAMS
2071+
if (!worker_start_watchdog_test_descendant()) {
2072+
worker_containment_unavailable();
2073+
}
2074+
#endif
2075+
if (!worker_start_parent_watchdog(process_initial_ppid)) {
2076+
worker_containment_unavailable();
20562077
}
20572078
#endif
20582079
cbm_index_supervisor_mark_host();

0 commit comments

Comments
 (0)