emotion-js / emotion

👩‍🎤 CSS-in-JS library designed for high performance style composition
https://emotion.sh/
MIT License
17.51k stars 1.11k forks source link

Potential memory leak #3221

Open amoore108 opened 3 months ago

amoore108 commented 3 months ago

Current behavior: We're seeing a potential memory leak from the latest version of @emotion, possibly the @emotion/jest library specifically: https://github.com/bbc/simorgh/actions/runs/10039435131/job/27743362024?pr=11779

This behaviour can be reproduced locally by running this Dependabot update branch: https://github.com/bbc/simorgh/pull/11779

I tried downgrading @emotion/jest back to 11.11.0 and it appears to work and not crash out (although causes snapshot issues, probably due to the mismatch between versions of expected packages)

Previously 11.12.0 of the other Emotion packages, apart from @emotion/jest, also worked: https://github.com/bbc/simorgh/pull/11776

<--- Last few GCs --->

[2287:0x6fed8b0]   212859 ms: Mark-sweep (reduce) 2986.5 (3669.6) -> 2986.2 (3626.8) MB, 1090.5 / 0.0 ms  (average mu = 0.018, current mu = 0.001) last resort; GC in old space requested
[2287:0x6fed8b0]   214007 ms: Mark-sweep (reduce) 2986.2 (3626.8) -> 2986.1 (3615.6) MB, 1147.6 / 0.0 ms  (average mu = 0.009, current mu = 0.001) last resort; GC in old space requested

<--- JS stacktrace --->

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: 0xb9c1f0 node::Abort() [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 2: 0xaa27ee  [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 3: 0xd73950 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 4: 0xd73cf7 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 5: 0xf3f18f v8::internal::HeapAllocator::AllocateRawWithRetryOrFailSlowPath(int, v8::internal::AllocationType, v8::internal::AllocationOrigin, v8::internal::AllocationAlignment) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 6: 0xf1f158 v8::internal::Factory::CodeBuilder::AllocateCode(bool) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 7: 0xf330cc v8::internal::Factory::CodeBuilder::BuildInternal(bool) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 8: 0xf33b1e v8::internal::Factory::CodeBuilder::Build() [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
 9: 0x15ed38e v8::internal::RegExpMacroAssemblerX64::GetCode(v8::internal::Handle<v8::internal::String>) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
10: 0x12b1e86 v8::internal::RegExpCompiler::Assemble(v8::internal::Isolate*, v8::internal::RegExpMacroAssembler*, v8::internal::RegExpNode*, int, v8::internal::Handle<v8::internal::String>) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
11: 0x12d1db7 v8::internal::RegExpImpl::Compile(v8::internal::Isolate*, v8::internal::Zone*, v8::internal::RegExpCompileData*, v8::base::Flags<v8::internal::RegExpFlag, int>, v8::internal::Handle<v8::internal::String>, v8::internal::Handle<v8::internal::String>, bool, unsigned int&) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
12: 0x12d2532 v8::internal::RegExpImpl::CompileIrregexp(v8::internal::Isolate*, v8::internal::Handle<v8::internal::JSRegExp>, v8::internal::Handle<v8::internal::String>, bool) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
13: 0x12d30ee v8::internal::RegExpImpl::IrregexpPrepare(v8::internal::Isolate*, v8::internal::Handle<v8::internal::JSRegExp>, v8::internal::Handle<v8::internal::String>) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
14: 0x12d3293 v8::internal::RegExpImpl::IrregexpExec(v8::internal::Isolate*, v8::internal::Handle<v8::internal::JSRegExp>, v8::internal::Handle<v8::internal::String>, int, v8::internal::Handle<v8::internal::RegExpMatchInfo>, v8::internal::RegExp::ExecQuirks) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
15: 0x12f862e v8::internal::Runtime_RegExpExec(int, unsigned long*, v8::internal::Isolate*) [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]
16: 0x17120b9  [/opt/hostedtoolcache/node/18.20.4/x64/bin/node]

To reproduce:

  1. Pull down this branch locally: https://github.com/bbc/simorgh/pull/11779
  2. Run yarn to install dependencies
  3. Run yarn test:unit to run unit tests
  4. Tests run for a period of time, but then crash out with a JavaScript heap out of memory error

Expected behavior:

Environment information:

Andarist commented 3 months ago

I can see what could cause this but I somewhat fail to see why 🤔 This shouldn't increase the memory usage all that much: https://github.com/emotion-js/emotion/pull/3198/commits/89bebfbca7f75e441663072a3dcd9632a4ac3136

I can look into your issue given that you have provided the repro case... but it will take some days before I get to it.

amoore108 commented 3 months ago

Thanks @Andarist, no major rush on this. Appreciate your time looking into it 👍

Nordicdesign commented 5 days ago

Similar situation here, bumped @emotion/jest from 11.11.0 to 11.13.0 and now if I run my tests with --detectLeaks, they all fail as leaking memory. Going back to 11.11.0 solves it.

Unfortunately can't provide a small repo to reproduce.