Repository navigation
fix: ensure resume.cfg captures last completed item state - #2394
ohmygod20260203 wants to merge 2 commits into
Conversation
This fix addresses issue projectdiscovery#2345 where resume.cfg may be written before items are fully processed, causing targets to be skipped on resume. The problem: - currentIndex was incremented BEFORE processing started - When CTRL+C was pressed, items that were in-progress were counted as completed in the resume file - On resume, these in-progress items would be skipped The solution: - Track lastCompletedIndex and lastCompletedItem separately - Update these values AFTER an item has been submitted for processing - SaveResumeConfig now saves the last completed state instead of the current processing state /claim projectdiscovery#2345
WalkthroughAdded concurrency-safe resume state to ResumeCfg (mutex-protected lastCompletedIndex and lastCompletedItem with setter/getter). Runner updates and persists these fields when queueing items; SaveResumeConfig now guards nil and writes the guarded last-completed state. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@runner/runner.go`:
- Around line 1445-1450: The assignments to
r.options.resumeCfg.lastCompletedIndex and lastCompletedItem in processItem are
racy because SaveResumeConfig can read them concurrently; introduce
synchronization by adding a mutex (e.g., sync.Mutex or sync.RWMutex) to the
ResumeCfg type and protect all reads/writes via methods (e.g.,
ResumeCfg.SetLastCompleted(index int, item string) and
ResumeCfg.GetLastCompleted() ...) or by locking around accesses; update
processItem to call the setter (or lock/unlock the ResumeCfg mutex) and update
SaveResumeConfig (and any other readers) to use the corresponding getter/lock so
lastCompletedIndex and lastCompletedItem are always accessed under the same
mutex.
- Around line 2801-2807: SaveResumeConfig currently reads fields of
r.options.resumeCfg that are concurrently written by processItem and now
protected by a mutex on ResumeCfg; acquire the same mutex when reading those
fields (e.g., lock r.options.resumeCfg.mu or the ResumeCfg's exported lock)
around accessing lastCompletedIndex and lastCompletedItem inside
SaveResumeConfig, then unlock before calling goconfig.Save, and also guard
against r.options.resumeCfg being nil by checking it first and returning a safe
error if nil to avoid a nil-pointer dereference.
| // Update lastCompleted AFTER the item has been submitted for processing | ||
| // This ensures resume.cfg reflects items that have at least been queued | ||
| if r.options.resumeCfg != nil { | ||
| r.options.resumeCfg.lastCompletedIndex = r.options.resumeCfg.currentIndex | ||
| r.options.resumeCfg.lastCompletedItem = k | ||
| } |
There was a problem hiding this comment.
Data race: concurrent access to lastCompletedIndex and lastCompletedItem without synchronization.
These fields are written here in processItem while SaveResumeConfig can read them concurrently (e.g., triggered by CTRL+C signal handler). String assignment is not atomic in Go, leading to potential undefined behavior.
Consider protecting access with a mutex in ResumeCfg:
🔒 Proposed fix: Add mutex synchronization
In runner/resume.go:
+import "sync"
+
type ResumeCfg struct {
ResumeFrom string
Index int
current string
currentIndex int
// lastCompletedIndex tracks the index of the last item that was fully processed
// This ensures we don't skip items that were in-progress when interrupted
lastCompletedIndex int
lastCompletedItem string
+ mu sync.Mutex
}In runner/runner.go (lines 1447-1450):
if r.options.resumeCfg != nil {
+ r.options.resumeCfg.mu.Lock()
r.options.resumeCfg.lastCompletedIndex = r.options.resumeCfg.currentIndex
r.options.resumeCfg.lastCompletedItem = k
+ r.options.resumeCfg.mu.Unlock()
}🤖 Prompt for AI Agents
In `@runner/runner.go` around lines 1445 - 1450, The assignments to
r.options.resumeCfg.lastCompletedIndex and lastCompletedItem in processItem are
racy because SaveResumeConfig can read them concurrently; introduce
synchronization by adding a mutex (e.g., sync.Mutex or sync.RWMutex) to the
ResumeCfg type and protect all reads/writes via methods (e.g.,
ResumeCfg.SetLastCompleted(index int, item string) and
ResumeCfg.GetLastCompleted() ...) or by locking around accesses; update
processItem to call the setter (or lock/unlock the ResumeCfg mutex) and update
SaveResumeConfig (and any other readers) to use the corresponding getter/lock so
lastCompletedIndex and lastCompletedItem are always accessed under the same
mutex.
Addresses CodeRabbit review on PR projectdiscovery#2394: - Add mu sync.Mutex to ResumeCfg struct - Lock mutex when writing lastCompletedIndex/lastCompletedItem - Lock mutex when reading in SaveResumeConfig This fixes the data race condition when multiple goroutines complete concurrently and try to update the resume state.
Addresses CodeRabbit review on PR projectdiscovery#2394: - Add mu sync.Mutex to ResumeCfg struct - Lock mutex when writing lastCompletedIndex/lastCompletedItem - Lock mutex when reading in SaveResumeConfig This fixes the data race condition when multiple goroutines complete concurrently and try to update the resume state.
|
Hi, thanks for your interest in contributing! Just a heads up, we ask contributors to work on 1 active issue at a time (see). Also, we welcome AI-assisted development, but submissions must be complete, tested, and ready to merge. Please also make sure to fill out the PR template with proof that your changes work. We're closing this PR along with your other open submissions. Once you're ready, feel free to pick one issue to focus on and resubmit; we'd be happy to review it. Appreciate your understanding! |
Proposed Changes
This PR fixes issue #2345 where
resume.cfgmay be written before items are fully processed, causing targets to be skipped on resume.The Problem
The current implementation has a race condition:
currentIndexis incremented BEFORE processing startsThe Solution
lastCompletedIndexandlastCompletedItemseparately fromcurrentIndexandcurrentSaveResumeConfignow saves the last completed state instead of the current processing stateFiles Changed
runner/resume.go: AddedlastCompletedIndexandlastCompletedItemfieldsrunner/runner.go: UpdatedprocessItemto track completed state, updatedSaveResumeConfigto use last completed valuesProof
Before (issue behavior):
After (with fix):
The resume.cfg will only include items that have been successfully submitted for processing, ensuring that in-progress items at the time of interruption will be re-processed on resume.
Checklist
/claim #2345
Summary by CodeRabbit