Source file src/runtime/testdata/testgoroutineleakprofile/goker/moby4951.go

     1  // Copyright 2025 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a MIT
     3  // license that can be found in the LICENSE file.
     4  
     5  /*
     6   * Project: moby
     7   * Issue or PR  : https://github.com/moby/moby/pull/4951
     8   * Buggy version: 81f148be566ab2b17810ad4be61a5d8beac8330f
     9   * fix commit-id: 2ffef1b7eb618162673c6ffabccb9ca57c7dfce3
    10   * Flaky: 100/100
    11   */
    12  package main
    13  
    14  import (
    15  	"os"
    16  	"runtime"
    17  	"runtime/pprof"
    18  	"sync"
    19  	"time"
    20  )
    21  
    22  func init() {
    23  	register("Moby4951", Moby4951)
    24  }
    25  
    26  type DeviceSet_moby4951 struct {
    27  	sync.Mutex
    28  	infos            map[string]*DevInfo_moby4951
    29  	nrDeletedDevices int
    30  }
    31  
    32  func (devices *DeviceSet_moby4951) DeleteDevice(hash string) {
    33  	devices.Lock()
    34  	defer devices.Unlock()
    35  
    36  	info := devices.lookupDevice(hash)
    37  
    38  	info.lock.Lock()
    39  	runtime.Gosched()
    40  	defer info.lock.Unlock()
    41  
    42  	devices.deleteDevice(info)
    43  }
    44  
    45  func (devices *DeviceSet_moby4951) lookupDevice(hash string) *DevInfo_moby4951 {
    46  	existing, ok := devices.infos[hash]
    47  	if !ok {
    48  		return nil
    49  	}
    50  	return existing
    51  }
    52  
    53  func (devices *DeviceSet_moby4951) deleteDevice(info *DevInfo_moby4951) {
    54  	devices.removeDeviceAndWait(info.Name())
    55  }
    56  
    57  func (devices *DeviceSet_moby4951) removeDeviceAndWait(devname string) {
    58  	/// remove devices by devname
    59  	devices.Unlock()
    60  	time.Sleep(300 * time.Nanosecond)
    61  	devices.Lock()
    62  }
    63  
    64  type DevInfo_moby4951 struct {
    65  	lock sync.Mutex
    66  	name string
    67  }
    68  
    69  func (info *DevInfo_moby4951) Name() string {
    70  	return info.name
    71  }
    72  
    73  func NewDeviceSet_moby4951() *DeviceSet_moby4951 {
    74  	devices := &DeviceSet_moby4951{
    75  		infos: make(map[string]*DevInfo_moby4951),
    76  	}
    77  	info1 := &DevInfo_moby4951{
    78  		name: "info1",
    79  	}
    80  	info2 := &DevInfo_moby4951{
    81  		name: "info2",
    82  	}
    83  	devices.infos[info1.name] = info1
    84  	devices.infos[info2.name] = info2
    85  	return devices
    86  }
    87  
    88  func Moby4951() {
    89  	prof := pprof.Lookup("goroutineleak")
    90  	defer func() {
    91  		time.Sleep(100 * time.Millisecond)
    92  		prof.WriteTo(os.Stdout, 2)
    93  	}()
    94  
    95  	go func() {
    96  		ds := NewDeviceSet_moby4951()
    97  		/// Delete devices by the same info
    98  		go ds.DeleteDevice("info1")
    99  		go ds.DeleteDevice("info1")
   100  	}()
   101  }
   102  

View as plain text