@@ -26,7 +26,7 @@ for (const status of ["enriching", "published"]) {
2626 // still match that snapshot when the guarded locking query runs.
2727 assert . match ( sql , / c a n d i d a t e .s t a t u s = e x p e c t e d .s t a t u s / ) ;
2828 assert . match ( sql , / c a n d i d a t e .d u p l i c a t e _ r e a s o n i s n o t d i s t i n c t f r o m e x p e c t e d .d u p l i c a t e _ r e a s o n / ) ;
29- assert . match ( sql , / f o r u p d a t e o f c a n d i d a t e / ) ;
29+ assert . match ( sql , / f o r n o k e y u p d a t e o f c a n d i d a t e / ) ;
3030 const expected = JSON . parse ( params [ 0 ] ) ;
3131 const current = expected . map ( ( row ) => ( { ...row , status } ) ) ;
3232 return { rows : current . filter ( ( row ) => expected . some ( ( old ) => old . id === row . id && old . status === row . status ) ) } ;
@@ -40,7 +40,7 @@ for (const status of ["enriching", "published"]) {
4040test ( "unchanged dedupe targets still move forward while changed targets are skipped" , async ( ) => {
4141 const writes = [ ] ;
4242 const client = { async query ( sql , params ) {
43- if ( sql . includes ( "for update of candidate" ) ) return { rows : [ { id : id ( 4 ) } ] } ;
43+ if ( sql . includes ( "for no key update of candidate" ) ) return { rows : [ { id : id ( 4 ) } ] } ;
4444 writes . push ( params ) ;
4545 assert . match ( sql , / s e t s t a t u s = ' q u e u e d ' / ) ;
4646 return { rows : [ ] } ;
@@ -81,3 +81,31 @@ test("database: a manual claim between dedupe read and write survives all stale
8181 await client . end ( ) ;
8282 }
8383} ) ;
84+
85+ // A claim must not block dedupe's foreign-key check on the row it holds. Otherwise a
86+ // claim waiting on dedupe's next row and dedupe waiting on the claimed row would deadlock.
87+ test ( "database: a claimed candidate can still be referenced by a concurrent dedupe write" , {
88+ skip : process . env . SELF_HOST_APPS_TEST_ENVIRONMENT !== "development" ,
89+ } , async ( ) => {
90+ const connect = ( ) => new pg . Client ( { connectionString : resolveDatabaseUrl ( { environment : "development" } ) , connectionTimeoutMillis : 15000 , query_timeout : 30000 } ) ;
91+ const claimer = connect ( ) ;
92+ const deduper = connect ( ) ;
93+ try {
94+ await claimer . connect ( ) ;
95+ await deduper . connect ( ) ;
96+ const { rows } = await claimer . query ( "select id from public.self_host_app_candidates where status = 'queued' order by id limit 2" ) ;
97+ assert . equal ( rows . length , 2 , "needs two queued development candidates" ) ;
98+ const [ claimed , referencing ] = rows . map ( ( row ) => row . id ) ;
99+ await claimer . query ( "begin" ) ;
100+ await claimIds ( claimer , [ claimed ] , false ) ;
101+ await deduper . query ( "begin" ) ;
102+ await deduper . query ( "set local lock_timeout = '3s'" ) ;
103+ // Rolled back below. A blocking lock would fail here with a lock timeout.
104+ await deduper . query ( "update public.self_host_app_candidates set duplicate_candidate_id = $1 where id = $2" , [ claimed , referencing ] ) ;
105+ } finally {
106+ await deduper . query ( "rollback" ) . catch ( ( ) => { } ) ;
107+ await claimer . query ( "rollback" ) . catch ( ( ) => { } ) ;
108+ await deduper . end ( ) . catch ( ( ) => { } ) ;
109+ await claimer . end ( ) . catch ( ( ) => { } ) ;
110+ }
111+ } ) ;
0 commit comments