Skip to content

Commit

Permalink
Merge pull request #20 from aau-network-security/debug-freeze
Browse files Browse the repository at this point in the history
Fixed deadlock caused by wrong mutex lock order
  • Loading branch information
Mikkelhost authored Nov 25, 2024
2 parents b1e5cfe + ae5303b commit de530ec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
5 changes: 3 additions & 2 deletions internal/daemon/agentpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,15 @@ func (agentLab *AgentLab) updateLabInfo() {
}

func (agentLab *AgentLab) close() error {
ctx := context.Background()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if agentLab.Conn == nil {
return errors.New("connection in agentLab is nil")
}
agentClient := aproto.NewAgentClient(agentLab.Conn)
_, err := agentClient.CloseLab(ctx, &aproto.CloseLabRequest{LabTag: agentLab.LabInfo.Tag})
if err != nil {
log.Error().Err(err).Msg("error updating lab info")
log.Error().Err(err).Msg("error closing lab")
return err
}
return nil
Expand Down
5 changes: 2 additions & 3 deletions internal/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,8 @@ func (d *daemon) labExpiryRoutine() {
log.Info().Msg("[lab-expiry-routine] starting routine")
for {
time.Sleep(1 * time.Second)
d.eventpool.M.RLock()
for _, event := range d.eventpool.Events {
events := d.eventpool.GetAllEvents()
for _, event := range events {
var wg sync.WaitGroup
anyLabsClosed := false
event.M.RLock()
Expand Down Expand Up @@ -406,7 +406,6 @@ func (d *daemon) labExpiryRoutine() {
broadCastCommandToEventTeams(event, updateEventInfo)
}
}
d.eventpool.M.RUnlock()
}
}

Expand Down
35 changes: 20 additions & 15 deletions internal/daemon/eventLabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,27 +183,30 @@ func (d *daemon) closeLab(c *gin.Context) {
return
}

team.M.Lock()
defer team.M.Unlock()

if team.Lab == nil {
teamLab := team.GetLab()
if teamLab == nil {
log.Debug().Str("team", team.Username).Msg("lab not found for team")
c.JSON(http.StatusNotFound, APIResponse{Status: "lab not found"})
return
}

defer saveState(d.eventpool, d.conf.StatePath)

event.M.Lock()
delete(event.Labs, team.Lab.LabInfo.Tag)
event.M.Unlock()
if team.Lab.Conn != nil {
if err := team.Lab.close(); err != nil {
log.Error().Err(err).Str("team", team.Username).Msg("Error closing lab for team")
}
go func(team *Team, event *Event) {
defer func() {
event.M.Lock()
delete(event.Labs, team.Lab.LabInfo.Tag)
event.M.Unlock()
team.M.Lock()
team.Lab = nil
team.M.Unlock()
saveState(d.eventpool, d.conf.StatePath)
sendCommandToTeam(team, updateTeam)
}()
if err := team.Lab.close(); err != nil {
log.Error().Err(err).Str("team", team.Username).Msg("Error closing lab for team")
}
}(team, event)
}
team.Lab = nil
sendCommandToTeam(team, updateTeam)

c.JSON(http.StatusOK, APIResponse{Status: "OK"})
}
Expand All @@ -226,7 +229,9 @@ func (d *daemon) cancelLabConfigurationRequest(c *gin.Context) {
}

team.M.Lock()
defer team.M.Unlock()
defer func(team *Team) {
team.M.Unlock()
}(team)

if team.Status == InQueue {
team.Status = Idle
Expand Down

0 comments on commit de530ec

Please sign in to comment.