@@ -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+
46904835TEST (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