@@ -49,40 +49,6 @@ async fn acquire_push_gate_lock(tx: &mut sqlx::PgConnection, community: Communit
4949 Ok ( ( ) )
5050}
5151
52- /// Recovery window for the activation backfill: recent events that the gate
53- /// legitimately skipped (no eligible lease at their commit time) are enqueued
54- /// when a lease activates, so a user who registers moments after a message
55- /// still gets woken. Product coverage only — the advisory-lock total order is
56- /// what makes the gate correct; see `PUSH_GATE_LOCK_NAMESPACE`.
57- const PUSH_GATE_BACKFILL_SECS : i64 = 120 ;
58-
59- /// Enqueue match jobs for recent gate-skipped events. MUST run inside the same
60- /// transaction that holds the exclusive push-gate lock: after this commit,
61- /// every event is either backfilled here or ordered after the activation and
62- /// enqueued by the trigger — running it post-commit would reopen the gap.
63- /// Keyed on relay `received_at` (not author-controlled `created_at`); the kind
64- /// list mirrors the trigger allowlist; `ON CONFLICT DO NOTHING` dedups against
65- /// rows the trigger already enqueued.
66- async fn backfill_push_match_jobs (
67- tx : & mut sqlx:: PgConnection ,
68- community : CommunityId ,
69- ) -> Result < ( ) > {
70- sqlx:: query (
71- "INSERT INTO push_match_queue (community_id, event_id) \
72- SELECT community_id, id FROM events \
73- WHERE community_id = $1 \
74- AND kind IN (9, 40002, 45001, 45003) \
75- AND deleted_at IS NULL \
76- AND received_at > now() - make_interval(secs => $2) \
77- ON CONFLICT DO NOTHING",
78- )
79- . bind ( community. as_uuid ( ) )
80- . bind ( PUSH_GATE_BACKFILL_SECS as f64 )
81- . execute ( & mut * tx)
82- . await ?;
83- Ok ( ( ) )
84- }
85-
8652/// Maximum claims for a malformed matcher job before it is discarded.
8753pub const MAX_MATCH_ATTEMPTS : i32 = 8 ;
8854
@@ -429,9 +395,6 @@ pub async fn accept_lease_event(
429395 }
430396 return Err ( error. into ( ) ) ;
431397 }
432- if is_active {
433- backfill_push_match_jobs ( tx. conn ( ) , community) . await ?;
434- }
435398 tx. commit ( ) . await ?;
436399 Ok ( AcceptLeaseOutcome :: Accepted )
437400 } )
@@ -573,9 +536,6 @@ async fn replace_lease(
573536 . await ?;
574537
575538 if accepted. is_some ( ) {
576- if is_active {
577- backfill_push_match_jobs ( & mut tx, community) . await ?;
578- }
579539 tx. commit ( ) . await ?;
580540 return Ok ( ReplaceLeaseOutcome :: Accepted ) ;
581541 }
@@ -2440,14 +2400,14 @@ mod postgres_tests {
24402400 . await
24412401 . expect ( "drain matcher queue" ) ;
24422402 let community = make_community ( & pool) . await ;
2403+ let author = [ 25 ; 32 ] ;
2404+ activate ( & pool, community, & author, "install" , & [ 26 ; 32 ] , 1 ) . await ;
24432405 let event = nostr:: EventBuilder :: new ( nostr:: Kind :: Custom ( 9 ) , "poison" )
24442406 . sign_with_keys ( & nostr:: Keys :: generate ( ) )
24452407 . expect ( "sign event" ) ;
24462408 crate :: event:: insert_event ( & pool, community, & event, None )
24472409 . await
24482410 . expect ( "insert event" ) ;
2449- let author = [ 25 ; 32 ] ;
2450- activate ( & pool, community, & author, "install" , & [ 26 ; 32 ] , 1 ) . await ;
24512411 let wake_id = match enqueue_wake (
24522412 & pool,
24532413 community,
@@ -2775,38 +2735,14 @@ mod postgres_tests {
27752735 ) ;
27762736 }
27772737
2778- /// T1b lost-wake race, forced (migration 0023): with no eligible lease the
2779- /// events trigger must skip the match enqueue, and a lease activation
2780- /// racing an in-flight event insert must be ordered AFTER it by the gate
2781- /// lock — so the activation's backfill enqueues the event the trigger
2782- /// skipped, exactly once. Without the shared/exclusive advisory pair, the
2783- /// trigger could read "no lease" while the activation commits
2784- /// concurrently, dropping that wake with no retry.
2738+ // Lease activation must wait for an event insert holding the shared gate lock.
27852739 #[ tokio:: test]
27862740 #[ ignore = "requires Postgres" ]
2787- async fn gate_orders_lease_activation_after_in_flight_event_and_backfills_it ( ) {
2741+ async fn gate_orders_lease_activation_after_in_flight_event ( ) {
27882742 let pool = setup_pool ( ) . await ;
27892743 let community = make_community ( & pool) . await ;
27902744
2791- // Phase 0: no lease anywhere in this community — a committed gated-kind
2792- // event must not be enqueued.
2793- let skipped = nostr:: EventBuilder :: new ( nostr:: Kind :: Custom ( 9 ) , "gate skips me" )
2794- . sign_with_keys ( & nostr:: Keys :: generate ( ) )
2795- . expect ( "sign skipped event" ) ;
2796- crate :: event:: insert_event ( & pool, community, & skipped, None )
2797- . await
2798- . expect ( "insert lease-less event" ) ;
2799- let queued: i64 =
2800- sqlx:: query_scalar ( "SELECT count(*) FROM push_match_queue WHERE community_id=$1" )
2801- . bind ( community. as_uuid ( ) )
2802- . fetch_one ( & pool)
2803- . await
2804- . expect ( "count queue after lease-less insert" ) ;
2805- assert_eq ! ( queued, 0 , "gate must skip enqueue with no eligible lease" ) ;
2806-
2807- // Phase 1: hold an event-insert transaction open past its INSERT. The
2808- // (non-deferred) trigger has already run: shared gate lock held, EXISTS
2809- // saw no lease, enqueue skipped — the classic lost-wake window.
2745+ // Hold the insert transaction open after its trigger takes the shared gate lock.
28102746 let raced = nostr:: EventBuilder :: new ( nostr:: Kind :: Custom ( 9 ) , "raced wake" )
28112747 . sign_with_keys ( & nostr:: Keys :: generate ( ) )
28122748 . expect ( "sign raced event" ) ;
@@ -2823,6 +2759,11 @@ mod postgres_tests {
28232759 . await
28242760 . expect ( "insert raced event inside held txn" ) ;
28252761
2762+ let insert_pid: i32 = sqlx:: query_scalar ( "SELECT pg_backend_pid()" )
2763+ . fetch_one ( & mut * insert_tx)
2764+ . await
2765+ . expect ( "read insert backend pid" ) ;
2766+
28262767 // A concurrent activation must block on the exclusive gate lock until
28272768 // the insert transaction resolves.
28282769 let activation = {
@@ -2852,8 +2793,10 @@ mod postgres_tests {
28522793 let mut parked = false ;
28532794 for _ in 0 ..100 {
28542795 let waiting: i64 = sqlx:: query_scalar (
2855- "SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND NOT granted" ,
2796+ "SELECT count(*) FROM pg_locks WHERE locktype='advisory' AND NOT granted \
2797+ AND $1 = ANY(pg_blocking_pids(pid))",
28562798 )
2799+ . bind ( insert_pid)
28572800 . fetch_one ( & pool)
28582801 . await
28592802 . expect ( "inspect advisory waiters" ) ;
@@ -2866,8 +2809,7 @@ mod postgres_tests {
28662809 assert ! ( parked, "activation must block on the exclusive gate lock" ) ;
28672810 assert ! ( !activation. is_finished( ) ) ;
28682811
2869- // Release the event; the activation acquires the gate, and its
2870- // backfill must enqueue the event the trigger skipped — exactly once.
2812+ // Committing the insert releases the shared lock so activation can proceed.
28712813 insert_tx. commit ( ) . await . expect ( "commit raced insert" ) ;
28722814 assert_eq ! (
28732815 tokio:: time:: timeout( std:: time:: Duration :: from_secs( 10 ) , activation)
@@ -2876,33 +2818,21 @@ mod postgres_tests {
28762818 . expect( "join activation" ) ,
28772819 ReplaceLeaseOutcome :: Accepted
28782820 ) ;
2879- let backfilled: Vec < Vec < u8 > > = sqlx:: query_scalar (
2880- "SELECT event_id FROM push_match_queue WHERE community_id=$1 ORDER BY created_at" ,
2881- )
2882- . bind ( community. as_uuid ( ) )
2883- . fetch_all ( & pool)
2884- . await
2885- . expect ( "read backfilled queue" ) ;
2886- assert ! (
2887- backfilled. contains( & raced. id. as_bytes( ) . to_vec( ) ) ,
2888- "raced event must be recovered by the activation backfill"
2889- ) ;
2890-
2891- // Phase 2: with the lease now active, the trigger enqueues directly and
2892- // the backfill's ON CONFLICT dedup keeps it single.
2821+ // The active lease makes subsequent messages eligible for enqueueing.
28932822 let direct = nostr:: EventBuilder :: new ( nostr:: Kind :: Custom ( 9 ) , "direct enqueue" )
28942823 . sign_with_keys ( & nostr:: Keys :: generate ( ) )
28952824 . expect ( "sign direct event" ) ;
28962825 crate :: event:: insert_event ( & pool, community, & direct, None )
28972826 . await
28982827 . expect ( "insert post-activation event" ) ;
2899- let per_event : Vec < i64 > = sqlx:: query_scalar (
2900- "SELECT count(*) FROM push_match_queue WHERE community_id=$1 GROUP BY event_id" ,
2828+ let queued : i64 = sqlx:: query_scalar (
2829+ "SELECT count(*) FROM push_match_queue WHERE community_id=$1 AND event_id=$2 " ,
29012830 )
29022831 . bind ( community. as_uuid ( ) )
2903- . fetch_all ( & pool)
2832+ . bind ( direct. id . as_bytes ( ) . as_slice ( ) )
2833+ . fetch_one ( & pool)
29042834 . await
2905- . expect ( "count queue rows per event " ) ;
2906- assert ! ( per_event . iter ( ) . all ( |count| * count == 1 ) ) ;
2835+ . expect ( "count post-activation message jobs " ) ;
2836+ assert_eq ! ( queued , 1 ) ;
29072837 }
29082838}
0 commit comments