Skip to content

Commit 24070cf

Browse files
panjf2000prattmic
authored andcommitted
runtime: fix the potential race of idle P's during injectglist
Fixes #63555 Change-Id: I26e7baf58fcb78e66e0feed5725e371e25d657cc Reviewed-on: https://go-review.googlesource.com/c/go/+/550175 Reviewed-by: Michael Pratt <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Carlos Amedee <[email protected]>
1 parent 234390e commit 24070cf

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/runtime/proc.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,8 +3816,10 @@ func injectglist(glist *gList) {
38163816
}
38173817

38183818
npidle := int(sched.npidle.Load())
3819-
var globq gQueue
3820-
var n int
3819+
var (
3820+
globq gQueue
3821+
n int
3822+
)
38213823
for n = 0; n < npidle && !q.empty(); n++ {
38223824
g := q.pop()
38233825
globq.pushBack(g)
@@ -3833,6 +3835,21 @@ func injectglist(glist *gList) {
38333835
if !q.empty() {
38343836
runqputbatch(pp, &q, qsize)
38353837
}
3838+
3839+
// Some P's might have become idle after we loaded `sched.npidle`
3840+
// but before any goroutines were added to the queue, which could
3841+
// lead to idle P's when there is work available in the global queue.
3842+
// That could potentially last until other goroutines become ready
3843+
// to run. That said, we need to find a way to hedge
3844+
//
3845+
// Calling wakep() here is the best bet, it will do nothing in the
3846+
// common case (no racing on `sched.npidle`), while it could wake one
3847+
// more P to execute G's, which might end up with >1 P's: the first one
3848+
// wakes another P and so forth until there is no more work, but this
3849+
// ought to be an extremely rare case.
3850+
//
3851+
// Also see "Worker thread parking/unparking" comment at the top of the file for details.
3852+
wakep()
38363853
}
38373854

38383855
// One round of scheduler: find a runnable goroutine and execute it.

0 commit comments

Comments
 (0)