@@ -74,6 +74,116 @@ TEST(checkpoint_does_not_truncate_wal) {
7474 PASS ();
7575}
7676
77+
78+ /* #897: any code path installing a fresh DB file must delete the
79+ * destination's -wal/-shm first. SQLite decides whether to replay a WAL
80+ * purely from the sidecar's own header/checksums — a leftover WAL from a
81+ * crashed previous session is recovered ON TOP of the freshly installed
82+ * file at the next open, splicing old-generation pages into it (short
83+ * indexes, btreeInitPage failures, or resurrected stale rows).
84+ *
85+ * Repro (per the issue): hot-copy a live WAL aside, close cleanly, restore
86+ * the copy as the crashed-session leftover, install a fresh generation via
87+ * cbm_store_dump_to_file, reopen — the stale generation's row must NOT be
88+ * visible and the fresh row must be. */
89+ static int tsc_copy_file (const char * src , const char * dst ) {
90+ FILE * in = fopen (src , "rb" );
91+ if (!in ) {
92+ return -1 ;
93+ }
94+ FILE * out = fopen (dst , "wb" );
95+ if (!out ) {
96+ (void )fclose (in );
97+ return -1 ;
98+ }
99+ char buf [4096 ];
100+ size_t n ;
101+ while ((n = fread (buf , 1 , sizeof (buf ), in )) > 0 ) {
102+ if (fwrite (buf , 1 , n , out ) != n ) {
103+ (void )fclose (in );
104+ (void )fclose (out );
105+ return -1 ;
106+ }
107+ }
108+ (void )fclose (in );
109+ (void )fclose (out );
110+ return 0 ;
111+ }
112+
113+ TEST (dump_install_ignores_stale_wal_sidecar ) {
114+ char * td = th_mktempdir ("cbm_stalewal" );
115+ char db_path [512 ];
116+ char wal_path [512 ];
117+ char stale_copy [512 ];
118+ snprintf (db_path , sizeof (db_path ), "%s/gen.db" , td );
119+ snprintf (wal_path , sizeof (wal_path ), "%s-wal" , db_path );
120+ snprintf (stale_copy , sizeof (stale_copy ), "%s/stale.wal" , td );
121+
122+ /* Generation 1: file-backed store with a marker row living in the WAL. */
123+ cbm_store_t * s1 = cbm_store_open_path (db_path );
124+ ASSERT_NOT_NULL (s1 );
125+ cbm_store_upsert_project (s1 , "walgen" , "/tmp/walgen" );
126+ cbm_node_t stale = {.project = "walgen" ,
127+ .label = "Function" ,
128+ .name = "stale_gen_node" ,
129+ .qualified_name = "walgen.mod.stale_gen_node" ,
130+ .file_path = "mod.py" ,
131+ .start_line = 1 ,
132+ .end_line = 2 };
133+ ASSERT_TRUE (cbm_store_upsert_node (s1 , & stale ) > 0 );
134+
135+ /* Hot-copy the live WAL (must be non-empty or the repro is vacuous). */
136+ struct stat st_wal = {0 };
137+ ASSERT_EQ (stat (wal_path , & st_wal ), 0 );
138+ ASSERT_TRUE (st_wal .st_size > 0 );
139+ ASSERT_EQ (tsc_copy_file (wal_path , stale_copy ), 0 );
140+ cbm_store_close (s1 ); /* clean close checkpoints + removes the WAL */
141+
142+ /* Simulate the crashed previous session's leftover sidecar. */
143+ ASSERT_EQ (tsc_copy_file (stale_copy , wal_path ), 0 );
144+
145+ /* Generation 2: fresh store installed over db_path. */
146+ cbm_store_t * s2 = cbm_store_open_memory ();
147+ ASSERT_NOT_NULL (s2 );
148+ cbm_store_upsert_project (s2 , "walgen" , "/tmp/walgen" );
149+ cbm_node_t fresh = {.project = "walgen" ,
150+ .label = "Function" ,
151+ .name = "fresh_gen_node" ,
152+ .qualified_name = "walgen.mod.fresh_gen_node" ,
153+ .file_path = "mod.py" ,
154+ .start_line = 1 ,
155+ .end_line = 2 };
156+ ASSERT_TRUE (cbm_store_upsert_node (s2 , & fresh ) > 0 );
157+ ASSERT_EQ (cbm_store_dump_to_file (s2 , db_path ), CBM_STORE_OK );
158+ cbm_store_close (s2 );
159+
160+ /* Reader: the stale WAL must not have been replayed onto gen 2. */
161+ cbm_store_t * s3 = cbm_store_open_path (db_path );
162+ ASSERT_NOT_NULL (s3 );
163+ cbm_node_t * hits = NULL ;
164+ int hit_count = 0 ;
165+ ASSERT_EQ (cbm_store_find_nodes_by_name (s3 , "walgen" , "fresh_gen_node" , & hits , & hit_count ),
166+ CBM_STORE_OK );
167+ ASSERT_TRUE (hit_count >= 1 );
168+ cbm_store_free_nodes (hits , hit_count );
169+ hits = NULL ;
170+ hit_count = 0 ;
171+ ASSERT_EQ (cbm_store_find_nodes_by_name (s3 , "walgen" , "stale_gen_node" , & hits , & hit_count ),
172+ CBM_STORE_OK );
173+ ASSERT_EQ (hit_count , 0 );
174+ cbm_store_free_nodes (hits , hit_count );
175+ cbm_store_close (s3 );
176+
177+ unlink (wal_path );
178+ char shm_path [512 ];
179+ snprintf (shm_path , sizeof (shm_path ), "%s-shm" , db_path );
180+ unlink (shm_path );
181+ unlink (db_path );
182+ unlink (stale_copy );
183+ PASS ();
184+ }
185+
77186SUITE (store_checkpoint ) {
78187 RUN_TEST (checkpoint_does_not_truncate_wal );
188+ RUN_TEST (dump_install_ignores_stale_wal_sidecar );
79189}
0 commit comments