From 64aa185153e1f70b47e3c6d67ee5bc0baa56ca08 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sat, 7 Nov 2026 23:57:25 +0100 Subject: [PATCH 1/6] Fix the deadlock --- src/services/game/matchmaking.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/services/game/matchmaking.rs b/src/services/game/matchmaking.rs index 7bbabb2..fbac46b 100644 --- a/src/services/game/matchmaking.rs +++ b/src/services/game/matchmaking.rs @@ -62,7 +62,11 @@ impl Matchmaking { ) { let queue = &mut *self.queue.lock(); - while let Some(entry) = queue.front() { + // Index of the queue entry currently being checked. Entries that don't + // match this game are skipped instead of being re-checked forever + let mut index = 0; + + while let Some(entry) = queue.get(index) { let join_state = { link.read().joinable_state(Some(&entry.rule_set)) }; // TODO: If player has been in queue long enough create @@ -70,8 +74,9 @@ impl Matchmaking { match join_state { GameJoinableState::Joinable => { + // Index is not advanced, the next entry moves into this position let entry = queue - .pop_front() + .remove(index) .expect("Expecting matchmaking entry but nothing was present"); debug!("Found player from queue adding them to the game (GID: {game_id})"); @@ -92,6 +97,9 @@ impl Matchmaking { GameJoinableState::NotMatch => { // TODO: Check started time and timeout // player if they've been waiting too long + + // Skip this entry and check the next one + index += 1; } } } From 9339c467d4a5e71c87e0a48c22ba207a954ef60b Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sat, 7 Nov 2026 23:59:16 +0100 Subject: [PATCH 2/6] Refactor game session management methods Refactor set_game and clear_game methods to use replace and take for better handling of previous game data. --- src/session/data.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/session/data.rs b/src/session/data.rs index 79c0e4d..90a9332 100644 --- a/src/session/data.rs +++ b/src/session/data.rs @@ -241,15 +241,20 @@ impl SessionData { } /// Sets the game the session is currently apart of - pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { + pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { // Set the current game - self.write_publish(|data| { - data.game = Some(SessionGameData { + let previous = self.write_publish(|data| { + data.game.replace(SessionGameData { player_id: data.player_assoc.player.id, game_id, game_ref, - }); + }) }); + + // Previous game MUST be dropped *after* the write lock is released + // as its drop removes the player from the game which requires + // accessing this session data again + drop(previous); } /// Clears the game the session is apart of @@ -258,7 +263,12 @@ impl SessionData { /// USER GAME, call [Self::clear_game_gm] instead which /// doesn't let the drop run preventing a deadlock pub fn clear_game(&self) { - self.write_publish(|data| data.game = None); + let previous = self.write_publish(|data| data.game.take()); + + // Game MUST be dropped *after* the write lock is released + // as its drop removes the player from the game which requires + // accessing this session data again + drop(previous); } /// Clears the game the session is apart of, safe to From d5a9375813c2b5754ef8799c27f58487f5d31027 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sun, 8 Nov 2026 00:01:48 +0100 Subject: [PATCH 3/6] Improve tunnel management and association logic Refactor tunnel management by enhancing dead tunnel removal and association handling. --- src/services/tunnel/mappings.rs | 66 ++++++++++++++++++++++++--------- 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/services/tunnel/mappings.rs b/src/services/tunnel/mappings.rs index 2ee7e3d..a403065 100644 --- a/src/services/tunnel/mappings.rs +++ b/src/services/tunnel/mappings.rs @@ -63,19 +63,28 @@ impl TunnelPoolMap { } } -#[derive(Default)] -pub struct TunnelMappings { - /// Next available tunnel ID - next_tunnel_id: TunnelId, - - /// Mapping from [TunnelId]s to the actual [TunnelHandle] for communicating - /// with the tunnel - id_to_tunnel: IntHashMap, +pub fn remove_dead_tunnels(&mut self, now: Instant, keep_alive_timeout: Duration) { + // Retain all now expired tunnels + self.id_to_tunnel.retain(|tunnel_id, data| { + // Initial dead check based on keep alive timeout + let mut dead = now.duration_since(data.last_alive) > keep_alive_timeout; + + // Check for closed http tunnels + if let TunnelHandle::Http(handle) = &data.handle + && handle.tx.is_closed() + { + dead = true + } - /// Mapping from [AssociationId] (Client association) to [TunnelHandle] - association_to_tunnel: HashMap, + // Remove other tunnel mappings + if dead { + // Remove association mapping + self.association_to_tunnel.remove(&data.association); + self.pool.remove_by_tunnel(tunnel_id); + } - pool: TunnelPoolMap, + !dead + }); } /// Represents a key that is created from a [PoolId] and [PoolIndex] combined @@ -138,7 +147,7 @@ impl TunnelMappings { // Retain all now expired tunnels self.id_to_tunnel.retain(|tunnel_id, data| { // Initial dead check based on keep alive timeout - let mut dead = data.last_alive.duration_since(now) > keep_alive_timeout; + let mut dead = now.duration_since(data.last_alive) > keep_alive_timeout; // Check for closed http tunnels if let TunnelHandle::Http(handle) = &data.handle @@ -171,7 +180,18 @@ impl TunnelMappings { // Insert the tunnel into the mappings self.id_to_tunnel.insert(tunnel_id, tunnel); - self.association_to_tunnel.insert(association, tunnel_id); + + // Replace any previous tunnel for this association + if let Some(previous) = self.association_to_tunnel.insert(association, tunnel_id) { + self.id_to_tunnel.remove(&previous); + self.pool.remove_by_tunnel(&previous); + } + + // Client is already in a game, connect the new tunnel to its slot + if let Some(pool_key) = self.association_to_pool.get(&association).copied() { + self.pool.remove_by_pool_key(&pool_key); + self.pool.insert(tunnel_id, pool_key); + } Some(tunnel_id) } @@ -196,16 +216,28 @@ impl TunnelMappings { /// Attempts to associate the tunnel from `address` to the provided /// `pool_id` and `pool_index` if there is a tunnel connected to /// `address` - pub fn associate_pool(&mut self, tunnel_id: TunnelId, pool_id: PoolId, pool_index: PoolIndex) { + pub fn associate_pool( + &mut self, + association: AssociationId, + pool_id: PoolId, + pool_index: PoolIndex, + ) { let key = PoolKey::new(pool_id, pool_index); - self.pool.insert(tunnel_id, key); + + // Remember the slot even if the tunnel doesn't exist yet + self.association_to_pool.insert(association, key); + + if let Some(tunnel_id) = self.get_association_tunnel(&association) { + self.pool.insert(tunnel_id, key); + } } /// Removes the association between a [PoolKey] and a [TunnelId] if /// one is present pub fn dissociate_pool(&mut self, pool_id: PoolId, pool_index: PoolIndex) { - self.pool - .remove_by_pool_key(&PoolKey::new(pool_id, pool_index)); + let key = PoolKey::new(pool_id, pool_index); + self.pool.remove_by_pool_key(&key); + self.association_to_pool.retain(|_, value| *value != key); } /// Get the pool key of a tunnel From db12c9c8da770617226ae9e888822ea43ad435b6 Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sun, 8 Nov 2026 00:02:20 +0100 Subject: [PATCH 4/6] Simplify pool association logic in mod.rs --- src/services/tunnel/mod.rs | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/services/tunnel/mod.rs b/src/services/tunnel/mod.rs index c374312..79def3a 100644 --- a/src/services/tunnel/mod.rs +++ b/src/services/tunnel/mod.rs @@ -62,15 +62,9 @@ impl TunnelService { pool_id: PoolId, pool_index: PoolIndex, ) { - let mappings = &mut *self.mappings.write(); - let tunnel_id = { - match mappings.get_association_tunnel(&association) { - Some(value) => value, - None => return, - } - }; - - mappings.associate_pool(tunnel_id, pool_id, pool_index); + self.mappings + .write() + .associate_pool(association, pool_id, pool_index); } pub fn dissociate_pool(&self, pool_id: PoolId, pool_index: PoolIndex) { From 2954a9293e8d4c4b8da55d763f254726c5b99afe Mon Sep 17 00:00:00 2001 From: JonatanRek Date: Sun, 8 Nov 2026 00:09:13 +0100 Subject: [PATCH 5/6] Update TunnelMappings struct and remove dead tunnels function Refactor TunnelMappings struct to include new fields and remove dead tunnels logic. --- src/services/tunnel/mappings.rs | 36 +++++++++++++++------------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/src/services/tunnel/mappings.rs b/src/services/tunnel/mappings.rs index a403065..a5ed326 100644 --- a/src/services/tunnel/mappings.rs +++ b/src/services/tunnel/mappings.rs @@ -63,28 +63,24 @@ impl TunnelPoolMap { } } -pub fn remove_dead_tunnels(&mut self, now: Instant, keep_alive_timeout: Duration) { - // Retain all now expired tunnels - self.id_to_tunnel.retain(|tunnel_id, data| { - // Initial dead check based on keep alive timeout - let mut dead = now.duration_since(data.last_alive) > keep_alive_timeout; - - // Check for closed http tunnels - if let TunnelHandle::Http(handle) = &data.handle - && handle.tx.is_closed() - { - dead = true - } +#[derive(Default)] +pub struct TunnelMappings { + /// Next available tunnel ID + next_tunnel_id: TunnelId, - // Remove other tunnel mappings - if dead { - // Remove association mapping - self.association_to_tunnel.remove(&data.association); - self.pool.remove_by_tunnel(tunnel_id); - } + /// Mapping from [TunnelId]s to the actual [TunnelHandle] for communicating + /// with the tunnel + id_to_tunnel: IntHashMap, + + /// Mapping from [AssociationId] (Client association) to [TunnelHandle] + association_to_tunnel: HashMap, + + /// Mapping from [AssociationId] to the game pool slot the client is in, + /// kept even if the client doesn't have a tunnel yet so a tunnel created + /// (or re-created) later on is still connected to the right slot + association_to_pool: HashMap, - !dead - }); + pool: TunnelPoolMap, } /// Represents a key that is created from a [PoolId] and [PoolIndex] combined From 385de91fb1da3930ed4b8f568857729eb4300574 Mon Sep 17 00:00:00 2001 From: jacobtread Date: Sun, 8 Nov 2026 12:45:30 +1300 Subject: [PATCH 6/6] chore: format and replace .copied() --- src/services/tunnel/mappings.rs | 7 ++++--- src/session/data.rs | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/services/tunnel/mappings.rs b/src/services/tunnel/mappings.rs index a5ed326..4af52f4 100644 --- a/src/services/tunnel/mappings.rs +++ b/src/services/tunnel/mappings.rs @@ -184,9 +184,9 @@ impl TunnelMappings { } // Client is already in a game, connect the new tunnel to its slot - if let Some(pool_key) = self.association_to_pool.get(&association).copied() { - self.pool.remove_by_pool_key(&pool_key); - self.pool.insert(tunnel_id, pool_key); + if let Some(pool_key) = self.association_to_pool.get(&association) { + self.pool.remove_by_pool_key(pool_key); + self.pool.insert(tunnel_id, *pool_key); } Some(tunnel_id) @@ -223,6 +223,7 @@ impl TunnelMappings { // Remember the slot even if the tunnel doesn't exist yet self.association_to_pool.insert(association, key); + // Associate the tunnel to the pool if theres an associated tunnel if let Some(tunnel_id) = self.get_association_tunnel(&association) { self.pool.insert(tunnel_id, key); } diff --git a/src/session/data.rs b/src/session/data.rs index 90a9332..8f03877 100644 --- a/src/session/data.rs +++ b/src/session/data.rs @@ -241,7 +241,7 @@ impl SessionData { } /// Sets the game the session is currently apart of - pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { + pub fn set_game(&self, game_id: GameID, game_ref: WeakGameRef) { // Set the current game let previous = self.write_publish(|data| { data.game.replace(SessionGameData { @@ -271,7 +271,7 @@ impl SessionData { drop(previous); } - /// Clears the game the session is apart of, safe to + /// Clears the game the session is apart of, safe to /// call from the game manager as it prevents the default /// drop behavior of trying to remove the player from the game pub fn clear_game_gm(&self) {