// func netpollclose(fd uintptr) int32
 //     Disable notifications for fd. Return an errno value.
 //
-// func netpoll(delta int64) gList
+// func netpoll(delta int64) (gList, int32)
 //     Poll the network. If delta < 0, block indefinitely. If delta == 0,
 //     poll without blocking. If delta > 0, block for up to delta nanoseconds.
-//     Return a list of goroutines built by calling netpollready.
+//     Return a list of goroutines built by calling netpollready,
+//     and a delta to add to netpollWaiters when all goroutines are ready.
+//     This will never return an empty list with a non-zero delta.
 //
 // func netpollBreak()
 //     Wake up the network poller, assumed to be blocked in netpoll.
        }
        // If we set the new deadline in the past, unblock currently pending IO if any.
        // Note that pd.publishInfo has already been called, above, immediately after modifying rd and wd.
+       delta := int32(0)
        var rg, wg *g
        if pd.rd < 0 {
-               rg = netpollunblock(pd, 'r', false)
+               rg = netpollunblock(pd, 'r', false, &delta)
        }
        if pd.wd < 0 {
-               wg = netpollunblock(pd, 'w', false)
+               wg = netpollunblock(pd, 'w', false, &delta)
        }
        unlock(&pd.lock)
        if rg != nil {
        if wg != nil {
                netpollgoready(wg, 3)
        }
+       netpollAdjustWaiters(delta)
 }
 
 //go:linkname poll_runtime_pollUnblock internal/poll.runtime_pollUnblock
        pd.wseq++
        var rg, wg *g
        pd.publishInfo()
-       rg = netpollunblock(pd, 'r', false)
-       wg = netpollunblock(pd, 'w', false)
+       delta := int32(0)
+       rg = netpollunblock(pd, 'r', false, &delta)
+       wg = netpollunblock(pd, 'w', false, &delta)
        if pd.rt.f != nil {
                deltimer(&pd.rt)
                pd.rt.f = nil
        if wg != nil {
                netpollgoready(wg, 3)
        }
+       netpollAdjustWaiters(delta)
 }
 
 // netpollready is called by the platform-specific netpoll function.
 // from netpoll. The mode argument is 'r', 'w', or 'r'+'w' to indicate
 // whether the fd is ready for reading or writing or both.
 //
+// This returns a delta to apply to netpollWaiters.
+//
 // This may run while the world is stopped, so write barriers are not allowed.
 //
 //go:nowritebarrier
-func netpollready(toRun *gList, pd *pollDesc, mode int32) {
+func netpollready(toRun *gList, pd *pollDesc, mode int32) int32 {
+       delta := int32(0)
        var rg, wg *g
        if mode == 'r' || mode == 'r'+'w' {
-               rg = netpollunblock(pd, 'r', true)
+               rg = netpollunblock(pd, 'r', true, &delta)
        }
        if mode == 'w' || mode == 'r'+'w' {
-               wg = netpollunblock(pd, 'w', true)
+               wg = netpollunblock(pd, 'w', true, &delta)
        }
        if rg != nil {
                toRun.push(rg)
        if wg != nil {
                toRun.push(wg)
        }
+       return delta
 }
 
 func netpollcheckerr(pd *pollDesc, mode int32) int {
                // Bump the count of goroutines waiting for the poller.
                // The scheduler uses this to decide whether to block
                // waiting for the poller if there is nothing else to do.
-               netpollWaiters.Add(1)
+               netpollAdjustWaiters(1)
        }
        return r
 }
        return old == pdReady
 }
 
