Skip to content

fix: prevent infinite loop on logging #370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kricsleo
Copy link
Member

resolves #298

Copy link

codecov bot commented Mar 22, 2025

Codecov Report

Attention: 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 57 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 Sentry.
📢 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
Copy link
Member

pi0 commented Mar 24, 2025

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
Copy link
Member Author

kricsleo commented Mar 24, 2025

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
Copy link
Member

pi0 commented Mar 25, 2025

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

@kricsleo
Copy link
Member Author

kricsleo commented Mar 25, 2025

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