@@ -1561,6 +1561,14 @@ mod tests {
15611561 assert_eq ! ( stale_ttl, None ) ;
15621562
15631563 let mut activation = pool. begin ( ) . await . expect ( "begin TTL activation" ) ;
1564+ // Model the repaired update_channel protocol (migration 0024): the
1565+ // TTL transition holds the per-channel advisory key EXCLUSIVE, which
1566+ // is what the event trigger's shared acquisition now waits on.
1567+ sqlx:: query ( "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))" )
1568+ . bind ( format ! ( "buzz_channel_ttl:{community_uuid}:{racing}" ) )
1569+ . execute ( & mut * activation)
1570+ . await
1571+ . expect ( "acquire exclusive channel TTL key" ) ;
15641572 let activation_deadline: DateTime < Utc > = sqlx:: query_scalar (
15651573 "UPDATE channels \
15661574 SET ttl_seconds = 60, ttl_deadline = clock_timestamp() + interval '60 seconds' \
@@ -1605,6 +1613,99 @@ mod tests {
16051613 ) ;
16061614 }
16071615
1616+ /// T1a repair regression test (migration 0024): permanent-channel event
1617+ /// commits must not serialize on the channel row. The 0022 trigger took
1618+ /// `FOR UPDATE` on the channel tuple before testing `ttl_seconds`, so
1619+ /// concurrent commits into one hot permanent channel queued at commit
1620+ /// time (deferred trigger) — invisible to any single-connection test.
1621+ /// This holds N insert transactions at a barrier past their INSERTs,
1622+ /// then proves (a) while all N sit pre-commit, no transaction holds a
1623+ /// row-level lock on the channel tuple, and (b) all N commits succeed
1624+ /// with the channel row untouched (permanent ⇒ no deadline write).
1625+ #[ tokio:: test]
1626+ #[ ignore = "requires Postgres" ]
1627+ async fn permanent_channel_event_commits_do_not_lock_the_channel_row ( ) {
1628+ const N : usize = 8 ;
1629+ // setup_pool's default cap (10) covers N held transactions plus the
1630+ // pg_locks inspector connection.
1631+ let pool = setup_pool ( ) . await ;
1632+ let community_uuid = make_test_community ( & pool) . await ;
1633+ let channel = make_test_channel ( & pool, community_uuid, None ) . await ;
1634+
1635+ // Open N transactions, run the full event INSERT in each (the deferred
1636+ // trigger fires at COMMIT), and park them at a barrier.
1637+ let mut txs = Vec :: new ( ) ;
1638+ for i in 0 ..N {
1639+ let mut tx = pool. begin ( ) . await . expect ( "begin insert txn" ) ;
1640+ let event = make_text_event ( & format ! ( "hot channel event {i}" ) ) ;
1641+ sqlx:: query (
1642+ "INSERT INTO events (community_id,id,pubkey,created_at,kind,tags,content,sig,received_at,channel_id) \
1643+ VALUES ($1,$2,$3,$4,9,$5,$6,$7,now(),$8)",
1644+ )
1645+ . bind ( community_uuid)
1646+ . bind ( event. id . as_bytes ( ) . as_slice ( ) )
1647+ . bind ( event. pubkey . as_bytes ( ) . as_slice ( ) )
1648+ . bind ( DateTime :: from_timestamp ( event. created_at . as_secs ( ) as i64 , 0 ) . unwrap ( ) )
1649+ . bind ( serde_json:: to_value ( & event. tags ) . unwrap ( ) )
1650+ . bind ( & event. content )
1651+ . bind ( event. sig . serialize ( ) . as_slice ( ) )
1652+ . bind ( channel)
1653+ . execute ( & mut * tx)
1654+ . await
1655+ . expect ( "insert event inside held txn" ) ;
1656+ txs. push ( tx) ;
1657+ }
1658+
1659+ // With all N transactions holding completed INSERTs, none may hold a
1660+ // row-level lock on the channels tuple. (The 0022 trigger would not
1661+ // have taken it yet either — it locks at COMMIT — so also verify the
1662+ // commit phase below completes without mutual blocking.)
1663+ let tuple_locks: i64 = sqlx:: query_scalar (
1664+ "SELECT count(*) FROM pg_locks l \
1665+ JOIN pg_class c ON c.oid = l.relation \
1666+ WHERE c.relname = 'channels' AND l.locktype = 'tuple'",
1667+ )
1668+ . fetch_one ( & pool)
1669+ . await
1670+ . expect ( "inspect pg_locks" ) ;
1671+ assert_eq ! ( tuple_locks, 0 , "no channel tuple locks while txns are held" ) ;
1672+
1673+ // Release all commits concurrently. Under 0022 these serialized on the
1674+ // channel row (each holding it across its WAL flush); under 0024 the
1675+ // shared advisory key admits them all. Join with a timeout so a
1676+ // regression fails fast instead of hanging the suite.
1677+ let commits = txs
1678+ . into_iter ( )
1679+ . map ( |tx| tokio:: spawn ( async move { tx. commit ( ) . await } ) )
1680+ . collect :: < Vec < _ > > ( ) ;
1681+ for c in commits {
1682+ tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 10 ) , c)
1683+ . await
1684+ . expect ( "concurrent permanent-channel commits must not block" )
1685+ . expect ( "join commit task" )
1686+ . expect ( "commit succeeds" ) ;
1687+ }
1688+
1689+ let deadline: Option < DateTime < Utc > > = sqlx:: query_scalar (
1690+ "SELECT ttl_deadline FROM channels WHERE community_id = $1 AND id = $2" ,
1691+ )
1692+ . bind ( community_uuid)
1693+ . bind ( channel)
1694+ . fetch_one ( & pool)
1695+ . await
1696+ . expect ( "read deadline after commits" ) ;
1697+ assert_eq ! ( deadline, None , "permanent channel must remain untouched" ) ;
1698+ let stored: i64 = sqlx:: query_scalar (
1699+ "SELECT count(*) FROM events WHERE community_id = $1 AND channel_id = $2" ,
1700+ )
1701+ . bind ( community_uuid)
1702+ . bind ( channel)
1703+ . fetch_one ( & pool)
1704+ . await
1705+ . expect ( "count stored events" ) ;
1706+ assert_eq ! ( stored as usize , N ) ;
1707+ }
1708+
16081709 #[ tokio:: test]
16091710 #[ ignore = "requires Postgres" ]
16101711 async fn get_event_by_id_is_scoped_when_event_id_collides_across_communities ( ) {
0 commit comments