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

Maximum call stack size exceeded #728

Closed
olback opened this issue Jun 11, 2020 · 16 comments
Closed

Maximum call stack size exceeded #728

olback opened this issue Jun 11, 2020 · 16 comments

Comments

@olback
Copy link

olback commented Jun 11, 2020

Bug

Version (complete output of terser -V or specific git commit)

terser 4.7.0

Complete CLI command or minify() options used

terser 1.js

terser input
From olback/es6-css-minify#106:
https://github.com/olback/es6-css-minify/files/4762748/1.js.zip

terser output or error

ERROR: Maximum call stack size exceeded
    at generator (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:1720:24)
    at doit (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:835:13)
    at AST_Call.print (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:842:13)
    at generator (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:1765:14)
    at doit (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:835:13)
    at AST_Dot.print (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:842:13)
    at generator (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:1721:25)
    at doit (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:835:13)
    at AST_Call.print (/home/olback/.npm-global/lib/node_modules/terser/lib/output.js:842:13)
    at /home/olback/.npm-global/lib/node_modules/terser/lib/output.js:1765:14

Expected result
Minified output

Related issues: #490, #543

@ni3stack
Copy link

ni3stack commented Jun 12, 2020

@olback
Let me know if you have the same issue so I'll keep my comment else will open a new issue.

Operating System: macOS
Node Version: 10.17.0
NPM Version: 6.11.3
webpack Version: 4.43.0
terser-webpack-plugin Version: 2.3.7.


FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x10096d6b6 node::Abort() (.cold.1) [/usr/local/Cellar/node@10/10.17.0/bin/node]
<--- Last few GCs --->
io[2911:0x103b0d000]    16098 ms: Mark-sweep 1398.1 (1415.2) -> 1397.9 (1416.7) MB, 640.9 / 0.0 ms  (+ 0.0 ms in 29 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 650 ms) (average mu = 0.155, current mu = 0.015) allocatio[2911:0x103b0d000]    16846 ms: Mark-sweep 1399.6 (1416.7) -> 1399.4 (1417.7) MB, 738.9 / 0.0 ms  (+ 0.0 ms in 27 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 747 ms) (average mu = 0.086, current mu = 0.012) allocatio

<--- JS stacktrace --->

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

    0: ExitFrame [pc: 0xae43685be3d]
    1: StubFrame [pc: 0xae43685d1ff]
Security context: 0x087aff81e6e9 <JSObject>
    2: /* anonymous */(aka /* anonymous */) [0x87a0d802a21] [Users/repo/node_modules/terser/dist/bundle.min.js:~1] 
[pc=0xae436d61038](this=0x087a881826f1 <undefined>)
    3: arguments adaptor frame: 1->0
    4: /* anonymous */(aka /* anonymous */) [0x87a0d802aa1] [/Users/nitinkp...

 2: 0x10003b8d0 node_module_register [/usr/local/Cellar/node@10/10.17.0/bin/node]
 3: 0x10003ba91 node::OnFatalError(char const*, char const*) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 4: 0x100178d87 v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 5: 0x100178d29 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 6: 0x10042d29c v8::internal::Heap::UpdateSurvivalStatistics(int) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 7: 0x10042eed9 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 8: 0x10042c576 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [/usr/local/Cellar/node@10/10.17.0/bin/node]
 9: 0x10042b387 v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [/usr/local/Cellar/node@10/10.17.0/bin/node]
10: 0x1004339ae v8::internal::Heap::AllocateRawWithLigthRetry(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/usr/local/Cellar/node@10/10.17.0/bin/node]
11: 0x1004339f8 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [/usr/local/Cellar/node@10/10.17.0/bin/node]
12: 0x1004142e7 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [/usr/local/Cellar/node@10/10.17.0/bin/node]
13: 0x1005ee60d v8::internal::Runtime_AllocateInNewSpace(int, v8::internal::Object**, v8::internal::Isolate*) [/usr/local/Cellar/node@10/10.17.0/bin/node]
14: 0xae43685be3d 
15: 0xae43685d1ff 

ERROR in js/app.js from Terser
Error: Call retries were exceeded
at ChildProcessWorker.initialize (/repo/node_modules/jest-worker/build/workers/ChildProcessWorker.js:191:21)
at ChildProcessWorker._onExit (/repo/node_modules/jest-worker/build/workers/ChildProcessWorker.js:268:12)
at ChildProcess.emit (events.js:198:13)
at ChildProcess.EventEmitter.emit (domain.js:448:20)
at Process.ChildProcess._handle.onexit (internal/child_process.js:248:12)

I didn't encounter this error until I run npm clean cache / npm install when the terser-webpack-plugin updated to 2.3.7 from 2.3.6.
I did some search in the stack overflow and also checked a few closed issues but none of them seems to be working.
Even updated & downgraded the package to the latest release i.e 3.0 & 2.1 but the result.

My Webpack config

 optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        chunkFilter: chunk => {
          // Exclude uglification for the `vendor` chunk
          if (chunk.name === 'vendor') {
            return false;
          }

          return true;
        },
        cache: true,
      }),
    ],
  },

