Help us shape the future of Go! Take the 2026 Go Developer Survey

Source file src/runtime/race/testdata/issue82771_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package race_test
     6  
     7  import (
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  // golang.org/issue/82771
    13  // A race detected inside a channel timer's sendTime, run by the
    14  // scheduler on g0, used to crash the process with SIGSEGV while
    15  // symbolizing the report instead of reporting the race.
    16  func TestRaceIssue82771(t *testing.T) {
    17  	// The first tick must come after the write below, so that the race
    18  	// is detected by the read in sendTime rather than by the write.
    19  	ticker := time.NewTicker(50 * time.Millisecond)
    20  	defer ticker.Stop()
    21  
    22  	// A goroutine blocked on the channel keeps the timer in the heap,
    23  	// so the scheduler runs sendTime when the P is otherwise idle.
    24  	go func() {
    25  		for range ticker.C {
    26  		}
    27  	}()
    28  
    29  	time.Sleep(10 * time.Millisecond)
    30  	// Unsynchronized write; sendTime calls time.Now, which reads time.Local.
    31  	// Storing the current value keeps other tests unaffected.
    32  	time.Local = issue82771Local()
    33  	time.Sleep(100 * time.Millisecond)
    34  }
    35  
    36  // issue82771Local returns time.Local. It is not inlined so that the
    37  // compiler cannot optimize away the store of time.Local to itself.
    38  //
    39  //go:noinline
    40  func issue82771Local() *time.Location {
    41  	return time.Local
    42  }
    43  

View as plain text