Skip to content

RemoveClient - Deadlock #10

@TerminalFi

Description

@TerminalFi

You have a dead lock because you are double locking.

go-chat/Server/server.go

Lines 130 to 150 in 79c1cee

lock.Lock()
defer lock.Unlock()
if ClientExists(name) {
delete(clients, name)
log.Print("[RemoveClient]: Removed client " + name)
if InGroup(name) {
RemoveClientFromGroup(name)
} else {
log.Print("[RemoveClient]: " + name + " was not in any groups.")
return nil
}
}
return errors.New("[RemoveClient]: Client (" + name + ") doesn't exist")
}
// AddClientToGroup will add a client to a group.
// It doesn't return anything.
func AddClientToGroup(c string, g string) {

go-chat/Server/server.go

Lines 78 to 85 in 79c1cee

func ClientExists(n string) bool {
lock.RLock()
defer lock.RUnlock()
for c := range clients {
if c == n {
return true
}

A fix would be

 	if ClientExists(name) {  	
 		lock.Lock() 
 		defer lock.Unlock() 
  
 		delete(clients, name) 
 		log.Print("[RemoveClient]: Removed client " + name) 
 		if InGroup(name)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions