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.

Fixes #22228

Signed-off-by: Nick Khyl <[email protected]>
This commit is contained in:
Nick Khyl committed 2026-11-06 18:19:39 -08:00
1 parent 46f938791d
commit f6fbcf8411
1 file changed
+8 -4
+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
@@ -522,15 +526,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()
}