Skip to content

Commit e2f6a19

Browse files
committed
Fix lock in coordinator
1 parent bcb97ac commit e2f6a19

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

enterprise/tailnet/coordinator.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,10 @@ func (c *haCoordinator) ServeAgent(conn net.Conn, id uuid.UUID) error {
178178
if len(nodes) > 0 {
179179
data, err := json.Marshal(nodes)
180180
if err != nil {
181-
c.mutex.Unlock()
182181
return xerrors.Errorf("marshal json: %w", err)
183182
}
184183
_, err = conn.Write(data)
185184
if err != nil {
186-
c.mutex.Unlock()
187185
return xerrors.Errorf("write nodes: %w", err)
188186
}
189187
}
@@ -250,17 +248,16 @@ func (c *haCoordinator) hangleAgentUpdate(id uuid.UUID, decoder *json.Decoder) (
250248
}
251249

252250
c.mutex.Lock()
253-
defer c.mutex.Unlock()
254-
255251
c.nodes[id] = &node
256-
257252
connectionSockets, ok := c.agentToConnectionSockets[id]
258253
if !ok {
254+
c.mutex.Unlock()
259255
return &node, nil
260256
}
261257

262258
data, err := json.Marshal([]*agpl.Node{&node})
263259
if err != nil {
260+
c.mutex.Unlock()
264261
return nil, xerrors.Errorf("marshal nodes: %w", err)
265262
}
266263

@@ -275,6 +272,7 @@ func (c *haCoordinator) hangleAgentUpdate(id uuid.UUID, decoder *json.Decoder) (
275272
_, _ = connectionSocket.Write(data)
276273
}()
277274
}
275+
c.mutex.Unlock()
278276
wg.Wait()
279277
return &node, nil
280278
}
@@ -394,12 +392,12 @@ func (c *haCoordinator) runPubsub() error {
394392
}
395393

396394
c.mutex.Lock()
397-
defer c.mutex.Unlock()
398-
399395
agentSocket, ok := c.agentSockets[agentUUID]
400396
if !ok {
397+
c.mutex.Unlock()
401398
return
402399
}
400+
c.mutex.Unlock()
403401

404402
// We get a single node over pubsub, so turn into an array.
405403
_, err = agentSocket.Write(nodeJSON)
@@ -410,7 +408,6 @@ func (c *haCoordinator) runPubsub() error {
410408
c.log.Error(ctx, "send callmemaybe to agent", slog.Error(err))
411409
return
412410
}
413-
414411
case "agenthello":
415412
agentUUID, err := uuid.ParseBytes(agentID)
416413
if err != nil {
@@ -426,7 +423,6 @@ func (c *haCoordinator) runPubsub() error {
426423
return
427424
}
428425
}
429-
430426
case "agentupdate":
431427
agentUUID, err := uuid.ParseBytes(agentID)
432428
if err != nil {
@@ -440,7 +436,6 @@ func (c *haCoordinator) runPubsub() error {
440436
c.log.Error(ctx, "handle agent update", slog.Error(err))
441437
return
442438
}
443-
444439
default:
445440
c.log.Error(ctx, "unknown peer event", slog.F("name", string(eventType)))
446441
}

0 commit comments

Comments
 (0)