another potential race condition, though this one is less likely

main
Kyle Johnsen 2022-02-13 07:07:26 -05:00
parent 6eb92d3657
commit 39d7359475
1 changed files with 8 additions and 13 deletions

View File

@ -389,22 +389,17 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
rooms.insert(String::from(&extended_room_name),r);
println!("New room {} created",&extended_room_name);
}
}
//the room is guaranteed to exist now
{ //actually add the client to the room (need a write lock)
let rooms = client.rooms_mutex.read().unwrap();
let mut clients = rooms[&extended_room_name].clients.write().unwrap();
//the room is guaranteed to exist now, so this call can't fail
let room_to_join = &rooms[&extended_room_name];
let mut clients = room_to_join.clients.write().unwrap();
clients.insert(client.id,client.clone());
}
{
println!("Client {} joined {}",client.id,&extended_room_name);
let rooms = client.rooms_mutex.read().unwrap();
let mut room = client.room.write().unwrap();
*room = Some(rooms.get(&extended_room_name).unwrap().clone()); //we create an option and assign it back to the room
*room = Some(room_to_join.clone()); //we create an option and assign it back to the room
}
//once the client is in the room, it can't suddenly die, so we can release the write lock, and can use the extended_room name without issue
{
let rooms = client.rooms_mutex.read().unwrap();
let clients = rooms[&extended_room_name].clients.read().unwrap(); //only need a read lock now