[go] runtime: don't steal work when a STW is pending

5 views
Skip to first unread message

Rhys Hiltner (Gerrit)

unread,
Sep 23, 2026, 9:48:18 PMSep 23
to goph...@pubsubhelper.golang.org, Rhys Hiltner, golang-co...@googlegroups.com

Rhys Hiltner has uploaded the change for review

Commit message

runtime: don't steal work when a STW is pending

A spinning M's role is to find runnable goroutines for its P, stealing
from other P's runnext slot as a last resort. When it comes to that, the
spinning M does a brief sleep to double-check that the victim P isn't
about to run the runnext goroutine itself. The target of the sleep is
3us, but the OS's timer slack can stretch it to beyond 50us.

During that sleep, another thread may have announced a stop-the-world.
When the spinning M wakes up, it easily steals the runnext goroutine
(the victim P has observed the STW request and has gone to sleep),
returns it to findRunnable, and soon the (formerly) spinning M begins
executing a goroutine even though the rest of the world is trying to
come to a stop.

After that wakeup, double-check that there's no pending STW before
committing to the runnext steal.

For #81657
Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1

Change diff

diff --git a/src/runtime/proc.go b/src/runtime/proc.go
index b49f132..bb18ed2 100644
--- a/src/runtime/proc.go
+++ b/src/runtime/proc.go
@@ -7765,6 +7765,11 @@
// optimization. So just yield.
osyield()
}
+ if sched.gcwaiting.Load() {
+ // The sleep above might have overlapped with a STW
+ // request. Check before committing to this new work.
+ return 0
+ }
}
}
}

Change information

Files:
  • M src/runtime/proc.go
Change size: XS
Delta: 1 file changed, 5 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
Gerrit-Change-Number: 837886
Gerrit-PatchSet: 1
Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Rhys Hiltner (Gerrit)

unread,
Sep 23, 2026, 9:49:46 PMSep 23
to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Rhys Hiltner voted

Auto-Submit+1
Commit-Queue+1
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
Gerrit-Change-Number: 837886
Gerrit-PatchSet: 1
Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
Gerrit-Reviewer: Rhys Hiltner <rhys.h...@gmail.com>
Gerrit-Comment-Date: Thu, 24 Sep 2026 01:49:41 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy

Michael Knyszek (Gerrit)

unread,
Sep 24, 2026, 8:50:43 PMSep 24
to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Michael Pratt and Rhys Hiltner

Michael Knyszek voted and added 1 comment

Votes added by Michael Knyszek

Code-Review+2

1 comment

File src/runtime/proc.go
Line 3854, Patchset 1 (Latest): if sched.gcwaiting.Load() {
// GC work may be available.
return nil, false, now, pollUntil, true
}
Michael Knyszek . unresolved

maybe we want to move this after runqsteal. it's a bit weird that we can return from it early because of gcwaiting, but still end up returning with newWork=false.

Open in Gerrit

Related details

Attention is currently required from:
  • Michael Pratt
  • Rhys Hiltner
Submit Requirements:
  • requirement satisfiedCode-Review
  • requirement is not satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
Gerrit-Change-Number: 837886
Gerrit-PatchSet: 1
Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: Rhys Hiltner <rhys.h...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Attention: Michael Pratt <mpr...@google.com>
Gerrit-Attention: Rhys Hiltner <rhys.h...@gmail.com>
Gerrit-Comment-Date: Fri, 25 Sep 2026 00:50:36 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
satisfied_requirement
unsatisfied_requirement
open
diffy

Rhys Hiltner (Gerrit)

unread,
Sep 25, 2026, 4:46:09 PMSep 25
to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Michael Pratt and Rhys Hiltner

Rhys Hiltner uploaded new patchset

Rhys Hiltner uploaded patch set #2 to this change.
Following approvals got outdated and were removed:
Open in Gerrit

Related details

Attention is currently required from:
  • Michael Pratt
  • Rhys Hiltner
Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
    Gerrit-Change-Number: 837886
    Gerrit-PatchSet: 2
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Rhys Hiltner (Gerrit)

    unread,
    Sep 25, 2026, 4:52:16 PMSep 25
    to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Michael Pratt

    Rhys Hiltner added 1 comment

    File src/runtime/proc.go
    Line 3854, Patchset 1: if sched.gcwaiting.Load() {

    // GC work may be available.
    return nil, false, now, pollUntil, true
    }
    Michael Knyszek . resolved

    maybe we want to move this after runqsteal. it's a bit weird that we can return from it early because of gcwaiting, but still end up returning with newWork=false.

    Rhys Hiltner

    Checking after sounds good, I've moved it down.

    (That delays the first check in stealWork, and grows one of the time windows where the scheduler goes without checking sched.gcwaiting. If it turns out that's a problem, we can come up with more general guidance on how frequently the scheduler should re-check that condition.)

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Pratt
    Submit Requirements:
    • requirement satisfiedCode-Review
    • requirement satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
    Gerrit-Change-Number: 837886
    Gerrit-PatchSet: 2
    Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
    Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
    Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
    Gerrit-Reviewer: Rhys Hiltner <rhys.h...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Attention: Michael Pratt <mpr...@google.com>
    Gerrit-Comment-Date: Fri, 25 Sep 2026 20:52:09 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Michael Knyszek <mkny...@google.com>
    satisfied_requirement
    unsatisfied_requirement
    open
    diffy

    Rhys Hiltner (Gerrit)

    unread,
    Sep 25, 2026, 5:16:05 PMSep 25
    to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Pratt, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Michael Pratt

    Rhys Hiltner voted Auto-Submit+1

    Auto-Submit+1
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Michael Pratt
    Submit Requirements:
      • requirement satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
      Gerrit-Change-Number: 837886
      Gerrit-PatchSet: 2
      Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
      Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
      Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
      Gerrit-Reviewer: Rhys Hiltner <rhys.h...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-Attention: Michael Pratt <mpr...@google.com>
      Gerrit-Comment-Date: Fri, 25 Sep 2026 21:15:58 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      satisfied_requirement
      unsatisfied_requirement
      open
      diffy

      Michael Pratt (Gerrit)

      unread,
      Sep 28, 2026, 5:47:26 PMSep 28
      to Rhys Hiltner, goph...@pubsubhelper.golang.org, Michael Pratt, golang...@luci-project-accounts.iam.gserviceaccount.com, Gopher Robot, golang-co...@googlegroups.com
      Attention needed from Rhys Hiltner

      Michael Pratt voted Code-Review+2

      Code-Review+2
      Open in Gerrit

      Related details

      Attention is currently required from:
      • Rhys Hiltner
      Submit Requirements:
        • requirement satisfiedCode-Review
        • requirement satisfiedNo-Unresolved-Comments
        • requirement satisfiedReview-Enforcement
        • requirement satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: comment
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
        Gerrit-Change-Number: 837886
        Gerrit-PatchSet: 2
        Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
        Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
        Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
        Gerrit-Reviewer: Rhys Hiltner <rhys.h...@gmail.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        Gerrit-Attention: Rhys Hiltner <rhys.h...@gmail.com>
        Gerrit-Comment-Date: Mon, 28 Sep 2026 21:47:22 +0000
        Gerrit-HasComments: No
        Gerrit-Has-Labels: Yes
        satisfied_requirement
        open
        diffy

        Gopher Robot (Gerrit)

        unread,
        Sep 28, 2026, 5:52:02 PMSep 28
        to Rhys Hiltner, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Michael Pratt, golang...@luci-project-accounts.iam.gserviceaccount.com, Michael Knyszek, golang-co...@googlegroups.com

        Gopher Robot submitted the change

        Change information

        Commit message:
        runtime: don't steal work when a STW is pending

        A spinning M's role is to find runnable goroutines for its P, stealing
        from other P's runnext slot as a last resort. When it comes to that, the
        spinning M does a brief sleep to double-check that the victim P isn't
        about to run the runnext goroutine itself. The target of the sleep is
        3us, but the OS's timer slack can stretch it to beyond 50us.

        During that sleep, another thread may have announced a stop-the-world.
        When the spinning M wakes up, it easily steals the runnext goroutine
        (the victim P has observed the STW request and has gone to sleep),
        returns it to findRunnable, and soon the (formerly) spinning M begins
        executing a goroutine even though the rest of the world is trying to
        come to a stop.

        After that wakeup, double-check that there's no pending STW before
        committing to the runnext steal.

        For #81657
        Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
        Auto-Submit: Rhys Hiltner <rhys.h...@gmail.com>
        Reviewed-by: Michael Pratt <mpr...@google.com>
        Reviewed-by: Michael Knyszek <mkny...@google.com>
        Files:
        • M src/runtime/proc.go
        Change size: S
        Delta: 1 file changed, 11 insertions(+), 4 deletions(-)
        Branch: refs/heads/master
        Submit Requirements:
        Open in Gerrit
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: merged
        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: I616416e3a43838123c8ac8a903096d9ccd5504c1
        Gerrit-Change-Number: 837886
        Gerrit-PatchSet: 3
        Gerrit-Owner: Rhys Hiltner <rhys.h...@gmail.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        open
        diffy
        satisfied_requirement
        Reply all
        Reply to author
        Forward
        0 new messages