I also tried to give the parallel property a number value: 2 / os.cpus().length;() -1.
but this also didn't work.
FYI - CPU's length is 4. No CircleCi configuration.
Even tried setting nodemon --max_old_space_size=4096 src/server/index.js.

let me know if I need to add/update something to overcome this problem.

@fabiosantoscode
Copy link
Collaborator

@nitinkumar8553 yours is a separate issue!

@olback thanks for bringing this to my attention. I was able to narrow it down to a single, very long line which crashes babel's parser as well (I found that out while trying to prettify it). I'm going to narrow this down further and understand why.

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Jun 13, 2020

Ok, I narrowed it down to:

something

Plus 1600 times .chain()

The reason this happens is because Terser's output system is based on (non-tail) recursion. It has to recurse every time it sees a .chain() and at some point it will be a stack overflow.

I don't know how reasonable it is to expect Terser to fix this. I mean, there have to be reasonable limits to everything, and usually, the limits with Terser are whether something might be written by a human. In this case, I can't see someone sitting down and chaining method calls 1600 times.

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Jun 13, 2020

To expand on @nitinkumar8553's issue a little bit:

You might've set the --max_old_space_size option on your toplevel node command, but as long as you have parallelism enabled, TerserPlugin will create a new node process to minify your code. When it does, you can't control what node flags are given to that new child process.

@ni3stack
Copy link

@fabiosantoscode as I have already mentioned in my comment above.
I did set --max_old_space_siz to 4096 & also tried enabling parallelism by setting true and even by doing os.cpus.length -1 to the property but couldn't get away with that error.

@tchetwin
Copy link
Contributor

@nitinkumar8553 just to check, the flag is --max-old-space-size 4096 and @fabiosantoscode is indicating that you should not run in parallel in conjunction with this as the arguments won't pass to the child processes.

@ezca2
Copy link

ezca2 commented Jun 22, 2023

could anybody find a solution to this issue?
i'm getting the error in docker enviroment, when it is on build stage

@fabiosantoscode
Copy link
Collaborator

@ezca2 you may have the same issue as #1402. Could you post any stack trace you may have there?

@mbshaban
Copy link

@ezca2 if you can build your app outside of Docker but not with Docker, then please ensure that you are using the same node version in both environments. Additionally, remember to copy both the package-lock.json and package.json files into the Docker environment.

@ankitaivd
Copy link

ankitaivd commented Jun 26, 2023

after changing --max-old-space-size 4096 and also change terser version but the build is failed.could anybody find a solution to this issue?

@ezca2
Copy link

ezca2 commented Jun 26, 2023

@ezca2 you may have the same issue as #1402. Could you post any stack trace you may have there?

Yes, i have read that post too at first time. Unfortunanly nobody could find a solution yet.
My stack trace is exactly the same that published post's author.
Thanks

@ankitaivd
Copy link

ankitaivd commented Jun 26, 2023 via email

@fabiosantoscode
Copy link
Collaborator

The stack trace from #1402 comes from a different tool than Terser. I don't have any information on what might have caused it (Terser version, input code, Terser options, does the issue go away without reduce_vars enabled, does the issue go away on another version), and couldn't obtain it so I closed it. I can guess a lot but I'm traveling right now and can't spend the time.

I would appreciate if someone could open another issue with those details.

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Jun 27, 2023

I've published a version 5.18.2 that swaps a recursive function with a loop. The function wouldn't have been infinitely recursive, but seeing a huge number of function definitions might cause too much recursion.

I don't know if this is the fix that needed to be pushed, but without any information, it's all I can do.

@ankitaivd
Copy link

ankitaivd commented Jul 11, 2023 via email

@fabiosantoscode
Copy link
Collaborator

fabiosantoscode commented Dec 7, 2023

Even though some other issues have crept into this discussion, this issue is related to code that has really deeply nested structures (expressions, statements, etc) and makes Terser crash as it uses recursion to transform it.

As mentioned in #1477, this isn't really fixable for fundamental reasons. The workaround is to tell node.js to use a larger stack size.

node --stack-size=2000 node_modules/.bin/terser {args}

Or, if you're using a script that uses Terser to minify:

node --stack-size=2000 ./my-minify-script.js

edit: I closed this issue to prevent mixing it up with other recursion issues. If Terser is crashing with a recursion error and you think your code isn't extremely deeply nested, please open a new issue.

@terser terser locked and limited conversation to collaborators Dec 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants