Skip to content

Commit b0fe8ae

Browse files
authored
Merge pull request DeusData#2376 from DeusData/test/watcher-pending-free-held-state
test(watcher): pin the pending-free drain on a held state, not a git probe
2 parents 5df8b04 + 07eb426 commit b0fe8ae

3 files changed

Lines changed: 71 additions & 17 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: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,21 +2121,34 @@ TEST(watcher_sustained_failure_logs_once_issue2015) {
21212121
failing_index_calls = 0;
21222122
failing_index_fail_first_n = 100; /* never succeeds */
21232123

2124-
cbm_watcher_poll_once(w); /* baseline */
2124+
/* Baseline poll. Its outcome is not asserted: if a git probe fails here,
2125+
* the first loop poll takes the baseline instead. */
2126+
cbm_watcher_poll_once(w);
21252127

2128+
/* The change is a DIRTY worktree, not a commit. The baseline leaves the
2129+
* dirty signature at "clean known", so this change is seen whenever the
2130+
* baseline lands. A commit made before a late (or HEAD-less) baseline
2131+
* would be adopted as the baseline HEAD and never reindexed. */
21262132
{
21272133
char p[300];
21282134
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "world\n");
21292135
}
2130-
wt_git(tmpdir, "add file.txt");
2131-
wt_git(tmpdir, "commit -q -m add-world");
21322136

21332137
sustained_log_hits = 0;
21342138
cbm_log_set_sink(sustained_log_sink);
21352139

2136-
/* Drive well past the threshold (10). touch clears the deadline each time
2137-
* so every iteration actually attempts a reindex. */
2138-
for (int i = 0; i < 14; i++) {
2140+
/* Drive well past the threshold (10): poll until the 14th failed reindex.
2141+
* A poll does not always reach the index callback: when one of its git
2142+
* probes fails transiently (spawn refused under runner load) it is a
2143+
* fail-closed no-op by design (#937) and the next poll retries. So the
2144+
* loop waits for the attempt count instead of assuming one per poll;
2145+
* touch clears the backoff deadline each time. Each poll runs at most one
2146+
* attempt for this single project, so the loop stops at exactly 14. A
2147+
* watcher that never reindexes hangs here, which the runner reports as a
2148+
* failure, never a pass. No-op polls cannot add log lines to the count:
2149+
* sustained_failure is emitted only on a failed index attempt, when the
2150+
* streak EQUALS the threshold. */
2151+
while (failing_index_calls < 14) {
21392152
cbm_watcher_touch(w, "sust-repo");
21402153
cbm_watcher_poll_once(w);
21412154
}
@@ -3361,8 +3374,22 @@ TEST(watcher_callback_data_passed) {
33613374
}
33623375

33633376
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. */
3377+
/* Contract: unwatch parks the project's state on the deferred-free list
3378+
* (an in-flight poll snapshot may still hold it), and the NEXT poll_once
3379+
* frees it, exactly once, without admitting index work for the unwatched
3380+
* project.
3381+
*
3382+
* No assertion here depends on a git probe succeeding. The earlier
3383+
* version asserted as a precondition that one dirty poll reindexed
3384+
* exactly once, which needs every git subprocess of that poll to
3385+
* succeed. A transient probe failure (spawn refused under runner load, a
3386+
* non-zero exit) makes that poll a fail-closed no-op by design (#937:
3387+
* the baseline stays uncommitted and the next poll retries), so the
3388+
* precondition read 0 on loaded macos-14 runners. Holding a spawn
3389+
* failure with cbm_subprocess_force_spawn_eagain_for_testing reproduces
3390+
* that CI log exactly. Dirty-poll-reindexes stays covered by
3391+
* watcher_detects_dirty_worktree and watcher_modify_tracked_file; this
3392+
* test pins the drain itself, through the pending-free seam. */
33663393
char tmpdir[256];
33673394
snprintf(tmpdir, sizeof(tmpdir), "/tmp/cbm_watcher_df_XXXXXX");
33683395
if (!cbm_mkdtemp(tmpdir))
@@ -3383,28 +3410,35 @@ TEST(watcher_unwatch_drains_pending_free) {
33833410
cbm_watcher_t *w = cbm_watcher_new(store, index_callback, NULL);
33843411
cbm_watcher_watch(w, "df-repo", tmpdir);
33853412
ASSERT_EQ(cbm_watcher_watch_count(w), 1);
3413+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
33863414
index_call_count = 0;
33873415

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

3392-
/* Make dirty + detect change */
3420+
/* Leave a new dirty state behind: if the unwatched state were still
3421+
* polled, the next poll would index it. */
33933422
{
33943423
char p[300];
33953424
th_append_file(wt_path(p, sizeof(p), tmpdir, "file.txt"), "dirty\n");
33963425
}
33973426
cbm_watcher_touch(w, "df-repo");
3398-
cbm_watcher_poll_once(w);
3399-
ASSERT_EQ(index_call_count, 1);
34003427

3401-
/* Unwatch — state moves to pending_free */
3428+
/* Unwatch: the state leaves the table and is parked, not freed. */
34023429
cbm_watcher_unwatch(w, "df-repo");
34033430
ASSERT_EQ(cbm_watcher_watch_count(w), 0);
3431+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 1);
34043432

3405-
/* Next poll drains pending_free — no crash, no double-free */
3406-
cbm_watcher_poll_once(w);
3407-
ASSERT_EQ(index_call_count, 1);
3433+
/* The next poll drains it and admits no index work. */
3434+
ASSERT_EQ(cbm_watcher_poll_once(w), 0);
3435+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
3436+
ASSERT_EQ(index_call_count, 0);
3437+
3438+
/* A further poll finds nothing left to free (no double free). */
3439+
ASSERT_EQ(cbm_watcher_poll_once(w), 0);
3440+
ASSERT_EQ(cbm_watcher_test_pending_free_count(w), 0);
3441+
ASSERT_EQ(index_call_count, 0);
34083442

34093443
cbm_watcher_free(w);
34103444
cbm_store_close(store);

0 commit comments

Comments
 (0)