Skip to content

Commit b5773d8

Browse files
committed
Don't process activity during closing
Signed-off-by: Spike Curtis <spike@coder.com>
1 parent 9321478 commit b5773d8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

wgengine/userspace.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,16 @@ func (e *userspaceEngine) noteRecvActivity(nk key.NodePublic) {
532532
e.wgLock.Lock()
533533
defer e.wgLock.Unlock()
534534

535+
// Reconfiguring wireguard while closing can cause deadlocks, since this
536+
// function could be called from receive functions that need to shut down
537+
// in order to close the wireguard device.
538+
e.mu.Lock()
539+
if e.closing {
540+
e.mu.Unlock()
541+
return
542+
}
543+
e.mu.Unlock()
544+
535545
if _, ok := e.recvActivityAt[nk]; !ok {
536546
// Not a trimmable peer we care about tracking. (See isTrimmablePeer)
537547
if e.trimmedNodes[nk] {
@@ -1058,24 +1068,34 @@ func (e *userspaceEngine) RequestStatus() {
10581068
}
10591069

10601070
func (e *userspaceEngine) Close() {
1071+
// It's important here to hold wgLock as well, so that noteRecvActivity exits early once we start
1072+
// closing the wgdev. Reprogramming wireguard from a recv function causes a deadlock while closing
1073+
// since the wireguard device waits for the recv function.
1074+
e.wgLock.Lock()
10611075
e.mu.Lock()
10621076
if e.closing {
10631077
e.mu.Unlock()
1078+
e.wgLock.Unlock()
10641079
return
10651080
}
10661081
e.closing = true
10671082
e.mu.Unlock()
1083+
e.wgLock.Unlock()
10681084

10691085
r := bufio.NewReader(strings.NewReader(""))
1086+
10701087
e.wgdev.IpcSetOperation(r)
1088+
10711089
e.magicConn.Close()
10721090
e.netMonUnregister()
10731091
if e.netMonOwned {
10741092
e.netMon.Close()
10751093
}
10761094
e.dns.Down()
10771095
e.router.Close()
1096+
10781097
e.wgdev.Close()
1098+
10791099
e.tundev.Close()
10801100
if e.birdClient != nil {
10811101
e.birdClient.DisableProtocol("tailscale")

0 commit comments

Comments
 (0)