Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix closed chan read 2 #3438

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions pkg/ebpf/bpf_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,10 @@ func (t *Tracee) processBPFLogs(ctx context.Context) {
}

case lost := <-t.lostBPFLogChannel:
// When terminating tracee-ebpf the lost channel receives multiple "0 lost events" events.
// This check prevents those 0 lost events messages to be written to stderr until the bug is fixed:
// https://github.com/aquasecurity/libbpfgo/issues/122
if lost > 0 {
if err := t.stats.LostBPFLogsCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost BPF logs count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d ebpf logs events", lost))
if err := t.stats.LostBPFLogsCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost BPF logs count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d ebpf logs events", lost))

case <-ctx.Done():
return
Expand Down
11 changes: 3 additions & 8 deletions pkg/ebpf/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,10 @@ func (t *Tracee) processFileCaptures(ctx context.Context) {
}

case lost := <-t.lostCapturesChannel:
// When terminating tracee-ebpf the lost channel receives multiple "0 lost events" events.
// This check prevents those 0 lost events messages to be written to stderr until the bug is fixed:
// https://github.com/aquasecurity/libbpfgo/issues/122
if lost > 0 {
if err := t.stats.LostWrCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost capture count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d capture events", lost))
if err := t.stats.LostWrCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost capture count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d capture events", lost))

case <-ctx.Done():
return
Expand Down
23 changes: 13 additions & 10 deletions pkg/ebpf/events_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ func (t *Tracee) processLostEvents() {
logger.Debugw("Starting processLostEvents goroutine")
defer logger.Debugw("Stopped processLostEvents goroutine")

// Since this is an end-stage goroutine, it should be terminated when:
// - lostEvChannel is closed, or finally when;
// - internal done channel is closed (not ctx).
for {
select {
case lost := <-t.lostEvChannel:
// When terminating tracee-ebpf the lost channel receives multiple "0 lost events" events.
// This check prevents those 0 lost events messages to be written to stderr until the bug is fixed:
// https://github.com/aquasecurity/libbpfgo/issues/122
if lost > 0 {
if err := t.stats.LostEvCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost event count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d events", lost))
case lost, ok := <-t.lostEvChannel:
if !ok {
return // lostEvChannel is closed, lost is zero value
}

if err := t.stats.LostEvCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost event count", "error", err)
}
// Since this is an end-state goroutine, it should be terminated only when Tracee done channel is closed.
logger.Warnw(fmt.Sprintf("Lost %d events", lost))

// internal done channel is closed when Tracee is stopped via Tracee.Close()
case <-t.done:
return
}
Expand Down
9 changes: 3 additions & 6 deletions pkg/ebpf/net_capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ func (t *Tracee) processNetCapEvents(ctx context.Context, in <-chan *trace.Event
_ = t.stats.NetCapCount.Increment()

case lost := <-t.lostNetCapChannel:
if lost > 0 {
// https://github.com/aquasecurity/libbpfgo/issues/122
if err := t.stats.LostNtCapCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost network events count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d network capture events", lost))
if err := t.stats.LostNtCapCount.Increment(lost); err != nil {
logger.Errorw("Incrementing lost network events count", "error", err)
}
logger.Warnw(fmt.Sprintf("Lost %d network capture events", lost))

case <-ctx.Done():
return
Expand Down
10 changes: 5 additions & 5 deletions pkg/ebpf/tracee.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,16 +1366,16 @@ func (t *Tracee) Run(ctx gocontext.Context) error {
t.ready(ctx) // executes ready callback, non blocking
<-ctx.Done() // block until ctx is cancelled elsewhere

// Stop perf buffers
// Close perf buffers

t.eventsPerfMap.Stop()
t.eventsPerfMap.Close()
if t.config.BlobPerfBufferSize > 0 {
t.fileWrPerfMap.Stop()
t.fileWrPerfMap.Close()
}
if pcaps.PcapsEnabled(t.config.Capture.Net) {
t.netCapPerfMap.Stop()
t.netCapPerfMap.Close()
}
t.bpfLogsPerfMap.Stop()
t.bpfLogsPerfMap.Close()

// TODO: move logic below somewhere else (related to file writes)

Expand Down