Skip to content

JavaScript heap out of memory #3740

Description

@erikerikson

This is a Bug Report

Description

Endless recursive file loading exceeds process memory limits.

When trying to remove the serverless.yml here:
https://github.com/Nordstrom/hello-retail/tree/master/web

With the command (STAGE is properly defined and references an existing stack):

../node_modules/.bin/sls remove -s $STAGE -v
  • What went wrong?
<--- Last few GCs --->

[22378:0x102804600]   192582 ms: Mark-sweep 1484.5 (1567.0) -> 1484.5 (1567.0) MB, 2065.8 / 0.0 ms  allocation failure GC in old space requested
[22378:0x102804600]   194638 ms: Mark-sweep 1484.5 (1567.0) -> 1484.4 (1536.0) MB, 2056.4 / 0.0 ms  last resort gc 
[22378:0x102804600]   196780 ms: Mark-sweep 1484.4 (1536.0) -> 1484.4 (1536.0) MB, 2141.0 / 0.0 ms  last resort gc 


<--- JS stacktrace --->

==== JS stack trace =========================================

    2: arguments adaptor frame: 1->2
Security context: 0x90122ec0d31 <JS Object>
    3: _settlePromiseFromHandler [<src-root>/hello-retail/node_modules/serverless/node_modules/bluebird/js/release/promise.js:~496] [pc=0x20e01cfbd1f3](this=0x2d4a7d8ea59 <a Promise with map 0x17140f73cc21>,handler=0x2d4a7d97671 <JS Function getValueFromSource.then.valueToPopulate (SharedFunctionInfo 0x2b325a77f011)>,receiver=0x90122e04311 <undefined>,val...

FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
 1: node::Abort() [~/.nvm/versions/node/v7.2.0/bin/node]
 2: node::FatalException(v8::Isolate*, v8::Local<v8::Value>, v8::Local<v8::Message>) [~/.nvm/versions/node/v7.2.0/bin/node]
 3: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [~/.nvm/versions/node/v7.2.0/bin/node]
 4: v8::internal::Factory::NewByteArray(int, v8::internal::PretenureFlag) [~/.nvm/versions/node/v7.2.0/bin/node]
 5: v8::internal::SourcePositionTableBuilder::ToSourcePositionTable(v8::internal::Isolate*, v8::internal::Handle<v8::internal::AbstractCode>) [~/.nvm/versions/node/v7.2.0/bin/node]
 6: v8::internal::FullCodeGenerator::MakeCode(v8::internal::CompilationInfo*) [~/.nvm/versions/node/v7.2.0/bin/node]
 7: v8::internal::(anonymous namespace)::GenerateUnoptimizedCode(v8::internal::CompilationInfo*) [~/.nvm/versions/node/v7.2.0/bin/node]
 8: v8::internal::(anonymous namespace)::GetUnoptimizedCode(v8::internal::CompilationInfo*) [~/.nvm/versions/node/v7.2.0/bin/node]
 9: v8::internal::Compiler::Compile(v8::internal::Handle<v8::internal::JSFunction>, v8::internal::Compiler::ClearExceptionFlag) [~/.nvm/versions/node/v7.2.0/bin/node]
10: v8::internal::Runtime_CompileLazy(int, v8::internal::Object**, v8::internal::Isolate*) [~/.nvm/versions/node/v7.2.0/bin/node]
11: 0x20e01ca063a7
12: 0x20e01ca0675a
13: 0x20e01ca07e55
14: 0x20e01cfbd1f3
15: 0x20e01cfe1eb4
Abort trap: 6
  • What did you expect should have happened?

Expected: removal of stack

Downgrading to [email protected] allowed this to proceed as expected.

  • What was the config you used?

https://github.com/Nordstrom/hello-retail/blob/master/web/serverless.yml

  • What stacktrace or error message from your provider did you see?

see above.

Additional Data

I suspect that this change may have introduced the new behavior but am not certain:
971a279#diff-b921cbcedd2151d24391fb39acb84adeR88

  • Serverless Framework Version you're using: 1.13.2, 1.14.0
  • Operating System: osx
  • Stack Trace: see above
  • Provider Error messages: n/a

Activity

  1. erikerikson commented on Jun 7, 2017

    @erikerikson
    ContributorAuthor

    This appears to be the result of changing the strategy of the variable resolution code so that it doesn't “replace in place” before recursively populating the resolved value (i.e. instead one should return the result and modify the original containing object before then trying to resolve the child).

    I haven't tried but the following seems like it would reproduce:

    ~/config.yml

    someVal: ${self:custom.config.anotherVal}
    anotherVal: foo
    

    ~/serverless.yml

    custom:
      config: ${file(./config.yml)}
    
    1. sls loads ~/serverless.yml
    2. sls loads config.yml
    3. sls hasn't updated the original serverless.yml-based object but begins populating what was loaded in 2. Namely ${self:custom.config.anotherVal}
    4. sls loads self and finds that the custom.config value is ${file(./config.yml)}: goto 2.

    entirely speculation.

  2. pmuens commented on Jun 7, 2017

    @pmuens
    Contributor

    Hey @erikerikson thanks for opening 👍

    Yes, I think you're right. We've just merged a fix for such issues recently (will be included in v1.15).

    Could you try it with the recent master or check again once we've published v1.15 later today?

    #3709 is the PR with the fix for this.

    Might also be related to #3627 and #3711.

  3. HyperBrain commented on Jun 7, 2017

    @HyperBrain
    Contributor

    In general, I think that a recursive approach for the variable substitution is not the best way (populateObject and populateProperty are calling themselves indirectly and each other). It would be better to implement that traversing with an iterative approach (breadth or depth first does not really matter).
    With such an approach it is also easier to keep a map of visited objects locally.

  4. erikerikson commented on Jun 7, 2017

    @erikerikson
    ContributorAuthor

    I tried this with the latest master and wasn't patient enough for it to blow up. Repetitive messages were written. Does not appear to be fixed in master.

  5. pmuens commented on Jun 8, 2017

    @pmuens
    Contributor

    Thanks for testing that out @erikerikson 👍

    I think @HyperBrain has a valid point about recursion here. Seems like that's the root issue. Maybe we should change it to the propose BFS or DFS.

    /cc @eahefnawy

  6. erikerikson commented on Jun 9, 2017

    @erikerikson
    ContributorAuthor

    Maybe. The issue is a result of the ability to do things like ${self:custom.mySetting.${opt:stage}}. Breadth vs depth first is irrelevant in such a "rewind" and "reevaluate" case. It's simply that in a world allowing self-reference, that the referent must be replaced in place of the original reference before the original is processed further.

  7. HyperBrain commented on Jun 10, 2017

    @HyperBrain
    Contributor

    @erikerikson Exactly. The inner references have to be resolved first as their values build up the outer references. So the dereferencing itself has to work with states that are stored on a stack in this case and the system would be rewinded to that state after the inner reference has been evaluated.

    Additionally the newly generated references can cause circular references depending on the value of the inner reference(s). The algorithm should be able to detect that, i.e. the dynamically generated reference has to be put into the map of visited objects.
    A point here is, that it is quite easy to detect these circular references and stop processing, but for the user it should show a kind of stack trace (i.e. the states that triggered a "rewind" or "reevaluate") to really show where the root of the circle is located.

  8. iwllyu commented on Jun 22, 2017

    @iwllyu

    We're seeing a similar issue, the cause is a cyclic reference. I tried this with 1.16.0 running sls deploy --acct dev --noDeploy

    serverless.yml

    provider:
      acct: ${opt:acct, self:provider.profile}
      vars: ${self:provider.accounts.${self:provider.acct}}    
      accounts:
        dev:
          publicStreamArn: arn:aws:kinesis:x:x:stream/${self:provider.vars.publicStreamName}
          privateStreamArn: arn:aws:kinesis:x:stream/${self:provider.vars.privateStreamName}
          privateStreamName: dev_stream
          publicStreamName: dev_stream
        prod:
          publicStreamArn: arn:aws:kinesis:x:x:stream/${self:provider.vars.publicStreamName}
          privateStreamArn: arn:aws:kinesis:x:x:stream/${self:provider.vars.privateStreamName}
          privateStreamName: private_stream
          publicStreamName: public_stream
    

    replacing ${self:provider.vars.publicStreamName} with ${self:provider.accounts.dev.publicStreamName} or
    dev_stream no longer produces the error

  9. modified the milestones: 1.17, 1.18 on Jun 23, 2017
  10. 18 remaining items

  11. removed this from the 1.23 milestone on Sep 21, 2017
  12. Eli-Goldberg commented on Oct 7, 2017

    @Eli-Goldberg

    Happens to me as well

  13. fruffin commented on Oct 17, 2017

    @fruffin
    Contributor

    Same problem here. That would be great to get this fix reviewed soon (I can confirm the fix works for my projects as well).
    Workaround for now was to downgrade serverless back to a working version

  14. Greenginer commented on Oct 25, 2017

    @Greenginer

    👍 for getting this fix out. This is starting to become a blocker to certain serverless deployments that are requiring difficult workarounds to utilize features that are available in later versions.

  15. added this to the 1.25 milestone on Dec 14, 2017
  16. dashmug commented on Jan 30, 2018

    @dashmug
    Contributor

    I'm getting this error randomly during sls deploy with my project.

    node v6.10.3
    serverless v1.26.0
    serverless-webpack v4.2.0
    

    I usually just restart the deploy and it's goes through the second time. This is probably caused by something else but I don't know where to start to investigate.

    On successful builds, the output is like this:

    ...
    Serverless: Copy modules: /codebuild/output/src172916450/src/build/audit-worker [4775 ms]
    Serverless: Prune: /codebuild/output/src172916450/src/build/audit-worker [1517 ms]
    Serverless: Zip function: /codebuild/output/src172916450/src/build/audit-worker [5635 ms]
    ...
    ... (this continues until the deploy succeeds)
    

    On failed builds, the output is like this:

    Serverless: Copy modules: /codebuild/output/src237433561/src/build/audit-worker [3144 ms]
    Serverless: Prune: /codebuild/output/src237433561/src/build/audit-worker [1611 ms]
    
    <--- Last few GCs --->
    
    126575 ms: Mark-sweep 1331.3 (1434.9) -> 1326.6 (1434.9) MB, 2159.0 / 0.0 ms [allocation failure] [GC in old space requested].
    128838 ms: Mark-sweep 1326.6 (1434.9) -> 1327.0 (1434.9) MB, 2262.4 / 0.0 ms [allocation failure] [GC in old space requested].
    130681 ms: Mark-sweep 1327.0 (1434.9) -> 1331.8 (1411.9) MB, 1842.6 / 0.0 ms [last resort gc].
    132121 ms: Mark-sweep 1331.8 (1411.9) -> 1336.5 (1411.9) MB, 1440.3 / 0.0 ms [last resort gc].
    
    
    <--- JS stacktrace --->
    
    ==== JS stack trace =========================================
    
    Security context: 0x1a8f0c2cfb39 <JS Object>
    1: onwrite(aka onwrite) [/codebuild/output/src237433561/src/node_modules/readable-stream/lib/_stream_writable.js:~440] [pc=0xbec57fd33dc] (this=0x1a8f0c204381 <undefined>,stream=0x1c436ce99581 <an Archiver with map 0x2cf623834091>,er=0x1a8f0c204201 <null>)
    2: /* anonymous */(aka /* anonymous */) [/codebuild/output/src237433561/src/node_modules/readable-stream/lib/_stream_writable.js:~16...
    
    FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
    Aborted
    
  17. HyperBrain commented on Jan 30, 2018

    @HyperBrain
    Contributor

    Hi @dashmug . Is the issue restricted to CodeBuild or does it happen in other environments too, and which loaders do you use in your project (e.g. TypeScript has some memory issues, where an issue already exists in the webpack repository). If the compile step scatters around in the Node heap, it might leave not enough memory afterwards.

  18. dashmug commented on Jan 30, 2018

    @dashmug
    Contributor

    @HyperBrain

    I only build my projects through CodeBuild and that's where it happens randomly in around 1 out of 10 builds. I'll try to do some sls deploys from my local to see if it happens locally.

    I do use Typescript with Webpack so this must be the reason. And this error hasn't happened for me in my non-Typescript Lambdas (though my non-typescript projects are relatively tiny compared to the typescript one).

    Could the occurrence of this error increase with the number of Lambdas I have? I started having this failure right after I added a 5th Lambda function (each Lambda is an entry point in Webpack).

    Are there known workarounds for this?

  19. HyperBrain commented on Jan 31, 2018

    @HyperBrain
    Contributor

    @dashmug You can check the comments in serverless-heaven/serverless-webpack#299. There has been a lengthy discussion of people facing the TypeScript memory problem and I remember there was some workaround that did the job.

  20. alonsoir commented on Jun 1, 2018

    @alonsoir

    Hi, i am getting this exception with the getting-started example:

    https://serverless.com/getting-started/

    [email protected] /usr/local/lib/node_modules/npm
    MacBook-Pro-Retina-de-Alonso:~ aironman$ npm version
    { npm: '5.0.3',
    ares: '1.10.1-DEV',
    cldr: '30.0.3',
    http_parser: '2.7.0',
    icu: '58.2',
    modules: '57',
    node: '8.1.4',
    openssl: '1.0.2l',
    tz: '2016j',
    unicode: '9.0',
    uv: '1.12.0',
    v8: '5.8.283.41',
    zlib: '1.2.11' }

    https://pastebin.com/yb6ZTxjH

  21. odykyi commented on Feb 10, 2020

    @odykyi

    An update: it works when I set transpileOnly: true for ts-loader.

  22. Berger92 commented on Jul 27, 2020

    @Berger92

    @odykyi where did you set it? Could you show the whole thing please?

  23. talaikis commented on Jul 21, 2021

    @talaikis

    Mhm..., 32 Gb RAM, out of memory after removing serverlesss-plugin-include-packages.

  24. pgrzesik commented on Jul 21, 2021

    @pgrzesik
    Contributor

    Hello @talaikis 👋 Could you share more details about your case? What version of the Framework are you on? Any chance for a reproducible example that consumes a lot of memory? Thanks in advance 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions