Skip to content

Don't process activity during closing #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions wgengine/userspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,16 @@ func (e *userspaceEngine) noteRecvActivity(nk key.NodePublic) {
e.wgLock.Lock()
defer e.wgLock.Unlock()

// Reconfiguring wireguard while closing can cause deadlocks, since this
// function could be called from receive functions that need to shut down
// in order to close the wireguard device.
e.mu.Lock()
if e.closing {
e.mu.Unlock()
return
}
e.mu.Unlock()

if _, ok := e.recvActivityAt[nk]; !ok {
// Not a trimmable peer we care about tracking. (See isTrimmablePeer)
if e.trimmedNodes[nk] {
Expand Down Expand Up @@ -1058,24 +1068,34 @@ func (e *userspaceEngine) RequestStatus() {
}

func (e *userspaceEngine) Close() {
// It's important here to hold wgLock as well, so that noteRecvActivity exits early once we start
// closing the wgdev. Reprogramming wireguard from a recv function causes a deadlock while closing
// since the wireguard device waits for the recv function.
e.wgLock.Lock()
e.mu.Lock()
if e.closing {
e.mu.Unlock()
e.wgLock.Unlock()
return
}
e.closing = true
e.mu.Unlock()
e.wgLock.Unlock()

r := bufio.NewReader(strings.NewReader(""))

e.wgdev.IpcSetOperation(r)

e.magicConn.Close()
e.netMonUnregister()
if e.netMonOwned {
e.netMon.Close()
}
e.dns.Down()
e.router.Close()

e.wgdev.Close()

e.tundev.Close()
if e.birdClient != nil {
e.birdClient.DisableProtocol("tailscale")
Expand Down