Skip to content

src: prevent hard coding stack trace limit - #30752

Closed
legendecas wants to merge 1 commit into
nodejs:masterfrom
legendecas:cpp-error-stack-limit
Closed

src: prevent hard coding stack trace limit#30752
legendecas wants to merge 1 commit into
nodejs:masterfrom
legendecas:cpp-error-stack-limit

Conversation

@legendecas

@legendecaslegendecas commented Dec 1, 2019

Copy link
Copy Markdown
Member

Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

@nodejs-github-botnodejs-github-bot added the c++ Issues and PRs that require attention from people who are familiar with C++. label Dec 1, 2019
@legendecaslegendecas mentioned this pull request Dec 1, 2019
4 tasks
@devsnek

Copy link
Copy Markdown
Member

the number of frames here should already be the <= stack trace limit. as I understand it our code just provides a lower bound.

@joyeecheung

joyeecheung commented Dec 1, 2019

Copy link
Copy Markdown
Member

TBH this does not sound like a good idea since v8 is considering removing these non-standard APIs: https://bugs.chromium.org/p/v8/issues/detail?id=6974 By adding support in our internal error printers we are sort of encouraging people to use it - also in the case of --trace-sync-io, it is more of an implementation detail that we use the error-related API to print things.

It is especially tricky to try handling these hooks ourselves considering we also made the mistake (?) of always respecting the Error.prepareStackTrace from the main context.

Comment threadsrc/node_errors.cc Outdated
Comment threadsrc/node_errors.cc Outdated
Comment threadsrc/node_errors.cc Outdated
Comment threadsrc/node_errors.cc Outdated
Comment threadsrc/node_errors.cc Outdated
Comment threadsrc/node_errors.cc Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is not a valid .ToLocalChecked(), can you handle errors here (and for the .ToChecked() below, as @lundibundi pointed out)? Ideally, this block here would also be wrapped in a v8::TryCatch in order to swallow these errors from trying to get the stack trace

@legendecas

Copy link
Copy Markdown
MemberAuthor

@joyeecheung Yes, the API Error.stackTraceLimit itself is not standard. The idea of PR is preventing hard-coding numbers in the codebase. Either the limit shall be referred from the global Error.stackTraceLimit or just stored in the node::Environment, it would sound good to me. Since we've already used Error.stackTraceLimit multiple places, I'd think it might be fine at the time. We could remove the usage on Error.stackTraceLimit later if it's time to deprecate the API.

Anyway, if anyone has any strong opinion on this idea, it also sounds good to me if the stacktrace limit shall be referred from some internal store.

@joyeecheung

Copy link
Copy Markdown
Member

@legendecas Adding something like Environment::kStackTraceLimit SGTM. I think most internal uses of Error.stackTraceLimit are only optimizations to avoid the overhead of generating a stack trace (with the pattern of setting it to 0 or 1, creating an error, and then restoring the limit), which I don't know if is actually effective at this point considering stack trace generation in v8 is lazy.

@legendecas
legendecasforce-pushed the cpp-error-stack-limit branch 5 times, most recently from 9bf92aa to e6f54bbCompareDecember 7, 2019 07:23

@addaleaxaddaleax left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess LGTM although I’d have preferred the solution that reads Error.stackTraceLimit

@legendecas

Copy link
Copy Markdown
MemberAuthor

@joyeecheung@lundibundi may I ask for your reviews on the PR since you have interests in preventing usage on the unstandardized Error.stackTraceLimit?

Comment threadsrc/env.h Outdated

@lundibundilundibundi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Though I would prefer the previous Error.stackTraceLimit version as it is useful to be able to control the C++-land stack size and to have it consistent with the JS-land. Regarding possible removal of the API, I think it is not much of an issue as this code will only be in once place and we can always replace it with a constant in a few days' time at max.

Also, the PR's name should be changed accordingly to the code.

Comment threadsrc/node_errors.cc Outdated
@legendecas

Copy link
Copy Markdown
MemberAuthor

this code will only be in once place

There are some to be landed recently or in an expected future. #29207 and #30516. I'll update the code accordingly.

@legendecas

Copy link
Copy Markdown
MemberAuthor

@devsnek the number of frames here should already be the <= stack trace limit. as I understand it our code just provides a lower bound.

It's true that in most cases stack trace wasn't very long to exceed the limit. Still, it is possible.

@lundibundi

Copy link
Copy Markdown
Member

this code will only be in once place

There are some to be landed recently or in an expected future. #29207 and #30516. I'll update the code accordingly.

I meant to have one function like get_stack_size or compute_stack_size and then use it everywhere. Then we can easily just make this function return 10; if needed.

@legendecaslegendecas changed the title src: respect Error.stackTraceLimit in c++ landsrc: prevent hard coding stack trace limitDec 19, 2019
@legendecas
legendecasforce-pushed the cpp-error-stack-limit branch 2 times, most recently from 543418f to 4f1f62aCompareDecember 19, 2019 15:23
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@BridgeAR

Copy link
Copy Markdown
Member

I agree with @addaleax and @lundibundi that the former solution seemed better. If the property will ever be removed, it's definitely possible to react to it appropriately. So far it's not likely that this is happening anytime soon.

@joyeecheung

joyeecheung commented Dec 24, 2019

Copy link
Copy Markdown
Member

I would not oppose to a solution that allows the JS land to configure the stack trace limit, but I would be -1 if this is Error.stackTraceLimit for the following reasons (other than it's non-standard):

  1. It's an implementation detail that we use the error stack trace API to print stack traces that are not necessarily related to errors (e.g. --trace-sync-io) (actually, I don't think we currently even use the error stack trace API at all in these cases, so we'd be adding dependencies instead)
  2. It's unsafe to access Error.stackTraceLimit (which can be an accessor) in some of the paths where stack traces are printed from C++ (e.g. in Environment::Exit)

@BridgeARBridgeAR added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Dec 25, 2019
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

BridgeAR pushed a commit that referenced this pull request Dec 25, 2019
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@BridgeAR

Copy link
Copy Markdown
Member

Landed in 8baee5e 🎉

BridgeAR pushed a commit that referenced this pull request Jan 3, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@legendecas
legendecas deleted the cpp-error-stack-limit branch January 5, 2020 05:37
@BridgeARBridgeAR mentioned this pull request Jan 7, 2020
targos pushed a commit that referenced this pull request Jan 14, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
targos pushed a commit that referenced this pull request Jan 14, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@targostargos mentioned this pull request Jan 15, 2020
BethGriggs pushed a commit that referenced this pull request Feb 6, 2020
Refer to Environment::stack_trace_limit() while printing fresh
stacktraces in c++ land.
PR-URL: #30752
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Denys Otrishko <shishugi@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
@MylesBorinsMylesBorins mentioned this pull request Feb 8, 2020
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.c++Issues and PRs that require attention from people who are familiar with C++.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@legendecas@devsnek@joyeecheung@lundibundi@nodejs-github-bot@BridgeAR@addaleax