opentelemetry-js icon indicating copy to clipboard operation
opentelemetry-js copied to clipboard

@opentelemetry/context-zone Maximum call stack size exceeded occurs when the Redux Tool Chrome Extension is enabled for Next v13.5.4

Open WeirShi opened this issue 2 years ago • 13 comments
trafficstars

What happened?

image

OpenTelemetry Setup Code

import { WebTracerProvider } from '@opentelemetry/sdk-trace-web'
import { Resource } from '@opentelemetry/resources'
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'
import { ZoneContextManager } from '@opentelemetry/context-zone'
import { registerInstrumentations } from '@opentelemetry/instrumentation


const provider = new WebTracerProvider()
provider.register({
  contextManager: new ZoneContextManager(),
})

package.json

"dependencies": {
    "@opentelemetry/api": "^1.4.0",
    "@opentelemetry/context-zone": "^1.9.1",
    "@opentelemetry/core": "^1.13.0",
    "@opentelemetry/exporter-trace-otlp-http": "^0.35.1",
    "@opentelemetry/instrumentation": "^0.39.1",
    "@opentelemetry/instrumentation-fetch": "^0.39.1",
    "@opentelemetry/instrumentation-xml-http-request": "^0.39.1",
    "@opentelemetry/otlp-transformer": "^0.39.1",
    "@opentelemetry/resources": "^1.13.0",
    "@opentelemetry/sdk-trace-web": "^1.13.0",
    "@opentelemetry/semantic-conventions": "^1.13.0"
  }

Relevant log output

No response

WeirShi avatar Oct 18 '23 09:10 WeirShi

The provided screenshot is too low resolution to read and appears to be minified code. Can you please copy and paste a text version of the stacktrace?

Can you also try using the stack context manager and see if the bug still persists? This context manager is not recommended for production but should help us to eliminate variables for debugging.

dyladan avatar Oct 18 '23 16:10 dyladan

This is the stack error message

RangeError: Maximum call stack size exceeded at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:73933) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483) at normalizeRouterState (7864-c71cb59fef1aa50d.js:1:74483)

and May I ask if there is any other package to replace @opentelemetry/context-zone?
Thanks

WeirShi avatar Oct 19 '23 07:10 WeirShi

normalizeRouterState is not opentelemetry code. This might be caused by the extension. Can you please provide a reproduction so we can look into this further?

dyladan avatar Oct 25 '23 16:10 dyladan

What is making you think this is caused by the context manager? I don't see reference to it in the stacktrace or error message?

dyladan avatar Oct 25 '23 16:10 dyladan

Demo

This is a Next.js demo project that includes a secondary development of sdk.js based on OpenTelemetry and imports the "context" package. In Next.js, if you load this project using the "Script beforeInteractive" or "Script afterInteractive" modes during production builds and with Redux DevTools enabled, it will result in an error stating "Maximum call stack size exceeded.". However, if you load the project using the "lazyOnload" mode, this issue should not occur.

WeirShi avatar Oct 31 '23 15:10 WeirShi

with Redux DevTools enabled, it will result in an error stating "Maximum call stack size exceeded.". However, if you load the project using the "lazyOnload" mode, this issue should not occur.

This REALLY does sound like the DevTools is adding something to the base prototype (of some class) that has a recursive loop (back to a parent object) so that when the recursive code attempt to traverse the object tree it goes forever.

And based on the normalizeRouterState in the recursive call stack this seems to be what that function is doing.

So it seems like the DevTools is seeing something that the Context Manager is adding that triggeres this scenario -- exactly what! Well thats the million dollar question. Can you (@WeirShir) do some debugging and identify what that normalizeRouterState is repeating on as this will help identify "who" is the culprit. I suspect a fix will be required in the normalizeRouterState and/or the DevTools and not (specifically) in the Context Manager.

MSNev avatar Nov 01 '23 16:11 MSNev

This sounds like an issue I need to raise with Redux Tool officials.

WeirShi avatar Nov 03 '23 10:11 WeirShi

FWIW normalizeRouterState is part of Next's use of the Redux DevTools:

  • https://github.com/vercel/next.js/blob/ffdfd33251165d8b2f092568c8e4e5f08c71a636/packages/next/src/client/components/use-reducer-with-devtools.ts#L12

As far as I know, the Redux DevTools don't do anything to "mess with prototypes".

markerikson avatar Nov 03 '23 14:11 markerikson

Really interesting... in this stackblitz, https://stackblitz.com/edit/github-wywdtb?file=src%2Fapp%2Flayout.tsx

You can npm i, and then npm run build && npx serve out - and you'll see in the console the issues.

What seems to happen is that the object prototype is polluted, or somehow there's a self-referential object that just stacks up calls to the normalizeRouterState function.

I am more on the first option:

Screenshot 2023-11-29 at 16 32 45

Why would the zone object end up in the Next.js redux dev tools code 🤔 it has to be pollution right? Note that this reproduction repository uses Next.js' export mode as well.

icyJoseph avatar Nov 29 '23 15:11 icyJoseph

Once you get to this point of the debugging, past the __PAGE__ stuff from RSC, there's no way back, it'll just stack up calls to normalizeRouterState 🤔

Screenshot 2023-11-29 at 16 36 35 Screenshot 2023-11-29 at 16 38 39

icyJoseph avatar Nov 29 '23 15:11 icyJoseph

This sounds like an issue I need to raise with Redux Tool officials

@WeirShi did you ever raise this with redux tools?

dyladan avatar Apr 03 '24 16:04 dyladan

@dyladan per above this appears to be an issue with Next's code, not the Redux DevTools

markerikson avatar Apr 03 '24 16:04 markerikson

Sorry. I guess the more pertinent question is "can this be closed?" It doesn't seem to be caused by anything we're doing

dyladan avatar Apr 03 '24 16:04 dyladan

There hasn't been much interaction on this and it does so far seem to be something in either Next or Redux DevTools. Will close this out for now, but if something new comes up leaning toward otel-js needing a fix, feel free to comment or open a new issue.

JamieDanielson avatar Jul 24 '24 16:07 JamieDanielson