diff --git a/internal/runner/runner.go b/internal/runner/runner.go index 516bd7ca50..e5e9469252 100644 --- a/internal/runner/runner.go +++ b/internal/runner/runner.go @@ -392,6 +392,9 @@ func (r *Runner) Close() { if r.tmpDir != "" { _ = os.RemoveAll(r.tmpDir) } + + //this is no-op unless nuclei is built with stats build tag + events.Close() } // setupPDCPUpload sets up the PDCP upload writer diff --git a/pkg/scan/events/scan_noop.go b/pkg/scan/events/scan_noop.go index a284657f52..055baed4ee 100644 --- a/pkg/scan/events/scan_noop.go +++ b/pkg/scan/events/scan_noop.go @@ -9,3 +9,6 @@ func AddScanEvent(event ScanEvent) { func InitWithConfig(config *ScanConfig, statsDirectory string) { } + +func Close() { +} diff --git a/pkg/scan/events/stats_build.go b/pkg/scan/events/stats_build.go index 6fe5f27174..77f471df57 100644 --- a/pkg/scan/events/stats_build.go +++ b/pkg/scan/events/stats_build.go @@ -23,6 +23,7 @@ type ScanStatsWorker struct { config *ScanConfig m *sync.Mutex directory string + file *os.File enc *json.Encoder } @@ -56,7 +57,7 @@ func (s *ScanStatsWorker) initEventsFile() error { if err != nil { return err } - defer f.Close() + s.file = f s.enc = json.NewEncoder(f) return nil } @@ -79,3 +80,22 @@ func AddScanEvent(event ScanEvent) { } defaultWorker.AddScanEvent(event) } + +// Close closes the file associated with the worker +func (s *ScanStatsWorker) Close() { + s.m.Lock() + defer s.m.Unlock() + + if s.file != nil { + _ = s.file.Close() + s.file = nil + } +} + +// Close closes the file associated with the worker +func Close() { + if defaultWorker == nil { + return + } + defaultWorker.Close() +}