Skip to content

Added Reset for correct time in log#220

Closed
fabiodefilipposoftware wants to merge 2 commits into
SpecterOps:2.Xfrom
fabiodefilipposoftware:patch-2
Closed

Added Reset for correct time in log#220
fabiodefilipposoftware wants to merge 2 commits into
SpecterOps:2.Xfrom
fabiodefilipposoftware:patch-2

Conversation

@fabiodefilipposoftware
Copy link
Copy Markdown

@fabiodefilipposoftware fabiodefilipposoftware commented May 16, 2026

Added watch.Reset(); for corrected time in log

Description

Motivation and Context

This PR addresses: [GitHub issue or Jira ticket number]

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Chore (a change that does not modify the application functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

Summary by CodeRabbit

  • Bug Fixes
    • Fixed incorrect timing accumulation during search result processing so per-item processing times are measured and reset correctly.
    • This ensures more accurate performance metrics and prevents elapsed time from carrying over between items, improving logging accuracy and monitoring.

Review Change Stack

Added watch.Reset(); for corrected time in log
@github-actions
Copy link
Copy Markdown


Thank you for your submission, we really appreciate it. Like many open-source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution. You can sign the CLA by just posting a Pull Request Comment same as the below format.


I have read the CLA Document and I hereby sign the CLA


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 07995bbb-9377-4af7-aae7-fe4ea261df44

📥 Commits

Reviewing files that changed from the base of the PR and between 80573d4 and de1db4d.

📒 Files selected for processing (1)
  • src/Runtime/CollectionTask.cs
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Runtime/CollectionTask.cs

Walkthrough

The PR resets the Stopwatch after each per-item timing measurement in ConsumeSearchResults to prevent elapsed time accumulation between loop iterations; a minor closing-brace formatting change is also included.

Changes

Stopwatch Reset in Search Result Processing

Layer / File(s) Summary
Stopwatch reset after item processing
src/Runtime/CollectionTask.cs
watch.Reset() is called after stopping and logging the per-item Stopwatch so each loop iteration's elapsed time measurement starts fresh. Closing-brace formatting adjusted.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🐰 I watched the ticks unwind,
each loop now neat and kind,
a reset hop and away,
fresh timing every day,
small tweak, steady mind.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description is largely incomplete. While the title and brief heading reference the change, critical sections like motivation/context, testing details, and checklist items are unfilled. Complete the Motivation and Context section with issue details, fill in How Has This Been Tested with specific test steps, and check appropriate boxes in the Types of Changes and Checklist sections.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: adding watch.Reset() for correct time logging in the CollectionTask.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/Runtime/CollectionTask.cs (1)

131-144: ⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Incomplete stopwatch reset coverage leads to timing accumulation in error paths.

The watch.Reset() on line 144 is only reached when an item is successfully processed and written. Two code paths skip the reset:

  1. Null processed objects (line 136-137): When processed == null, the continue skips the reset, leaving the stopwatch stopped but not cleared.
  2. Exceptions (line 146-149): Any exception after Start() bypasses the reset, potentially leaving the watch running or stopped with accumulated time.

Since Stopwatch.Start() resumes from the previous elapsed time rather than resetting to zero, the next iteration will log cumulative time instead of per-item time.

Example failure:

  • Item A processes in 100ms but returns null → no reset, stopped at 100ms
  • Item B calls Start() (resumes from 100ms), processes in 50ms, Stop() at 150ms
  • Log incorrectly shows "took 150ms" instead of 50ms for Item B
⏱️ Recommended fix: reset immediately after logging

Move the reset to line 135 (right after logging) to ensure it executes in all code paths:

 log.LogTrace("Consumer {ThreadID} took {time} ms to process {obj}", threadId,
     watch.Elapsed.TotalMilliseconds, res.DisplayName);
+watch.Reset();
 if (processed == null)
     continue;
 
 if (processed is Domain d && _context.CollectedDomainSids.Contains(d.ObjectIdentifier))
 {
     d.Properties.Add("collected", true);
 }
 await _outputChannel.Writer.WriteAsync(processed);
-watch.Reset();

Alternative (even cleaner): Replace Start() + Reset() with Restart() at line 131:

 log.LogTrace("Consumer {ThreadID} started processing {obj} ({type})", threadId, res.DisplayName, res.ObjectType);
-watch.Start();
+watch.Restart();
 var processed = await processor.ProcessObject(item, res);
 watch.Stop();
 log.LogTrace("Consumer {ThreadID} took {time} ms to process {obj}", threadId,
     watch.Elapsed.TotalMilliseconds, res.DisplayName);
 if (processed == null)
     continue;
 
 if (processed is Domain d && _context.CollectedDomainSids.Contains(d.ObjectIdentifier))
 {
     d.Properties.Add("collected", true);
 }
 await _outputChannel.Writer.WriteAsync(processed);
-watch.Reset();

Using Restart() (which resets and starts in one call) eliminates the manual reset entirely and handles all edge cases.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/Runtime/CollectionTask.cs` around lines 131 - 144, The stopwatch handling
in CollectionTask (watch started before calling processor.ProcessObject and
reset only after writing) can accumulate elapsed time when processed == null or
exceptions occur; update the timing so each iteration measures only that item by
replacing the Start()+Reset() pattern with watch.Restart() before calling
processor.ProcessObject (or, alternatively, move watch.Reset() immediately after
the log.LogTrace call) so the stopwatch is always reset regardless of the
processed null path or exceptions; ensure references touched include watch,
processor.ProcessObject(item, res), log.LogTrace(...,
watch.Elapsed.TotalMilliseconds, ...), processed and
_outputChannel.Writer.WriteAsync(processed).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@src/Runtime/CollectionTask.cs`:
- Around line 131-144: The stopwatch handling in CollectionTask (watch started
before calling processor.ProcessObject and reset only after writing) can
accumulate elapsed time when processed == null or exceptions occur; update the
timing so each iteration measures only that item by replacing the
Start()+Reset() pattern with watch.Restart() before calling
processor.ProcessObject (or, alternatively, move watch.Reset() immediately after
the log.LogTrace call) so the stopwatch is always reset regardless of the
processed null path or exceptions; ensure references touched include watch,
processor.ProcessObject(item, res), log.LogTrace(...,
watch.Elapsed.TotalMilliseconds, ...), processed and
_outputChannel.Writer.WriteAsync(processed).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 385f0ec0-2e81-4a9e-ab1e-af3ce4e779d3

📥 Commits

Reviewing files that changed from the base of the PR and between f989500 and 80573d4.

📒 Files selected for processing (1)
  • src/Runtime/CollectionTask.cs

Reset the watch timer only after processing an item.
Copy link
Copy Markdown
Author

@fabiodefilipposoftware fabiodefilipposoftware left a comment

Choose a reason for hiding this comment

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

Corrected position of watch.Reset();

@github-actions github-actions Bot locked and limited conversation to collaborators May 16, 2026
@fabiodefilipposoftware fabiodefilipposoftware deleted the patch-2 branch May 16, 2026 23:58
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant