Skip to content

Commit 74e301d

Browse files
committed
fix(daemon): end a permanent watch whose project was deleted
Under `daemon start` a watch now outlives its last session. delete_project from another session or the CLI removes the project's DB but meets no owner of the watch: daemon MCP sessions carry no watcher pointer, so nothing unwatches it, and the next change in that root re-indexed the deleted project. When a permanent watch has no subscribers, no job for its project is running and its DB is gone, the watcher callback now removes the watch and skips, the rule a session refresh already applies to a deleted project. The running-job guard is needed because a full reindex unlinks the DB while it rebuilds it: a second change during that rebuild must join the job, not end the watch. Session-owned watches are unchanged. Tests: daemon_application_permanent_watch_ends_when_project_db_is_deleted (RED before: the deleted project was re-indexed) and daemon_application_permanent_watch_kept_while_its_reindex_runs (RED without the running-job guard: the second change ended the watch). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent a728eeb commit 74e301d

2 files changed

Lines changed: 168 additions & 0 deletions

File tree

‎src/daemon/application.c‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4268,6 +4268,21 @@ static void application_watch_encode_index_args_locked(const cbm_daemon_applicat
42684268
*args = encoded ? yyjson_mut_write(document, 0, NULL) : NULL;
42694269
}
42704270

