Skip to content

Commit

Permalink
Fix panic error due to file already closed in stats mode (#5774)
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancanbakir authored Oct 28, 2024
1 parent b57d086 commit 97403c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/scan/events/scan_noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ func AddScanEvent(event ScanEvent) {

func InitWithConfig(config *ScanConfig, statsDirectory string) {
}

func Close() {
}
22 changes: 21 additions & 1 deletion pkg/scan/events/stats_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type ScanStatsWorker struct {
config *ScanConfig
m *sync.Mutex
directory string
file *os.File
enc *json.Encoder
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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()
}

0 comments on commit 97403c2

Please sign in to comment.