@@ -41,45 +41,48 @@ func (pb *PerfBuffer) Start() {
4141}
4242
4343func (pb * PerfBuffer ) Stop () {
44- if pb .stop != nil {
45- // Tell the poll goroutine that it's time to exit
46- close (pb .stop )
47-
48- // The event and lost channels should be drained here since the consumer
49- // may have stopped at this point. Failure to drain it will
50- // result in a deadlock: the channel will fill up and the poll
51- // goroutine will block in the callback.
52- go func () {
53- // revive:disable:empty-block
54- for range pb .eventsChan {
55- }
44+ if pb .stop == nil {
45+ return
46+ }
5647
57- if pb .lostChan != nil {
58- for range pb .lostChan {
59- }
60- }
61- // revive:enable:empty-block
62- }()
48+ // Signal the poll goroutine to exit
49+ close (pb .stop )
6350
64- // Wait for the poll goroutine to exit
65- pb .wg .Wait ()
51+ // The event and lost channels should be drained here since the consumer
52+ // may have stopped at this point. Failure to drain it will
53+ // result in a deadlock: the channel will fill up and the poll
54+ // goroutine will block in the callback.
55+ go func () {
56+ // revive:disable:empty-block
57+ for range pb .eventsChan {
58+ }
6659
67- // Close the channel -- this is useful for the consumer but
68- // also to terminate the drain goroutine above.
69- close (pb .eventsChan )
7060 if pb .lostChan != nil {
71- close (pb .lostChan )
61+ for range pb .lostChan {
62+ }
7263 }
64+ // revive:enable:empty-block
65+ }()
66+
67+ // Wait for the poll goroutine to exit
68+ pb .wg .Wait ()
7369
74- // This allows Stop() to be called multiple times safely
75- pb .stop = nil
70+ // Close the channel -- this is useful for the consumer but
71+ // also to terminate the drain goroutine above.
72+ close (pb .eventsChan )
73+ if pb .lostChan != nil {
74+ close (pb .lostChan )
7675 }
76+
77+ // Reset pb.stop to allow multiple safe calls to Stop()
78+ pb .stop = nil
7779}
7880
7981func (pb * PerfBuffer ) Close () {
8082 if pb .closed {
8183 return
8284 }
85+
8386 pb .Stop ()
8487 C .perf_buffer__free (pb .pb )
8588 eventChannels .remove (pb .slot )
@@ -101,6 +104,7 @@ func (pb *PerfBuffer) poll(timeout int) error {
101104 if errno == syscall .EINTR {
102105 continue
103106 }
107+
104108 return fmt .Errorf ("error polling perf buffer: %w" , errno )
105109 }
106110 }
0 commit comments