4271+
/* Under `daemon start` a watch outlives its last session, so delete_project
4272+
* from another session or the CLI can remove the project's DB while nothing
4273+
* owns the watch any more: daemon MCP sessions carry no watcher pointer and
4274+
* never unwatch it. Re-indexing such an orphan would resurrect the deleted
4275+
* project. A full reindex also unlinks the DB while it rebuilds it, so a
4276+
* missing DB means "deleted" only while no job for the project is running.
4277+
* Caller holds application->mutex. */
4278+
static bool application_permanent_watch_orphaned_locked(cbm_daemon_application_t *application,
4279+
const cbm_daemon_application_watch_t *watch,
4280+
const char *project_key) {
4281+
return application->permanent && watch->subscribers == 0 &&
4282+
!application_find_active_job_locked(application, project_key) &&
4283+
!application_regular_db_exists(watch->project);
4284+
}
4285+
42714286
static cbm_daemon_application_job_t *application_background_index_admit(
42724287
cbm_daemon_application_t *application, const char *project_name, const char *canonical_root,
42734288
const char *project_key, yyjson_mut_doc *document, yyjson_mut_val *root, char **args,
@@ -4281,6 +4296,12 @@ static cbm_daemon_application_job_t *application_background_index_admit(
42814296
}
42824297
cbm_daemon_application_watch_t *watch =
42834298
require_live_watch ? application_find_watch_locked(application, project_name) : NULL;
4299+
if (watch && application_permanent_watch_orphaned_locked(application, watch, project_key)) {
4300+
/* End the orphan the way a session refresh ends a deleted project's
4301+
* watch; the callback below becomes a harmless skip. */
4302+
application_remove_watch_locked(application, watch);
4303+
watch = NULL;
4304+
}
42844305
bool watch_live = !require_live_watch ||
42854306
(watch && application_watch_root_matches(watch->root, canonical_root) &&
42864307
(watch->subscribers > 0 || application->permanent));

‎tests/test_daemon_application.c‎

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4687,6 +4687,151 @@ TEST(daemon_application_permanent_watch_survives_disconnect_and_indexes_without_
46874687
PASS();
46884688
}
46894689

4690+
/* A permanent daemon keeps a project watched after its last session closes.
4691+
* delete_project from another session or the CLI removes the project's DB but
4692+
* owns no watch, so the watch itself must notice: a later change must end the
4693+
* orphaned watch, never re-create the deleted index. */
4694+
TEST(daemon_application_permanent_watch_ends_when_project_db_is_deleted) {
4695+
app_watch_race_fixture_t fixture;
4696+
bool fixture_ready = app_watch_race_fixture_init(&fixture, 48);
4697+
if (fixture_ready) {
4698+
cbm_daemon_application_set_permanent(fixture.application, true);
4699+
/* Should a job be admitted, it completes at once: the verdict is
4700+
* read from the start counter, never from a timeout. */
4701+
atomic_store(&fixture.fake.allow_completion, true);
4702+
}
4703+
if (fixture.session) {
4704+
fixture.callbacks.session_cancel(fixture.callbacks.context, fixture.session);
4705+
fixture.callbacks.session_close(fixture.callbacks.context, fixture.session);
4706+
fixture.session = NULL;
4707+
}
4708+
int watch_after_close = fixture_ready ? cbm_watcher_watch_count(fixture.watcher) : -1;
4709+
bool db_removed = fixture_ready && cbm_unlink(fixture.db_path) == 0;
4710+
int result = db_removed ? cbm_daemon_application_watcher_index(fixture.project, fixture.root,
4711+
fixture.application)
4712+
: -2;
4713+
int starts = fixture_ready ? atomic_load(&fixture.fake.starts) : -1;
4714+
int watch_after_index = fixture_ready ? cbm_watcher_watch_count(fixture.watcher) : -1;
4715+
bool cleaned = app_watch_race_fixture_finish(&fixture);
4716+
4717+
ASSERT_TRUE(fixture_ready);
4718+
ASSERT_EQ(watch_after_close, 1);
4719+
ASSERT_TRUE(db_removed);
4720+
/* A harmless skip: nothing admitted, the deleted index stays deleted. */
4721+
ASSERT_EQ(result, 1);
4722+
ASSERT_EQ(starts, 0);
4723+
/* The orphaned physical watch is torn down. */
4724+
ASSERT_EQ(watch_after_index, 0);
4725+
ASSERT_TRUE(cleaned);
4726+
PASS();
4727+
}
4728+
4729+
/* The second watcher callback either joins the running job (its retained
4730+
* ownership is the job's second subscriber) or returns. Both states latch, so
4731+
* whichever happens decides the wait; the deadline only catches "neither". */
4732+
static bool app_wait_for_watcher_join_or_return(cbm_daemon_application_t *application,
4733+
const char *project, atomic_bool *returned) {
4734+
uint64_t deadline = cbm_now_ms() + APP_TEST_TIMEOUT_MS;
4735+
while (cbm_now_ms() < deadline) {
4736+
if (cbm_daemon_application_job_subscribers(application, project) == 2 ||
4737+
atomic_load(returned)) {
4738+
return true;
4739+
}
4740+
cbm_usleep(1000);
4741+
}
4742+
return false;
4743+
}
4744+
4745+
/* A full reindex unlinks the project's DB while it rebuilds it, so a missing
4746+
* DB alone does not mean the project was deleted. While the permanent watch's
4747+
* own job runs, a second change in its root must join that job and keep the
4748+
* watch; once the job ends, both callbacks hand their ownership back. */
4749+
TEST(daemon_application_permanent_watch_kept_while_its_reindex_runs) {
4750+
app_watch_race_fixture_t fixture;
4751+
bool fixture_ready = app_watch_race_fixture_init(&fixture, 49);
4752+
if (fixture_ready) {
4753+
cbm_daemon_application_set_permanent(fixture.application, true);
4754+
}
4755+
if (fixture.session) {
4756+
fixture.callbacks.session_cancel(fixture.callbacks.context, fixture.session);
4757+
fixture.callbacks.session_close(fixture.callbacks.context, fixture.session);
4758+
fixture.session = NULL;
4759+
}
4760+
int watch_after_close = fixture_ready ? cbm_watcher_watch_count(fixture.watcher) : -1;
4761+
4762+
app_watcher_index_thread_t first = {
4763+
.application = fixture.application,
4764+
.project = fixture.project,
4765+
.root = fixture.root,
4766+
.pause_before_subscribe = false,
4767+
.result = -1,
4768+
};
4769+
atomic_init(&first.ready, false);
4770+
atomic_init(&first.proceed, true);
4771+
atomic_init(&first.done, false);
4772+
app_watcher_index_thread_t second = {
4773+
.application = fixture.application,
4774+
.project = fixture.project,
4775+
.root = fixture.root,
4776+
.pause_before_subscribe = false,
4777+
.result = -1,
4778+
};
4779+
atomic_init(&second.ready, false);
4780+
atomic_init(&second.proceed, true);
4781+
atomic_init(&second.done, false);
4782+
4783+
/* The fake worker keeps the first job running until allow_completion. */
4784+
cbm_thread_t first_thread;
4785+
bool first_started = fixture_ready && watch_after_close == 1 &&
4786+
cbm_thread_create(&first_thread, 0, app_watcher_index_thread, &first) == 0;
4787+
bool job_running = first_started && app_wait_for_atomic_int(&fixture.fake.starts, 1);
4788+
bool db_removed = job_running && cbm_unlink(fixture.db_path) == 0;
4789+
cbm_thread_t second_thread;
4790+
bool second_started =
4791+
db_removed && cbm_thread_create(&second_thread, 0, app_watcher_index_thread, &second) == 0;
4792+
bool settled = second_started && app_wait_for_watcher_join_or_return(
4793+
fixture.application, fixture.project, &second.done);
4794+
size_t subscribers_while_running =
4795+
fixture_ready ? cbm_daemon_application_job_subscribers(fixture.application, fixture.project)
4796+
: 0;
4797+
bool second_returned_while_running = atomic_load(&second.done);
4798+
int watch_while_running = fixture_ready ? cbm_watcher_watch_count(fixture.watcher) : -1;
4799+
4800+
if (fixture_ready) {
4801+
atomic_store(&fixture.fake.allow_completion, true);
4802+
}
4803+
bool first_joined = first_started && app_wait_for_atomic_bool(&first.done, true) &&
4804+
cbm_thread_join(&first_thread) == 0;
4805+
bool second_joined = second_started && app_wait_for_atomic_bool(&second.done, true) &&
4806+
cbm_thread_join(&second_thread) == 0;
4807+
int starts = fixture_ready ? atomic_load(&fixture.fake.starts) : -1;
4808+
size_t subscribers_after =
4809+
fixture_ready ? cbm_daemon_application_job_subscribers(fixture.application, fixture.project)
4810+
: SIZE_MAX;
4811+
int watch_after = fixture_ready ? cbm_watcher_watch_count(fixture.watcher) : -1;
4812+
bool cleaned = app_watch_race_fixture_finish(&fixture);
4813+
4814+
ASSERT_TRUE(fixture_ready);
4815+
ASSERT_EQ(watch_after_close, 1);
4816+
ASSERT_TRUE(job_running);
4817+
ASSERT_TRUE(db_removed);
4818+
ASSERT_TRUE(settled);
4819+
/* The second change joined the running job instead of ending the watch. */
4820+
ASSERT_EQ(subscribers_while_running, 2);
4821+
ASSERT_FALSE(second_returned_while_running);
4822+
ASSERT_EQ(watch_while_running, 1);
4823+
ASSERT_TRUE(first_joined);
4824+
ASSERT_TRUE(second_joined);
4825+
ASSERT_EQ(first.result, 0);
4826+
ASSERT_EQ(second.result, 0);
4827+
ASSERT_EQ(starts, 1);
4828+
/* Each callback released the ownership it retained at admission. */
4829+
ASSERT_EQ(subscribers_after, 0);
4830+
ASSERT_EQ(watch_after, 1);
4831+
ASSERT_TRUE(cleaned);
4832+
PASS();
4833+
}
4834+
46904835
TEST(daemon_application_late_watcher_session_owns_active_watcher_job) {
46914836
app_watch_race_fixture_t fixture;
46924837
bool fixture_ready = app_watch_race_fixture_init(&fixture, 45);
@@ -7793,6 +7938,8 @@ SUITE(daemon_application) {
77937938
RUN_TEST(daemon_application_final_cancel_drains_admitted_watcher_job);
77947939
RUN_TEST(daemon_application_watcher_job_follows_exact_live_watch_owners);
77957940
RUN_TEST(daemon_application_permanent_watch_survives_disconnect_and_indexes_without_session);
7941+
RUN_TEST(daemon_application_permanent_watch_ends_when_project_db_is_deleted);
7942+
RUN_TEST(daemon_application_permanent_watch_kept_while_its_reindex_runs);
77967943
RUN_TEST(daemon_application_late_watcher_session_owns_active_watcher_job);
77977944
RUN_TEST(daemon_application_watch_roots_match_windows_separators_only);
77987945
RUN_TEST(daemon_application_async_completion_disables_all_watches);

0 commit comments

Comments
 (0)