@@ -1426,8 +1426,32 @@ func (s APITestSuite) TestResyncFailed() {
14261426 "%" + s .suffix + "%" )
14271427 require .NoError (s .t , err )
14281428
1429+ // Use a dedicated connection with a different application_name to avoid being killed
1430+ // by the pg_terminate_backend above, which targets application_name = 'peerdb'
1431+ catalogConnStr := internal .GetCatalogConnectionStringFromEnv (s .t .Context ())
1432+ statusConnConfig , err := pgx .ParseConfig (catalogConnStr )
1433+ require .NoError (s .t , err )
1434+ statusConnConfig .RuntimeParams ["application_name" ] = "catalog_test_access"
1435+ statusConn , err := pgx .ConnectConfig (s .t .Context (), statusConnConfig )
1436+ require .NoError (s .t , err )
1437+ defer statusConn .Close (s .t .Context ())
1438+
14291439 EnvWaitFor (s .t , env , 3 * time .Minute , "wait for failed" , func () bool {
1430- return env .GetFlowStatus (s .t ) == protos .FlowStatus_STATUS_FAILED
1440+ var flowStatus protos.FlowStatus
1441+ // This is the only test not using `GetFlowStatus` because that method
1442+ // will use a PG connection pool that has the `application_name` set to 'peerdb' because
1443+ // this is an internal method and initializes the connection assuming it is being called from PeerDB service
1444+ // rather than test code.
1445+ //
1446+ // In CI, the catalog DB is the same as the source DB (Postgres), so the pg_terminate_backend call above will
1447+ // kill all connections with application_name 'peerdb', including those from the connection pool used by GetFlowStatus.
1448+ // This generated a race condition between the backend termination and the test finalization that
1449+ // is removed by initializing a dedicated connection with a different application_name that is not killed by
1450+ // the query above thanks to the filter condition `application_name == 'peerdb'`.
1451+ err := statusConn .QueryRow (
1452+ s .t .Context (), "SELECT status FROM flows WHERE workflow_id = $1" , env .GetID (),
1453+ ).Scan (& flowStatus )
1454+ return err == nil && flowStatus == protos .FlowStatus_STATUS_FAILED
14311455 })
14321456
14331457 err = mvManager .DropBadMV (s .t .Context ())
0 commit comments