Skip to content
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

Throwing within util.inspect.custom can cause an ERR_INTERNAL_ASSERTION #49177

Open
paperdave opened this issue Aug 15, 2023 · 4 comments
Open
Labels
util Issues and PRs related to the built-in util module.

Comments

@paperdave
Copy link

Version

20.5.1

Platform

Darwin paperback.local 22.6.0 Darwin Kernel Version 22.6.0: Wed Jul 5 22:21:53 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6020 arm64

Subsystem

No response

What steps will reproduce the bug?

import util from "util";
console.log({
  hello: {
    [util.inspect.custom]: () => {
      throw new Error("hello");
    },
  },
});

How often does it reproduce? Is there a required condition?

Every Time

What is the expected behavior? Why is that the expected behavior?

To throw the error without an internal assertion error.

What do you see instead?

node:internal/errors:496
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_INTERNAL_ASSERTION]: Error: hello
    at [nodejs.util.inspect.custom] (file:///private/tmp/scratchpad_20230815T031811/index.mjs:5:13)
    at formatValue (node:internal/util/inspect:804:19)
    at formatProperty (node:internal/util/inspect:1944:11)
    at formatRaw (node:internal/util/inspect:1053:9)
    at formatValue (node:internal/util/inspect:839:10)
    at inspect (node:internal/util/inspect:363:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2297:40)
    at formatWithOptions (node:internal/util/inspect:2159:10)
    at console.value (node:internal/console/constructor:342:14)
    at console.log (node:internal/console/constructor:379:61)
This is caused by either a bug in Node.js or incorrect usage of Node.js internals.
Please open an issue with this stack trace at https://github.com/nodejs/node/issues

    at new NodeError (node:internal/errors:405:5)
    at Function.fail (node:internal/assert:20:9)
    at handleMaxCallStackSize (node:internal/util/inspect:1555:10)
    at formatRaw (node:internal/util/inspect:1061:12)
    at formatValue (node:internal/util/inspect:839:10)
    at inspect (node:internal/util/inspect:363:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2297:40)
    at formatWithOptions (node:internal/util/inspect:2159:10)
    at console.value (node:internal/console/constructor:342:14)
    at console.log (node:internal/console/constructor:379:61) {
  code: 'ERR_INTERNAL_ASSERTION'
}

Node.js v20.5.1

Additional information

No response

@atlowChemi atlowChemi added the util Issues and PRs related to the built-in util module. label Aug 17, 2023
@Manish4044
Copy link

Hey @atlowChemi @paperdave , as someone new to this project, will this issue be a good point to start?

@atlowChemi
Copy link
Member

@Manish4044 this is an issue which the solution is not likely to face any objection, so it would probably be OK. 🙂
That said, this is a code issue which will require getting into the internals, and if you are looking for a smooth starting point - docs fixes/improvements might be easier.

@BridgeAR
Copy link
Member

BridgeAR commented Sep 13, 2023

The issue is in the handleMaxCallStackSize method. There are actually multiple reasons that could cause an error to be thrown in that spot and they should cause the original error from being thrown instead of creating the internal one.

function handleMaxCallStackSize(ctx, err, constructorName, indentationLvl) {
if (isStackOverflowError(err)) {
ctx.seen.pop();
ctx.indentationLvl = indentationLvl;
return ctx.stylize(
`[${constructorName}: Inspection interrupted ` +
'prematurely. Maximum call stack size exceeded.]',
'special',
);
}
/* c8 ignore next */
assert.fail(err.stack);
}

It is therefore very straight forward to fix.

image

@Manish4044
Copy link

Thanks @atlowChemi @BridgeAR I will try to to use these info and fix the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
util Issues and PRs related to the built-in util module.
Projects
None yet
Development

No branches or pull requests

4 participants