[go] time: detect copies of Timer and Ticker

0 views
Skip to first unread message

Gopher Robot (Gerrit)

unread,
Aug 10, 2026, 4:29:23 PM (13 hours ago) Aug 10
to Michael Pratt, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, golang...@luci-project-accounts.iam.gserviceaccount.com, Cherry Mui, Ian Lance Taylor, Carlos Amedee, Michael Knyszek, Alan Donovan, Russ Cox, golang-co...@googlegroups.com

Gopher Robot submitted the change

Unreviewed changes

2 is the latest approved patch-set.
No files were changed between the latest approved patch-set and the submitted one.

Change information

Commit message:
time: detect copies of Timer and Ticker

The public Timer and Ticker types are only the beginning of the larger
runtime.timeTimer types. User allocations of the public types will not
initialize the runtime-only portion of the type. The existing init bool
fields allow the methods to detect user allocations and panic.

Copies have a similar problem. If a user copies a valid Timer or Ticker,
only the beginning of the actual type is copied and it will not be
usable [1]. The init bool is not sufficient to detect this, as a copy
will copy the bool field. Instead, change the init bool to a self
pointer. If the object is copied, the pointer will no longer match.

Alternatively, we could use a pointer field to a separate runtime type.
This would make the type safe to copy. This was the initial approach in
patch set 1 of CL 568339, but it was changed to avoid the extra
allocation.

[1] Even if the entire structure was defined in time, copying would
still not be safe, as runtime.timeTimer is not safe to copy. e.g., it
contains mutex values. This is what CL 568339 was addressing.

Fixes #73886.
Change-Id: I6a6a636ce703c0e946aa9ee545f756b60eafff0a
Auto-Submit: Michael Pratt <mpr...@google.com>
Reviewed-by: Ian Lance Taylor <ia...@golang.org>
Reviewed-by: Cherry Mui <cher...@google.com>
Files:
  • M src/runtime/time.go
  • M src/runtime/time_test.go
  • M src/time/sleep.go
  • M src/time/sleep_test.go
  • M src/time/tick.go
  • M src/time/tick_test.go
Change size: M
Delta: 6 files changed, 121 insertions(+), 22 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: I6a6a636ce703c0e946aa9ee545f756b60eafff0a
Gerrit-Change-Number: 676537
Gerrit-PatchSet: 4
Gerrit-Owner: Michael Pratt <mpr...@google.com>
Gerrit-Reviewer: Carlos Amedee <car...@golang.org>
Gerrit-Reviewer: Cherry Mui <cher...@google.com>
Gerrit-Reviewer: Gopher Robot <go...@golang.org>
Gerrit-Reviewer: Ian Lance Taylor <ia...@golang.org>
Gerrit-Reviewer: Michael Knyszek <mkny...@google.com>
Gerrit-Reviewer: Michael Pratt <mpr...@google.com>
Gerrit-CC: Alan Donovan <adon...@google.com>
Gerrit-CC: Russ Cox <r...@golang.org>
open
diffy
satisfied_requirement
Reply all
Reply to author
Forward
0 new messages