Skip to content

Commit c9ec3ce

Browse files
committed
Fix race condition on close
1 parent 1fca44c commit c9ec3ce

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

agent/agent.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ func (a *agent) run(ctx context.Context) error {
221221
}
222222

223223
func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*tailnet.Conn, error) {
224+
a.closeMutex.Lock()
225+
if a.isClosed() {
226+
a.closeMutex.Unlock()
227+
return nil, xerrors.New("closed")
228+
}
229+
a.connCloseWait.Add(1)
230+
a.closeMutex.Unlock()
224231
network, err := tailnet.NewConn(&tailnet.Options{
225232
Addresses: []netip.Prefix{netip.PrefixFrom(codersdk.TailnetIP, 128)},
226233
DERPMap: derpMap,
@@ -242,9 +249,6 @@ func (a *agent) createTailnet(ctx context.Context, derpMap *tailcfg.DERPMap) (*t
242249
if err != nil {
243250
return nil, xerrors.Errorf("listen on the ssh port: %w", err)
244251
}
245-
a.closeMutex.Lock()
246-
a.connCloseWait.Add(1)
247-
a.closeMutex.Unlock()
248252
go func() {
249253
defer a.connCloseWait.Done()
250254
for {

0 commit comments

Comments
 (0)