Skip to content

Commit f3cda77

Browse files
authored
fix(core): prevent timer/ticker leaks; use unsafe.Add for pointer arithmetic (#9486)
This PR improves runtime safety and resource handling: - Timer: replaced time.After(...) with time.NewTimer(...) and ensured timer.Stop() is always called to avoid hidden leaks in loops. - Ticker: added ticker.Stop() and refactored time.Tick(...) usage to use time.NewTicker for proper lifecycle management. - Unsafe pointer: switched from unsafe.Pointer + uintptr(offset) to unsafe.Add(...) for clearer and more consistent pointer arithmetic. These changes make timer/ticker usage safer in long-running goroutines and align pointer operations with modern Go practices.
1 parent a96343e commit f3cda77

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

contrib/jepsen/main.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,18 @@ func jepsenServe() error {
238238
// command to finish.
239239
_ = cmd.Start()
240240
ticker := time.NewTicker(time.Second)
241+
defer ticker.Stop()
242+
243+
timeout := time.NewTimer(5 * time.Minute)
244+
defer timeout.Stop()
241245
for {
242246
select {
243-
case <-time.After(5 * time.Minute):
247+
case <-timeout.C:
244248
wg.Done()
245249
errCh <- errors.New("lein run serve couldn't run after 5 minutes")
246250
return
247251
case <-ticker.C:
248252
if err := checkServing(); err == nil {
249-
ticker.Stop()
250253
wg.Done()
251254
errCh <- nil
252255
return

dgraph/cmd/zero/zero.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,11 @@ func (s *Server) periodicallyPostTelemetry() {
106106

107107
glog.Infof("Starting telemetry data collection for zero...")
108108

109+
ticker := time.NewTicker(6 * time.Hour)
110+
defer ticker.Stop()
111+
109112
var lastPostedAt time.Time
110-
for ; true; <-time.Tick(time.Hour * 6) {
113+
for range ticker.C {
111114
if !s.Node.AmLeader() {
112115
continue
113116
}

posting/size.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ func (l *List) DeepSize() uint64 {
5353
hmap := reflect.ValueOf(l.mutationMap)
5454
// Note: this will fail if the -race detector flag is used with go tools (test, run),
5555
// see: https://github.com/golang/go/issues/48501
56-
numBuckets := int(math.Pow(2, float64((*(*uint8)(
57-
unsafe.Pointer(hmap.Pointer() + uintptr(9)))))))
58-
numOldBuckets := (*(*uint16)(unsafe.Pointer(hmap.Pointer() + uintptr(10))))
56+
numBuckets := int(math.Pow(2, float64(*(*uint8)(unsafe.Add(unsafe.Pointer(hmap.Pointer()), 9)))))
57+
numOldBuckets := *(*uint16)(unsafe.Add(unsafe.Pointer(hmap.Pointer()), 10))
5958
size += uint64(numOldBuckets * sizeOfBucket)
6059
if l.mutationMap.len() > 0 || numBuckets > 1 {
6160
size += uint64(numBuckets * sizeOfBucket)

x/x.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,6 +1227,7 @@ func StoreSync(db DB, closer *z.Closer) {
12271227
// We technically don't need to call this due to mmap being able to survive process crashes.
12281228
// But, once a minute is infrequent enough that we won't lose any performance due to this.
12291229
ticker := time.NewTicker(time.Minute)
1230+
defer ticker.Stop()
12301231
for {
12311232
select {
12321233
case <-ticker.C:

0 commit comments

Comments
 (0)