src/runtime/runtime2.go | 4 ++++ src/runtime/signal_unix.go | 10 +++++++++- diff --git a/src/runtime/runtime2.go b/src/runtime/runtime2.go index 99eb19eb0c02e270eba165905bd6169736dc3bbc..2a872bfba9258805ad6f6bf74619c08906eaf4d7 100644 --- a/src/runtime/runtime2.go +++ b/src/runtime/runtime2.go @@ -540,6 +540,10 @@ // signals. This is used to detect when a preemption is // requested, but fails. Accessed atomically. preemptGen uint32 + // Whether this is a pending preemption signal on this M. + // Accessed atomically. + signalPending uint32 + dlogPerM mOS diff --git a/src/runtime/signal_unix.go b/src/runtime/signal_unix.go index d2e669380510cc3ed487f7378f5e1e5545dd4011..f18e6b5785d4e43f9a904e8d6a031e25670ffa70 100644 --- a/src/runtime/signal_unix.go +++ b/src/runtime/signal_unix.go @@ -333,6 +333,7 @@ } // Acknowledge the preemption. atomic.Xadd(&gp.m.preemptGen, 1) + atomic.Store(&gp.m.signalPending, 0) } const preemptMSupported = pushCallSupported @@ -359,7 +360,14 @@ // This can only happen in the go_bootstrap program (otherwise cgo is // required). return } - signalM(mp, sigPreempt) + if atomic.Cas(&mp.signalPending, 0, 1) { + // If multiple threads are preempting the same M, it may send many + // signals to the same M such that it hardly make progress, causing + // live-lock problem. Apparently this could happen on darwin. See + // issue #37741. + // Only send a signal if there isn't already one pending. + signalM(mp, sigPreempt) + } } // sigFetchG fetches the value of G safely when running in a signal handler.