Skip to content

Commit

Permalink
🚨 recover from panic's inside goroutines (#1525)
Browse files Browse the repository at this point in the history
The main cnspec goroutine has already a `recover()` statement, though Go
doesn't share that between goroutines and since the calculation of the
policies happen on another thread, we were not reporting upstream.

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Dec 27, 2024
1 parent 1381943 commit 14033d6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions policy/executor/internal/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import (

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/upstream/health"
"go.mondoo.com/cnquery/v11/utils/iox"
"go.mondoo.com/cnspec/v11"
"go.mondoo.com/cnspec/v11/policy"
"google.golang.org/protobuf/proto"
)
Expand Down Expand Up @@ -122,6 +124,7 @@ func (c *BufferedCollector) run() {
c.wg.Add(1)
go func() {
defer c.wg.Done()
defer health.ReportPanic("cnspec", cnspec.Version, cnspec.Build)

done := false
results := []*llx.RawResult{}
Expand Down
3 changes: 3 additions & 0 deletions policy/executor/internal/execution_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (

"github.com/rs/zerolog/log"
"go.mondoo.com/cnquery/v11/llx"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/upstream/health"
"go.mondoo.com/cnspec/v11"
)

const MEM_DEBUG_ENV = "MEM_DEBUG"
Expand Down Expand Up @@ -63,6 +65,7 @@ func (em *executionManager) Start() {
em.wg.Add(1)
go func() {
defer em.wg.Done()
defer health.ReportPanic("cnspec", cnspec.Version, cnspec.Build)
for {
// Prioritize stopChan
select {
Expand Down
5 changes: 5 additions & 0 deletions policy/scan/local_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"go.mondoo.com/cnquery/v11/providers-sdk/v1/recording"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/upstream"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/upstream/gql"
"go.mondoo.com/cnquery/v11/providers-sdk/v1/upstream/health"
"go.mondoo.com/cnquery/v11/utils/multierr"
"go.mondoo.com/cnquery/v11/utils/slicesx"
"go.mondoo.com/cnspec/v11"
Expand Down Expand Up @@ -314,6 +315,8 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
scanGroups.Add(1)
go func() {
defer scanGroups.Done()
defer health.ReportPanic("cnspec", cnspec.Version, cnspec.Build)

if err := multiprogress.Open(); err != nil {
log.Error().Err(err).Msg("failed to open progress bar")
}
Expand Down Expand Up @@ -415,6 +418,8 @@ func (s *LocalScanner) distributeJob(job *Job, ctx context.Context, upstream *up
wg.Add(1)
go func() {
defer wg.Done()
defer health.ReportPanic("cnspec", cnspec.Version, cnspec.Build)

for i := range batch {
asset := batch[i].Asset
runtime := batch[i].Runtime
Expand Down

0 comments on commit 14033d6

Please sign in to comment.