-func netpollunblock(pd *pollDesc, mode int32, ioready bool) *g {
+// netpollunblock moves either pd.rg (if mode == 'r') or
+// pd.wg (if mode == 'w') into the pdReady state.
+// This returns any goroutine blocked on pd.{rg,wg}.
+// It adds any adjustment to netpollWaiters to *delta;
+// this adjustment should be applied after the goroutine has
+// been marked ready.
+func netpollunblock(pd *pollDesc, mode int32, ioready bool, delta *int32) *g {
        gpp := &pd.rg
        if mode == 'w' {
                gpp = &pd.wg
                        if old == pdWait {
                                old = pdNil
                        } else if old != pdNil {
-                               netpollWaiters.Add(-1)
+                               *delta -= 1
                        }
                        return (*g)(unsafe.Pointer(old))
                }
                unlock(&pd.lock)
                return
        }
+       delta := int32(0)
        var rg *g
        if read {
                if pd.rd <= 0 || pd.rt.f == nil {
                }
                pd.rd = -1
                pd.publishInfo()
-               rg = netpollunblock(pd, 'r', false)
+               rg = netpollunblock(pd, 'r', false, &delta)
        }
        var wg *g
        if write {
                }
                pd.wd = -1
                pd.publishInfo()
-               wg = netpollunblock(pd, 'w', false)
+               wg = netpollunblock(pd, 'w', false, &delta)
        }
        unlock(&pd.lock)
        if rg != nil {
        if wg != nil {
                netpollgoready(wg, 0)
        }
+       netpollAdjustWaiters(delta)
 }
 
 func netpollDeadline(arg any, seq uintptr) {
        netpolldeadlineimpl(arg.(*pollDesc), seq, false, true)
 }
 
+// netpollAnyWaiters reports whether any goroutines are waiting for I/O.
+func netpollAnyWaiters() bool {
+       return netpollWaiters.Load() > 0
+}
+
+// netpollAdjustWaiters adds delta to netpollWaiters.
+func netpollAdjustWaiters(delta int32) {
+       if delta != 0 {
+               netpollWaiters.Add(delta)
+       }
+}
+
 func (c *pollCache) alloc() *pollDesc {
        lock(&c.lock)
        if c.first == nil {
 
 // delay > 0: block for up to that many nanoseconds
 //
 //go:nowritebarrierrec
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        var timeout uintptr
        if delay < 0 {
                timeout = ^uintptr(0)
        } else if delay == 0 {
                // TODO: call poll with timeout == 0
-               return gList{}
+               return gList{}, 0
        } else if delay < 1e6 {
                timeout = 1
        } else if delay < 1e15 {
                // If a timed sleep was interrupted, just return to
                // recalculate how long we should sleep now.
                if timeout > 0 {
-                       return gList{}
+                       return gList{}, 0
                }
                goto retry
        }
                n--
        }
        var toRun gList
+       delta := int32(0)
        for i := 1; i < len(pfds) && n > 0; i++ {
                pfd := &pfds[i]
 
                }
                if mode != 0 {
                        pds[i].setEventErr(pfd.revents == _POLLERR, 0)
-                       netpollready(&toRun, pds[i], mode)
+                       delta += netpollready(&toRun, pds[i], mode)
                        n--
                }
        }
        unlock(&mtxset)
-       return toRun
+       return toRun, delta
 }
 
 // delay < 0: blocks indefinitely
 // delay == 0: does not block, just polls
 // delay > 0: block for up to that many nanoseconds
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        if epfd == -1 {
-               return gList{}
+               return gList{}, 0
        }
        var waitms int32
        if delay < 0 {
                // If a timed sleep was interrupted, just return to
                // recalculate how long we should sleep now.
                if waitms > 0 {
-                       return gList{}
+                       return gList{}, 0
                }
                goto retry
        }
        var toRun gList
+       delta := int32(0)
        for i := int32(0); i < n; i++ {
                ev := events[i]
                if ev.Events == 0 {
                        tag := tp.tag()
                        if pd.fdseq.Load() == tag {
                                pd.setEventErr(ev.Events == syscall.EPOLLERR, tag)
-                               netpollready(&toRun, pd, mode)
+                               delta += netpollready(&toRun, pd, mode)
                        }
                }
        }
-       return toRun
+       return toRun, delta
 }
 
 func netpollBreak() {
 }
 
-func netpoll(delay int64) gList {
-       return gList{}
+func netpoll(delay int64) (gList, int32) {
+       return gList{}, 0
 }
 
 // delay < 0: blocks indefinitely
 // delay == 0: does not block, just polls
 // delay > 0: block for up to that many nanoseconds
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        if kq == -1 {
-               return gList{}
+               return gList{}, 0
        }
        var tp *timespec
        var ts timespec
                // If a timed sleep was interrupted, just return to
                // recalculate how long we should sleep now.
                if delay > 0 {
-                       return gList{}
+                       return gList{}, 0
                }
                goto retry
        }
        var toRun gList
+       delta := int32(0)
        for i := 0; i < int(n); i++ {
                ev := &events[i]
 
                                }
                        }
                        pd.setEventErr(ev.flags == _EV_ERROR, tag)
-                       netpollready(&toRun, pd, mode)
+                       delta += netpollready(&toRun, pd, mode)
                }
        }
-       return toRun
+       return toRun, delta
 }
 
 // delay < 0: blocks indefinitely
 // delay == 0: does not block, just polls
 // delay > 0: block for up to that many nanoseconds
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        if portfd == -1 {
-               return gList{}
+               return gList{}, 0
        }
 
        var wait *timespec
                // If a timed sleep was interrupted and there are no events,
                // just return to recalculate how long we should sleep now.
                if delay > 0 {
-                       return gList{}
+                       return gList{}, 0
                }
                goto retry
        }
 
        var toRun gList
+       delta := int32(0)
        for i := 0; i < int(n); i++ {
                ev := &events[i]
 
                        // about the event port on SmartOS.
                        //
                        // See golang.org/x/issue/30840.
-                       netpollready(&toRun, pd, mode)
+                       delta += netpollready(&toRun, pd, mode)
                }
        }
 
-       return toRun
+       return toRun, delta
 }
 
 import "runtime/internal/atomic"
 
 var netpollInited atomic.Uint32
-var netpollWaiters atomic.Uint32
 
 var netpollStubLock mutex
 var netpollNote note
 
 // Polls for ready network connections.
 // Returns list of goroutines that become runnable.
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        // Implementation for platforms that do not support
        // integrated network poller.
        if delay != 0 {
                // (eg when running TestNetpollBreak).
                osyield()
        }
-       return gList{}
+       return gList{}, 0
 }
 
 func netpollinited() bool {
        return netpollInited.Load() != 0
 }
+
+func netpollAnyWaiters() bool {
+       return false
+}
+
+func netpollAdjustWaiters(delta int32) {
+}
 
 
 func netpollBreak() {}
 
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        lock(&mtx)
 
        // If delay >= 0, we include a subscription of type Clock that we use as
 
        if len(pollsubs) == 0 {
                unlock(&mtx)
-               return gList{}
+               return gList{}, 0
        }
 
        evts = evts[:len(pollsubs)]
                // recalculate how long we should sleep now.
                if delay > 0 {
                        unlock(&mtx)
-                       return gList{}
+                       return gList{}, 0
                }
                goto retry
        }
 
        var toRun gList
+       delta := int32(0)
        for i := 0; i < int(nevents); i++ {
                e := &evts[i]
                if e.typ == eventtypeClock {
                        pd := (*pollDesc)(unsafe.Pointer(uintptr(e.userdata)))
                        netpolldisarm(pd, mode)
                        pd.setEventErr(e.error != 0, 0)
-                       netpollready(&toRun, pd, mode)
+                       delta += netpollready(&toRun, pd, mode)
                }
        }
 
        unlock(&mtx)
-       return toRun
+       return toRun, delta
 }
 
 // delay < 0: blocks indefinitely
 // delay == 0: does not block, just polls
 // delay > 0: block for up to that many nanoseconds
-func netpoll(delay int64) gList {
+func netpoll(delay int64) (gList, int32) {
        var entries [64]overlappedEntry
        var wait, qty, flags, n, i uint32
        var errno int32
        mp := getg().m
 
        if iocphandle == _INVALID_HANDLE_VALUE {
-               return gList{}
+               return gList{}, 0
        }
        if delay < 0 {
                wait = _INFINITE
                mp.blocked = false
                errno = int32(getlasterror())
                if errno == _WAIT_TIMEOUT {
-                       return gList{}
+                       return gList{}, 0
                }
                println("runtime: GetQueuedCompletionStatusEx failed (errno=", errno, ")")
                throw("runtime: netpoll failed")
        }
        mp.blocked = false
+       delta := int32(0)
        for i = 0; i < n; i++ {
                op = entries[i].op
                if op != nil && op.pd == entries[i].key {
                        if stdcall5(_WSAGetOverlappedResult, op.pd.fd, uintptr(unsafe.Pointer(op)), uintptr(unsafe.Pointer(&qty)), 0, uintptr(unsafe.Pointer(&flags))) == 0 {
                                errno = int32(getlasterror())
                        }
-                       handlecompletion(&toRun, op, errno, qty)
+                       delta += handlecompletion(&toRun, op, errno, qty)
                } else {
                        netpollWakeSig.Store(0)
                        if delay == 0 {
                        }
                }
        }
-       return toRun
+       return toRun, delta
 }
 
-func handlecompletion(toRun *gList, op *net_op, errno int32, qty uint32) {
+func handlecompletion(toRun *gList, op *net_op, errno int32, qty uint32) int32 {
        mode := op.mode
        if mode != 'r' && mode != 'w' {
                println("runtime: GetQueuedCompletionStatusEx returned invalid mode=", mode)
        }
        op.errno = errno
        op.qty = qty
-       netpollready(toRun, op.pd, mode)
+       return netpollready(toRun, op.pd, mode)
 }
 
 
        mp := acquirem() // disable preemption because it can be holding p in a local var
        if netpollinited() {
-               list := netpoll(0) // non-blocking
+               list, delta := netpoll(0) // non-blocking
                injectglist(&list)
+               netpollAdjustWaiters(delta)
        }
        lock(&sched.lock)
 
        // blocked thread (e.g. it has already returned from netpoll, but does
        // not set lastpoll yet), this thread will do blocking netpoll below
        // anyway.
-       if netpollinited() && netpollWaiters.Load() > 0 && sched.lastpoll.Load() != 0 {
-               if list := netpoll(0); !list.empty() { // non-blocking
+       if netpollinited() && netpollAnyWaiters() && sched.lastpoll.Load() != 0 {
+               if list, delta := netpoll(0); !list.empty() { // non-blocking
                        gp := list.pop()
                        injectglist(&list)
+                       netpollAdjustWaiters(delta)
                        casgstatus(gp, _Gwaiting, _Grunnable)
                        if traceEnabled() {
                                traceGoUnpark(gp, 0)
        }
 
        // Poll network until next timer.
-       if netpollinited() && (netpollWaiters.Load() > 0 || pollUntil != 0) && sched.lastpoll.Swap(0) != 0 {
+       if netpollinited() && (netpollAnyWaiters() || pollUntil != 0) && sched.lastpoll.Swap(0) != 0 {
                sched.pollUntil.Store(pollUntil)
                if mp.p != 0 {
                        throw("findrunnable: netpoll with p")
                        // When using fake time, just poll.
                        delay = 0
                }
-               list := netpoll(delay) // block until new work is available
+               list, delta := netpoll(delay) // block until new work is available
                // Refresh now again, after potentially blocking.
                now = nanotime()
                sched.pollUntil.Store(0)
                unlock(&sched.lock)
                if pp == nil {
                        injectglist(&list)
+                       netpollAdjustWaiters(delta)
                } else {
                        acquirep(pp)
                        if !list.empty() {
                                gp := list.pop()
                                injectglist(&list)
+                               netpollAdjustWaiters(delta)
                                casgstatus(gp, _Gwaiting, _Grunnable)
                                if traceEnabled() {
                                        traceGoUnpark(gp, 0)
        if !runqempty(p) {
                return true
        }
-       if netpollinited() && netpollWaiters.Load() > 0 && sched.lastpoll.Load() != 0 {
-               if list := netpoll(0); !list.empty() {
+       if netpollinited() && netpollAnyWaiters() && sched.lastpoll.Load() != 0 {
+               if list, delta := netpoll(0); !list.empty() {
                        injectglist(&list)
+                       netpollAdjustWaiters(delta)
                        return true
                }
        }
                lastpoll := sched.lastpoll.Load()
                if netpollinited() && lastpoll != 0 && lastpoll+10*1000*1000 < now {
                        sched.lastpoll.CompareAndSwap(lastpoll, now)
-                       list := netpoll(0) // non-blocking - returns list of goroutines
+                       list, delta := netpoll(0) // non-blocking - returns list of goroutines
                        if !list.empty() {
                                // Need to decrement number of idle locked M's
                                // (pretending that one more is running) before injectglist.
                                incidlelocked(-1)
                                injectglist(&list)
                                incidlelocked(1)
+                               netpollAdjustWaiters(delta)
                        }
                }
                if GOOS == "netbsd" && needSysmonWorkaround {