question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Maximum call stack size exceeded when using circular dependencies inside a scoped container

See original GitHub issue

When trying to get services inside a scoped container that reference each other, we get Maximum call stack size exceeded error.

Code required to reproduce:

import "reflect-metadata";
import Container, { Service, Inject } from "typedi";

@Service()
class A {
  @Inject(() => B)
  b: any;
}

@Service()
class B {
  @Inject(() => A)
  a: any;
}

// Works
console.log({
  a: Container.get(A),
  b: Container.get(B),
});

const instance = Container.of("some-instance");
// Throws Maximum call stack size exceeded error
console.log({
  a: instance.get(A),
  b: instance.get(B),
});

Output when running with ts-node:

PS D:\typedi-test> ts-node --project .\tsconfig.json index.ts
{ a: A { b: B { a: [Circular] } }, b: B { a: A { b: [Circular] } } }

D:\typedi-test\src\ContainerInstance.ts:245
        return this.services.find(service => {
                             ^
RangeError: Maximum call stack size exceeded
    at D:\typedi-test\src\ContainerInstance.ts:245:35
    at Array.find (<anonymous>)
    at ContainerInstance.findService (D:\typedi-test\src\ContainerInstance.ts:245:30)
    at ContainerInstance.get (D:\typedi-test\src\ContainerInstance.ts:102:39)
    at Object.value (D:\typedi-test\src\decorators\Inject.ts:48:42)
    at D:\typedi-test\src\ContainerInstance.ts:381:54
    at Array.forEach (<anonymous>)
    at ContainerInstance.applyPropertyHandlers (D:\typedi-test\src\ContainerInstance.ts:376:28)
    at ContainerInstance.getServiceValue (D:\typedi-test\src\ContainerInstance.ts:343:18)
    at ContainerInstance.get (D:\typedi-test\src\ContainerInstance.ts:114:32)

No idea why source paths are wrong there …

Error when running compiled code:

PS D:\typedi-test> node .\dist\index.js
{ a: A { b: B { a: [Circular] } }, b: B { a: A { b: [Circular] } } }
D:\typedi-test\node_modules\reflect-metadata\Reflect.js:562
        function GetOrCreateMetadataMap(O, P, Create) {
                                       ^

RangeError: Maximum call stack size exceeded
    at GetOrCreateMetadataMap (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:562:40)
    at OrdinaryHasOwnMetadata (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:593:31)
    at OrdinaryGetMetadata (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:601:26)
    at OrdinaryGetMetadata (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:606:24)
    at OrdinaryGetMetadata (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:606:24)
    at Object.getMetadata (D:\typedi-test\node_modules\reflect-metadata\Reflect.js:357:20)
    at ContainerInstance.getServiceValue (D:\typedi-test\node_modules\typedi\ContainerInstance.js:181:75)
    at ContainerInstance.get (D:\typedi-test\node_modules\typedi\ContainerInstance.js:47:30)
    at Object.value (D:\typedi-test\node_modules\typedi\decorators\Inject.js:30:42)
    at D:\typedi-test\node_modules\typedi\ContainerInstance.js:246:54

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

0reactions
github-actions[bot]commented, Feb 10, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Maximum call stack size exceeded" in Angular 9 ng build
I had circular dependencies caused by my IDE automatically adding references to modules and components before I changed some things around. Use ......
Read more >
Changelog - Cypress Documentation
Fixed a regression in 6.5.0 that could cause Cypress to crash with a RangeError: Maximum call stack size exceeded at _deconstructPacket error.
Read more >
Circular Dependencies — Autofac 6.0.0 documentation
Circular dependencies are mutual runtime dependencies between components. ... If either type is set to factory scope you won't get the results you're ......
Read more >
Modules with circular dependency with Angular 4
I am trying to do something similar and it fails at compilation with AOT : RangeError: Maximum call stack size exceeded. When I...
Read more >
Google Python Style Guide
pylint is a tool for finding bugs and style problems in Python source code. It finds problems that are ... Avoid staticmethod and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found