Skip to content

Commit 5d70cb0

Browse files
runtime: leave cleantimers early if G is being preempted
The cleantimers can run for a while in some unlikely cases. If the GC is trying to preempt the G, it is forced to wait as the G is holding timersLock. To avoid introducing a GC delay, return from cleantimers if the G has a preemption request. Fixes #37779 Change-Id: Id9a567f991e26668e2292eefc39e2edc56efa4e0 Reviewed-on: https://go-review.googlesource.com/c/go/+/223122 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Cherry Zhang <[email protected]>
1 parent 29b36a8 commit 5d70cb0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/runtime/time.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,10 +499,20 @@ func resettimer(t *timer, when int64) {
499499
// slows down addtimer. Reports whether no timer problems were found.
500500
// The caller must have locked the timers for pp.
501501
func cleantimers(pp *p) {
502+
gp := getg()
502503
for {
503504
if len(pp.timers) == 0 {
504505
return
505506
}
507+
508+
// This loop can theoretically run for a while, and because
509+
// it is holding timersLock it cannot be preempted.
510+
// If someone is trying to preempt us, just return.
511+
// We can clean the timers later.
512+
if gp.preemptStop {
513+
return
514+
}
515+
506516
t := pp.timers[0]
507517
if t.pp.ptr() != pp {
508518
throw("cleantimers: bad p")

0 commit comments

Comments
 (0)