Skip to content

fix: prevent infinite loop on logging - #370

Open
kricsleo wants to merge 2 commits into
unjs:mainfrom
kricsleo:fix/avoid-infinite-loop
Open

kricsleo wants to merge 2 commits into
unjs:mainfrom
kricsleo:fix/avoid-infinite-loop

Conversation

@kricsleo

Copy link
Copy Markdown
Member

resolves #298

@codecov

codecov Bot commented Mar 22, 2025 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 30.86%. Comparing base (24c98ce) to head (a8a2384).
⚠️ Report is 59 commits behind head on main.

Files with missing lines Patch % Lines
src/consola.ts 83.33% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main     #370       +/-   ##
===========================================
- Coverage   61.08%   30.86%   -30.23%     
===========================================
  Files          13       43       +30     
  Lines        1380     1740      +360     
  Branches       58      100       +42     
===========================================
- Hits          843      537      -306     
- Misses        537     1176      +639     
- Partials        0       27       +27     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pi0

pi0 commented Mar 24, 2025

Copy link
Copy Markdown
Member

Nice solution however as discussed in #298 it is an issue with Browser reporter without util.format support. Unless we can reproduce it in node, we might better apply this fix to browser reporter.

@kricsleo

kricsleo commented Mar 24, 2025 •

Copy link
Copy Markdown
Member Author

It can be reproduced in node, see #298 (comment)

And it's caused by this JSON.strinfify

consola/src/consola.ts

Lines 425 to 430 in 5ac9ed7

const serializedLog = JSON.stringify([
logObj.type,
logObj.tag,
logObj.args,
]);
const isSameLog = this._lastLog.serialized === serializedLog;

JSON.stringify invokes the getter, which logs the same obj again, resulting in a recursive call to JSON.stringify, and this process continues indefinitely.

@pi0

pi0 commented Mar 25, 2025

Copy link
Copy Markdown
Member

Wondering if we could make it safer (and faster) by using ohash for comparing log objects instead of full serialization.

@kricsleo

kricsleo commented Mar 25, 2025 •

Copy link
Copy Markdown
Member Author

No, I've tried (ohash or other methods), but they don't help.

Anythings that works like serialize (which triggers getter) will result in infinite loop.

It's not just the getter; Proxy can also trigger this behavior.

For example:

const p = new Proxy({}, {
  get(target, prop, receiver) {
    console.warn('proxy getter', p)
    return Reflect.get(target, prop, receiver)
  }
})

consola.wrapConsole();
consola.warn(p);
consola.restoreConsole();

This would also result in infinite loop. (And there could be more edge cases 🫠)

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.

infinite loop protection

2 participants