Fix panic error due to file already closed in stats mode (#5774)

This commit is contained in:
Dogan Can Bakir
2024-10-28 13:26:21 +03:00
committed by GitHub
parent b57d086afc
commit 97403c203e
3 changed files with 27 additions and 1 deletions

View File

@@ -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

View File

@@ -9,3 +9,6 @@ func AddScanEvent(event ScanEvent) {
func InitWithConfig(config *ScanConfig, statsDirectory string) {
}
func Close() {
}

View File

@@ -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()
}