Skip to content

fix: always call timeEnd after timeLog in createDebugger when inspect is true#143

Open
vinamra1102 wants to merge 1 commit into
unjs:mainfrom
vinamra1102:fix/debugger-timer-leak-when-inspect-true
Open

fix: always call timeEnd after timeLog in createDebugger when inspect is true#143
vinamra1102 wants to merge 1 commit into
unjs:mainfrom
vinamra1102:fix/debugger-timer-leak-when-inspect-true

Conversation

@vinamra1102
Copy link
Copy Markdown

@vinamra1102 vinamra1102 commented May 22, 2026

What does this PR fix?

createDebugger in src/debugger.ts calls console.timeLog() in the
afterEach handler when inspect: true but never calls console.timeEnd().
This causes every hook call to leak a console timer. On the second call to
the same hook the browser warns:

Timer 'hookName' already exists

Since inspect defaults to true in browsers, every browser user of
createDebugger is affected.

Root cause

console.timeEnd() was inside the else branch of the inspect check,
so it was never reached when inspect: true. The timer started by
console.time() in beforeEach was never ended.

Fix

Moved console.timeEnd() outside the if/else block so it always runs
regardless of inspect value. console.timeLog() still only runs when
inspect: true.

Before:

if (options.inspect) {
  console.timeLog(logPrefix(event), event.args)
} else {
  console.timeEnd(logPrefix(event))
}

After:

if (options.inspect) {
  console.timeLog(logPrefix(event), event.args)
}
console.timeEnd(logPrefix(event))

Tests added

  • should end timer when inspect is true — asserts console.timeEnd
    is called with the hook label
  • should not leak timers when calling same hook twice with inspect
    asserts console.time and console.timeEnd are each called exactly
    twice with no leaked timers

Fixes #142

Developed with AI assistance (Claude Code).

Summary by CodeRabbit

  • Bug Fixes

    • Improved timer lifecycle management in the debugger. Timer cleanup now executes consistently regardless of inspection mode settings.
  • Tests

    • Added validation tests for timer behavior during debugging operations to ensure timers properly initialize and terminate.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 22, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0f15d6c9-b985-4982-b806-c38e163ba71b

📥 Commits

Reviewing files that changed from the base of the PR and between b77477c and b41b2c0.

📒 Files selected for processing (2)
  • src/debugger.ts
  • test/debuger.test.ts

📝 Walkthrough

Walkthrough

This PR fixes a timer leak in createDebugger when inspect mode is enabled. Previously, console.timeEnd() was only called when inspect was false, causing timers to remain active. The fix ensures timers are always ended after logging, and new tests validate proper cleanup on repeated hook calls.

Changes

Timer Cleanup Fix for Debug Inspect Mode

Layer / File(s) Summary
Timer cleanup logic
src/debugger.ts
The afterEach hook now calls console.timeEnd() unconditionally after console.timeLog(...) when inspect is true, instead of only ending the timer when inspect is false.
Test coverage for timer lifecycle
test/debuger.test.ts
Two new test cases verify that console.timeEnd is invoked with the correct hook label and that successive calls to the same hook do not leak timers.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

A timer once leaked without end,
But now our rabbit will mend!
console.timeEnd() runs free,
No more duplicate timers, you see! 🐰⏱️
Debug inspect mode, now pristine and clean.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 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 primary fix: ensuring timeEnd is called after timeLog in createDebugger when inspect is true, which directly addresses the timer leak issue.
Linked Issues check ✅ Passed The changes directly address issue #142's objectives: preventing timer leaks by ensuring console.timeEnd() always runs regardless of inspect option, with added tests validating the fix.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fix the timer leak in createDebugger and add necessary tests; no unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ 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.

@vinamra1102
Copy link
Copy Markdown
Author

CI workflow is awaiting approval ,could a maintainer please
approve when convenient? Happy to address any feedback. Thank you!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

createDebugger leaks console timer when inspect: true

1 participant