]> Cypherpunks.ru repositories - gostls13.git/commit
runtime: prevent send on closed channel in wakeableSleep
authorMichael Anthony Knyszek <mknyszek@google.com>
Tue, 14 Nov 2023 18:56:21 +0000 (18:56 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 14 Nov 2023 20:12:39 +0000 (20:12 +0000)
commit3073f3f9411737de2232e6f6d634c118b53aed22
tree8a8a2b9879dbbbe43dd1c67e0482c6b32f29cd8e
parent67d555e089180aaa39993729b7f9693ea12d6ea6
runtime: prevent send on closed channel in wakeableSleep

Currently wakeableSleep has a race where, although stopTimer is called,
the timer could be queued already and fire *after* the wakeup channel is
closed.

Fix this by protecting wakeup with a lock used on the close and wake
paths and assigning the wakeup to nil on close. The wake path then
ignores a nil wakeup channel. This fixes the problem by ensuring that a
failure to stop the timer only results in the timer doing nothing,
rather than trying to send on a closed channel.

The addition of this lock requires some changes to the static lock
ranking system.

Thiere's also a second problem here: the timer could be delayed far
enough into the future that when it fires, it observes a non-nil wakeup
if the wakeableSleep has been re-initialized and reset.

Fix this problem too  by allocating the wakeableSleep on the heap and
creating a new one instead of reinitializing the old one. The GC will
make sure that the reference to the old one stays alive for the timer to
fire, but that timer firing won't cause a spurious wakeup in the new
one.

Change-Id: I2b979304e755c015d4466991f135396f6a271069
Reviewed-on: https://go-review.googlesource.com/c/go/+/542335
Reviewed-by: Michael Pratt <mpratt@google.com>
Commit-Queue: Michael Knyszek <mknyszek@google.com>
Auto-Submit: Michael Knyszek <mknyszek@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Knyszek <mknyszek@google.com>
src/runtime/lockrank.go
src/runtime/mklockrank.go
src/runtime/trace2.go
src/runtime/trace2cpu.go