File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change @@ -532,6 +532,16 @@ func (e *userspaceEngine) noteRecvActivity(nk key.NodePublic) {
532
532
e .wgLock .Lock ()
533
533
defer e .wgLock .Unlock ()
534
534
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
+
535
545
if _ , ok := e .recvActivityAt [nk ]; ! ok {
536
546
// Not a trimmable peer we care about tracking. (See isTrimmablePeer)
537
547
if e .trimmedNodes [nk ] {
@@ -1058,24 +1068,34 @@ func (e *userspaceEngine) RequestStatus() {
1058
1068
}
1059
1069
1060
1070
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 ()
1061
1075
e .mu .Lock ()
1062
1076
if e .closing {
1063
1077
e .mu .Unlock ()
1078
+ e .wgLock .Unlock ()
1064
1079
return
1065
1080
}
1066
1081
e .closing = true
1067
1082
e .mu .Unlock ()
1083
+ e .wgLock .Unlock ()
1068
1084
1069
1085
r := bufio .NewReader (strings .NewReader ("" ))
1086
+
1070
1087
e .wgdev .IpcSetOperation (r )
1088
+
1071
1089
e .magicConn .Close ()
1072
1090
e .netMonUnregister ()
1073
1091
if e .netMonOwned {
1074
1092
e .netMon .Close ()
1075
1093
}
1076
1094
e .dns .Down ()
1077
1095
e .router .Close ()
1096
+
1078
1097
e .wgdev .Close ()
1098
+
1079
1099
e .tundev .Close ()
1080
1100
if e .birdClient != nil {
1081
1101
e .birdClient .DisableProtocol ("tailscale" )
You can’t perform that action at this time.
0 commit comments