@@ -1015,12 +1015,12 @@ impl PoolInner {
10151015 // in the database instead of just in memory
10161016 let result = pool
10171017 . configure_fdw ( coord. servers . as_ref ( ) )
1018- . and_then ( |( ) | migrate_schema ( & pool. logger , & mut conn) )
1019- . and_then ( |count| coord. propagate ( & pool, count) ) ;
1018+ . and_then ( |( ) | migrate_schema ( & pool. logger , & mut conn) ) ;
10201019 debug ! ( & pool. logger, "Release migration lock" ) ;
10211020 advisory_lock:: unlock_migration ( & mut conn) . unwrap_or_else ( |err| {
10221021 die ( & pool. logger , "failed to release migration lock" , & err) ;
10231022 } ) ;
1023+ let result = result. and_then ( |count| coord. propagate ( & pool, count) ) ;
10241024 result. unwrap_or_else ( |err| die ( & pool. logger , "migrations failed" , & err) ) ;
10251025
10261026 // Locale check
@@ -1233,6 +1233,10 @@ impl PoolCoordinator {
12331233 /// other pools will then recreate any tables that they imported from
12341234 /// `shard`. If `pool` is a new shard, we also map all other shards into
12351235 /// it.
1236+ ///
1237+ /// This tries to take the migration lock and must therefore be run from
1238+ /// code that does _not_ hold the migration lock as it will otherwise
1239+ /// deadlock
12361240 fn propagate ( & self , pool : & PoolInner , count : MigrationCount ) -> Result < ( ) , StoreError > {
12371241 // pool is a new shard, map all other shards into it
12381242 if count. is_new ( ) {
@@ -1244,7 +1248,14 @@ impl PoolCoordinator {
12441248 if count. had_migrations ( ) {
12451249 let server = self . server ( & pool. shard ) ?;
12461250 for pool in self . pools . lock ( ) . unwrap ( ) . values ( ) {
1247- if let Err ( e) = pool. remap ( server) {
1251+ let mut conn = pool. get ( ) ?;
1252+ let remap_res = {
1253+ advisory_lock:: lock_migration ( & mut conn) ?;
1254+ let res = pool. remap ( server) ;
1255+ advisory_lock:: unlock_migration ( & mut conn) ?;
1256+ res
1257+ } ;
1258+ if let Err ( e) = remap_res {
12481259 error ! ( pool. logger, "Failed to map imports from {}" , server. shard; "error" => e. to_string( ) ) ;
12491260 return Err ( e) ;
12501261 }
0 commit comments