Skip to content

Commit 5a9c72d

Browse files
committed
test(watcher): pin the pending-free drain on a held state, not a git probe
watcher_unwatch_drains_pending_free failed three times on macos-14 CI with `index_call_count == 0, expected 1` at its dirty-poll precondition. That precondition needs every git subprocess of one poll (rev-parse HEAD, status, submodule foreach) to succeed. A transient probe failure (spawn refused under runner load, a non-zero exit) makes that poll a fail-closed no-op by design: DeusData#937 leaves the baseline uncommitted and the next poll retries. Several of those paths return without a log line, which matches the CI output exactly: watch, baseline, FAIL, with no watcher.changed and no watcher.git.failed. Holding a spawn failure with cbm_subprocess_force_spawn_eagain_for_testing reproduces that output deterministically. This is a test race, not a production defect. The precondition was also unrelated to the contract under test. A poll that stopped draining would neither crash nor leak, because cbm_watcher_free frees the same deferred list. So the old test could not see a broken drain at all. Add a test-only seam, cbm_watcher_test_pending_free_count, and assert the stable states the watcher holds: 0 parked before unwatch, 1 after, 0 after the next poll with no index work, and still 0 after a further poll. No assertion depends on a git probe succeeding. Dirty-poll reindexing stays covered by watcher_detects_dirty_worktree and watcher_modify_tracked_file. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 64c23fa commit 5a9c72d

3 files changed

Lines changed: 52 additions & 11 deletions

File tree

‎src/watcher/watcher.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,15 @@ int cbm_watcher_run(cbm_watcher_t *w, int base_interval_ms) {
16531653
cbm_log_info("watcher.stop");
16541654
return 0;
16551655
}
1656+
1657+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
1658+
int cbm_watcher_test_pending_free_count(cbm_watcher_t *w) {
1659+
if (!w) {
1660+
return -1;
1661+
}
1662+
cbm_mutex_lock(&w->projects_lock);
1663+
int count = w->pending_free_count;
1664+
cbm_mutex_unlock(&w->projects_lock);
1665+
return count;
1666+
}
1667+
#endif

‎src/watcher/watcher.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ int cbm_watcher_watch_count(cbm_watcher_t *w);
110110
* without first giving the counter atomic accessors. */
111111
int cbm_watcher_index_failure_count(cbm_watcher_t *w, const char *project_name);
112112

113+
#if defined(CBM_ENABLE_TEST_SEAMS) && CBM_ENABLE_TEST_SEAMS
114+
/* Test seam: number of unwatched/replaced project states parked on the
115+
* deferred-free list, read under projects_lock. The drain in poll_once is
116+
* otherwise unobservable — cbm_watcher_free frees the same list, so a poll
117+
* that stopped draining would neither crash nor leak. Test builds only. */
118+
int cbm_watcher_test_pending_free_count(cbm_watcher_t *w);
119+
#endif
120+
113121
/* Return the adaptive poll interval (ms) for a given file count. */
114122
int cbm_watcher_poll_interval_ms(int file_count);
115123

‎tests/test_watcher.c‎

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,8 +3361,22 @@ TEST(watcher_callback_data_passed) {
33613361
}
33623362

33633363
TEST(watcher_unwatch_drains_pending_free) {
3364-
/* Unwatch moves project_state to pending_free; the next poll_once
3365-
* must drain it without crash or leak. */
3364+
/* Contract: unwatch parks the project's state on the deferred-free list
3365+
* (an in-flight poll snapshot may still hold it), and the NEXT poll_once
3366+
* frees it, exactly once, without admitting index work for the unwatched
3367+
* project.
3368+
*
3369+
* No assertion here depends on a git probe succeeding. The earlier
3370+
* version asserted as a precondition that one dirty poll reindexed
3371+
* exactly once, which needs every git subprocess of that poll to
3372+
* succeed. A transient probe failure (spawn refused under runner load, a
3373+
* non-zero exit) makes that poll a fail-closed no-op by design (#937:
3374+
* the baseline stays uncommitted and the next poll retries), so the
3375+
* precondition read 0 on loaded macos-14 runners. Holding a spawn
3376+
* failure with cbm_subprocess_force_spawn_eagain_for_testing reproduces
3377+
* that CI log exactly. Dirty-poll-reindexes stays covered by
3378+
* watcher_detects_dirty_worktree and watcher_modify_tracked_file; this
3379+
* test pins the drain itself, through the pending-free seam. */
33663380
char tmpdir[256];
33673381
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_df_XXXXXX");
33683382
if (!cbm_mkdtemp(tmpdir))
@@ -3383,28 +3397,35 @@ TEST(watcher_unwatch_drains_pending_free) {
33833397
cbm_watcher_t *w = cbm_watcher_new(store, index_callback, NULL);
33843398
cbm_watcher_watch(w, "df-repo", tmpdir);
33853399
ASSERT_EQ(cbm_watcher_watch_count(w), 1);
3400+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
33863401
index_call_count = 0;
33873402

3388-
/* Baseline */
3403+
/* Baseline: the state goes through a poll snapshot. Its outcome is not
3404+
* asserted; a baseline poll never indexes, whatever git answers. */
33893405
cbm_watcher_poll_once(w);
3390-
ASSERT_EQ(index_call_count, 0);
33913406

3392-
/* Make dirty + detect change */
3407+
/* Leave a new dirty state behind: if the unwatched state were still
3408+
* polled, the next poll would index it. */
33933409
{
33943410
char p[300];
33953411
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "dirty\n");
33963412
}
33973413
cbm_watcher_touch(w, "df-repo");
3398-
cbm_watcher_poll_once(w);
3399-
ASSERT_EQ(index_call_count, 1);
34003414

3401-
/* Unwatch — state moves to pending_free */
3415+
/* Unwatch: the state leaves the table and is parked, not freed. */
34023416
cbm_watcher_unwatch(w, "df-repo");
34033417
ASSERT_EQ(cbm_watcher_watch_count(w), 0);
3418+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 1);
34043419

3405-
/* Next poll drains pending_free — no crash, no double-free */
3406-
cbm_watcher_poll_once(w);
3407-
ASSERT_EQ(index_call_count, 1);
3420+
/* The next poll drains it and admits no index work. */
3421+
ASSERT_EQ(cbm_watcher_poll_once(w), 0);
3422+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
3423+
ASSERT_EQ(index_call_count, 0);
3424+
3425+
/* A further poll finds nothing left to free (no double free). */
3426+
ASSERT_EQ(cbm_watcher_poll_once(w), 0);
3427+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
3428+
ASSERT_EQ(index_call_count, 0);
34083429

34093430
cbm_watcher_free(w);
34103431
cbm_store_close(store);

0 commit comments

Comments
 (0)