net/netmon: don't hold mu while closing the OS monitor

On Windows, winMon.Close unregisters the route/address/interface change
callbacks and waits for in-flight ones to return. Those callbacks call
Monitor.isActive, which takes m.mu. Monitor.Close held m.mu across
om.Close, so a change notification arriving during Close deadlocked
with it, and the test binary hung until the timeout.

Release m.mu before calling om.Close. m.closed is already set under the
lock, so late callbacks see the monitor as inactive and return.

This is also the root cause of the Windows "ipnlocal: watchdog timeout"
panics that kill the ipn/ipnlocal and wgengine/netstack test binaries.

Add TestMonitorCloseWaitsForOSCallback, which uses a fake osMon whose
Close, like winipcfg's Unregister, waits for a callback that needs
m.mu. That reproduces the deadlock on any OS.

flakestress TestMonitorCloseWaitsForOSCallback, -p 4, linux:
  before: 16/16 runs deadlocked
  after:  0/135569 runs

Fixes #22228
Fixes #22548

Change-Id: Ibe7740ca23a1ab375d37c280838942f49b4a6a8f
Signed-off-by: Nick Khyl <nickk@tailscale.com>
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Signed-off-by: Guy J Grigsby <guy@tailscale.com>
This commit is contained in:
Nick Khyl authored and Guy J Grigsby committed 2026-12-03 16:59:49 -07:00
1 parent 0090c64ff3
commit 413ec5760e
2 files changed
+57 -4

No files matched your search

+8 -4
View File
@@ -49,6 +49,10 @@ type message interface {
// osMon is the interface that each operating system-specific
// implementation of the link monitor must implement.
//
// Lock order: Monitor.mu is acquired after any osMon lock. An osMon
// may acquire Monitor.mu (e.g. winMon calls Monitor.isActive from OS
// callbacks); Monitor must not call osMon methods while holding it.
type osMon interface {
Close() error
@@ -72,7 +76,7 @@ type Monitor struct {
b *eventbus.Client
changed *eventbus.Publisher[ChangeDelta]
om osMon // nil means not supported on this platform
om osMon // nil means not supported on this platform; its methods must not be called with mu held
change chan bool // send false to wake poller, true to also force ChangeDeltas be sent
stop chan struct{} // closed on Stop
static bool // static Monitor that doesn't actually monitor
@@ -526,15 +530,15 @@ func (m *Monitor) Close() error {
if m.wallTimer != nil {
m.wallTimer.Stop()
}
started := m.started
m.mu.Unlock()
// Close the OS monitor without holding m.mu; see the osMon lock order.
var err error
if m.om != nil {
err = m.om.Close()
}
started := m.started
m.mu.Unlock()
if started {
m.goroutines.Wait()
}
+49
View File
@@ -4,11 +4,13 @@
package netmon
import (
"errors"
"flag"
"net"
"net/netip"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
@@ -32,6 +34,53 @@ func TestMonitorStartClose(t *testing.T) {
}
}
// callbackWaitingOSMon is an osMon whose Close, like the Windows
// winipcfg Unregister methods, waits for an in-flight OS callback
// that needs the Monitor's mutex.
type callbackWaitingOSMon struct {
m *Monitor
closed chan struct{}
}
func (om *callbackWaitingOSMon) Close() error {
close(om.closed)
var wg sync.WaitGroup
wg.Go(func() { om.m.InterfaceState() })
wg.Wait()
return nil
}
func (om *callbackWaitingOSMon) Receive() (message, error) {
<-om.closed
return nil, errors.New("closed")
}
func TestMonitorCloseWaitsForOSCallback(t *testing.T) {
bus := eventbus.New()
defer bus.Close()
mon, err := New(bus, t.Logf)
if err != nil {
t.Fatal(err)
}
if err := mon.om.Close(); err != nil {
t.Fatal(err)
}
mon.om = &callbackWaitingOSMon{m: mon, closed: make(chan struct{})}
mon.Start()
done := make(chan error, 1)
go func() { done <- mon.Close() }()
select {
case err := <-done:
if err != nil {
t.Fatal(err)
}
case <-time.After(10 * time.Second):
t.Fatal("Monitor.Close deadlocked waiting for OS callback")
}
}
func TestMonitorJustClose(t *testing.T) {
bus := eventbus.New()
defer bus.Close()