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

Jest test suite leaking memory #7

Closed
Langstra opened this issue Sep 7, 2020 · 4 comments
Closed

Jest test suite leaking memory #7

Langstra opened this issue Sep 7, 2020 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@Langstra
Copy link

Langstra commented Sep 7, 2020

Describe the bug
While running tests the test suite is leaking memory. We found out when at some point in time our tests crashed due to being out of memory.

I believe this has to do with mikro-orm and not with jest or nestjs, because I created a new nestjs project. The tests then worked. After adding @mikro-orm/core and @mikro-orm/nestjs the tests came up with the error.

Stack trace

Test suite failed to run

    EXPERIMENTAL FEATURE!
    Your test suite is leaking memory. Please ensure all references are cleaned.

    There is a number of things that can leak memory:
      - Async operations that have not finished (e.g. fs.readFile).
      - Timers not properly mocked (e.g. setInterval, setTimeout).
      - Keeping references to the global scope.

      at onResult (../../../node_modules/@jest/core/build/TestScheduler.js:190:18)
      at ../../../node_modules/@jest/core/build/TestScheduler.js:304:17
      at ../../../node_modules/emittery/index.js:260:13
          at Array.map (<anonymous>)
      at Emittery.Typed.emit (../../../node_modules/emittery/index.js:258:23)

<--- Last few GCs --->

[82559:0x59204c0]   161826 ms: Mark-sweep 2039.8 (2052.5) -> 2039.1 (2052.3) MB, 726.0 / 0.2 ms  (average mu = 0.079, current mu = 0.006) allocation failure scavenge might not succeed
[82559:0x59204c0]   162561 ms: Mark-sweep 2040.1 (2052.3) -> 2039.7 (2053.0) MB, 731.2 / 0.2 ms  (average mu = 0.043, current mu = 0.005) allocation failure scavenge might not succeed


<--- JS stacktrace --->

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0xa2b020 node::Abort() [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 2: 0x97a467 node::FatalError(char const*, char const*) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 3: 0xb9e0ee v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 4: 0xb9e467 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 5: 0xd3e875  [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 6: 0xd3f21b v8::internal::Heap::RecomputeLimits(v8::internal::GarbageCollector) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 7: 0xd4d012 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 8: 0xd4de65 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
 9: 0xd5082c v8::internal::Heap::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
10: 0xd1fecb v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationType, v8::internal::AllocationOrigin) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
11: 0x10501ef v8::internal::Runtime_AllocateInYoungGeneration(int, unsigned long*, v8::internal::Isolate*) [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
12: 0x13a9ed9  [/home/langstra/.nvm/versions/node/v14.4.0/bin/node]
Aborted (core dumped)
error Command failed with exit code 134.

To Reproduce
Steps to reproduce the behavior:

  1. git clone https://github.com/Langstra/nest-mikro-orm-jest-leak
  2. cd nest-mikro-orm-jest-leak
  3. yarn
  4. yarn test:e2e

Expected behavior
No memory leaks

Versions

Dependency Version
node 14.4.0
typescript 3.7.4
mikro-orm/core 4.0.0-rc.8
mikro-orm/mysql 4.0.0-rc.8
mikro-orm/nestjs 4.0.0.alpha-4
@Langstra Langstra added the bug Something isn't working label Sep 7, 2020
@B4nan
Copy link
Member

B4nan commented Sep 7, 2020

Yes, it is leaking, because for each test you are bootstrapping the application, but you never tear it down. Add this to the test file and the leak is gone:

  afterEach(async () => {
    await app.close();
  });

@B4nan B4nan closed this as completed Sep 7, 2020
@Langstra
Copy link
Author

Langstra commented Sep 7, 2020

Oh yea, forgot to add that in the reproduction repository. However, even after adding it the memory leak remains.

@Langstra
Copy link
Author

Langstra commented Sep 7, 2020

I see that is responds to the onApplicationShutdown hook, but maybe it should be used in beforeApplicationShutdown? Since onApplicationShutdown will run after app.close resolves?

Also maybe enableShutdownHooks need to be enabled as they are by default disabled?

@B4nan
Copy link
Member

B4nan commented Sep 7, 2020

Oh I see, it was more of a random "pass". Anyway, the "detect leaks" feature is experimental, for me it never worked correctly. See here for example:

jestjs/jest#5234

I see that is responds to the onApplicationShutdown hook, but maybe it should be used in beforeApplicationShutdown? Since onApplicationShutdown will run after app.close resolves?

Also maybe enableShutdownHooks need to be enabled as they are by default disabled?

This is based on the @nestjs/typeorm module, so I'd say that should be correct when it's done by the author of nest.

https://github.com/nestjs/typeorm/blob/master/lib/typeorm-core.module.ts#L102

Removing the onApplicationShutdown bit and explicitly calling orm.close() does not help. But I really believe this is more of a jest failure than actual leak. When I tried to duplicate the test 8 times and allowed garbage collection and heap logging, I can see the memory usage is consistent:

➜  nest-mikro-orm-jest-leak git:(master) ✗ node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage --config ./test/jest-e2e.json
 PASS  test/app1.e2e-spec.ts (62 MB heap size)
 PASS  test/app4.e2e-spec.ts (75 MB heap size)
 PASS  test/app5.e2e-spec.ts (76 MB heap size)
 PASS  test/app3.e2e-spec.ts (76 MB heap size)
 PASS  test/app7.e2e-spec.ts (76 MB heap size)
 PASS  test/app6.e2e-spec.ts (76 MB heap size)
 PASS  test/app2.e2e-spec.ts (76 MB heap size)
 PASS  test/app.e2e-spec.ts (76 MB heap size)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants