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

@ -320,7 +320,7 @@ fn client_leave_room(client: &Arc<Client>, send_to_client: bool){
break; break;
} }
} }
println!("Changing master to {}",new_master_id); println!("Changing master to {}",new_master_id);
for (_k,v) in clients.iter() { for (_k,v) in clients.iter() {
send_client_master_message(&v, new_master_id); send_client_master_message(&v, new_master_id);
@ -389,22 +389,17 @@ fn read_join_message(stream: &mut TcpStream, client: &Arc<Client>){
rooms.insert(String::from(&extended_room_name),r); rooms.insert(String::from(&extended_room_name),r);
println!("New room {} created",&extended_room_name); println!("New room {} created",&extended_room_name);
} }
} //the room is guaranteed to exist now, so this call can't fail
//the room is guaranteed to exist now let room_to_join = &rooms[&extended_room_name];
let mut clients = room_to_join.clients.write().unwrap();
{ //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();
clients.insert(client.id,client.clone()); clients.insert(client.id,client.clone());
}
{
println!("Client {} joined {}",client.id,&extended_room_name); println!("Client {} joined {}",client.id,&extended_room_name);
let rooms = client.rooms_mutex.read().unwrap();
let mut room = client.room.write().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 rooms = client.rooms_mutex.read().unwrap();
let clients = rooms[&extended_room_name].clients.read().unwrap(); //only need a read lock now let clients = rooms[&extended_room_name].clients.read().unwrap(); //only need a read lock now