Skip to content

Commit

Permalink
fix: rb.stopped should be set in the Stop method
Browse files Browse the repository at this point in the history
This cleans up the design of a solution to an issue where Stop()
 could be called after the sole consumer of the rb.stop channel
 has closed but rb.stopped has not yet been set. This removes
the need to have a non-blocking channel write with `select` as
it turns out we really don't need this channel at all.

Signed-off-by: grantseltzer <grantseltzer@gmail.com>
  • Loading branch information
grantseltzer committed Feb 27, 2021
1 parent 8d3c3d5 commit 6eb7608
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions libbpfgo/libbpfgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ type PerfBuffer struct {
type RingBuffer struct {
rb *C.struct_ring_buffer
bpfMap *BPFMap
stop chan bool
stopped bool
closed bool
}
Expand Down Expand Up @@ -643,24 +642,18 @@ func (m *Module) InitRingBuf(mapName string, eventsChan chan []byte) (*RingBuffe
ringBuf := &RingBuffer{
rb: rb,
bpfMap: bpfMap,
stop: make(chan bool),
}
m.ringBufs = append(m.ringBufs, ringBuf)
return ringBuf, nil
}

func (rb *RingBuffer) Start() {
go rb.poll()
rb.stopped = false
}

func (rb *RingBuffer) Stop() {
if rb.stopped {
return
}
select {
case rb.stop <- true:
default:
}
rb.stopped = true
}

func (rb *RingBuffer) Close() {
Expand All @@ -676,11 +669,6 @@ func (rb *RingBuffer) Close() {

func (rb *RingBuffer) poll() error {

go func() {
<-rb.stop
rb.stopped = true
}()

for {
err := C.ring_buffer__poll(rb.rb, 0)
if err < 0 {
Expand Down

0 comments on commit 6eb7608

Please sign in to comment.