Skip to content

Commit 35bf9af

Browse files
committed
fix: Ensure tailnet coordinator can never be blocked
1 parent 413bfb8 commit 35bf9af

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

tailnet/coordinator.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net"
88
"net/netip"
99
"sync"
10+
"time"
1011

1112
"github.com/google/uuid"
1213
"golang.org/x/xerrors"
@@ -164,23 +165,25 @@ func (c *Coordinator) ServeClient(conn net.Conn, id uuid.UUID, agent uuid.UUID)
164165
c.mutex.Unlock()
165166
continue
166167
}
168+
c.mutex.Unlock()
167169
// Write the new node from this client to the actively
168170
// connected agent.
169171
data, err := json.Marshal([]*Node{&node})
170172
if err != nil {
171173
c.mutex.Unlock()
172174
return xerrors.Errorf("marshal nodes: %w", err)
173175
}
176+
err = agentSocket.SetWriteDeadline(time.Now().Add(5 * time.Second))
177+
if err != nil {
178+
return xerrors.Errorf("set write deadline: %w", err)
179+
}
174180
_, err = agentSocket.Write(data)
175181
if errors.Is(err, io.EOF) {
176-
c.mutex.Unlock()
177182
return nil
178183
}
179184
if err != nil {
180-
c.mutex.Unlock()
181185
return xerrors.Errorf("write json: %w", err)
182186
}
183-
c.mutex.Unlock()
184187
}
185188
}
186189

@@ -255,11 +258,12 @@ func (c *Coordinator) ServeAgent(conn net.Conn, id uuid.UUID) error {
255258
for _, connectionSocket := range connectionSockets {
256259
connectionSocket := connectionSocket
257260
go func() {
261+
_ = connectionSocket.SetWriteDeadline(time.Now().Add(5 * time.Second))
258262
_, _ = connectionSocket.Write(data)
259263
wg.Done()
260264
}()
261265
}
262-
wg.Wait()
263266
c.mutex.Unlock()
267+
wg.Wait()
264268
}
265269
}

0 commit comments

Comments
 (0)