From 2bafcbf7d5379076990dbd1e463135638ab678b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fan=20Can=20Bak=C4=B1r?= Date: Sun, 27 Oct 2024 00:28:06 +0300 Subject: [PATCH] Fix panic error due to file already closed in stats mode --- internal/runner/runner.go | 3 +++ pkg/scan/events/scan_noop.go | 3 +++ pkg/scan/events/stats_build.go | 22 +++++++++++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) 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() +}