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

Example for harmony in readme is very dangerous #165

Closed
SamSaffron opened this issue May 16, 2019 · 4 comments
Closed

Example for harmony in readme is very dangerous #165

SamSaffron opened this issue May 16, 2019 · 4 comments

Comments

@SamSaffron
Copy link

The line:

config.assets.js_compressor = Uglifier.new(harmony: true)

Can lead to severe memory leaks if you are using MiniRacer via exec js. V8 is not fork safe, initializing Uglifier will spawn a context.

At Discourse we saw a giant memory leak that lead to both of these commits:

rubyjs/mini_racer@f407f68

discourse/discourse@76173de

Burning all the contexts is not ideal, so I guess the best solution would be to amend Uglifier so it "defer" initializes? That way the odds of having a context going in the master process will be very low.

@AndrewRayCode
Copy link

This might explain the issue I'm seeing. We're trying to compile assets for a Rails server with webpacker > uglifier. I believe we're using execjs to do so. This appears to be taking up so much memory that it crashes our CI system, even when giving it 32gb of memory:

   ActionView::Template::Error:
   
     <--- Last few GCs --->
   
     [367:0x2642080]    53048 ms: Mark-sweep 1419.2 (1478.7) -> 1419.1 (1447.7) MB, 1132.9 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1133 ms) last resort GC in old space requested
     [367:0x2642080]    53780 ms: Mark-sweep 1419.1 (1447.7) -> 1419.1 (1447.7) MB, 731.1 / 0.0 ms  last resort GC in old space requested
   
   
     <--- JS stacktrace --->
   
     ==== JS stack trace =========================================
   
     Security context: 0x25bb8425879 <JSObject>
         2: optimize [/tmp/execjs20190921-165-1yrzpkhjs:3798] [bytecode=0x3e88a7812099 offset=37](this=0x2bb5d4f0c2e1 <AST_Call map = 0x36f4f3cdbee1>,compressor=0x72f30c82c59 <Compressor map = 0x36f4f3cc62a9>)
         3: before [/tmp/execjs20190921-165-1yrzpkhjs:3798] [bytecode=0x223f4c6266b1 offset=64](this=0x72f30c82c59 <Compressor map = 0x36f4f3cc62a9>,node=0x2bb5d4f0c2e1 <AST_Call map = 0x36f4f3cdbee1...
   
     FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
      1: node::Abort() [nodejs]
      2: 0x8cbf4c [nodejs]
      3: v8::Utils::ReportOOMFailure(char const*, bool) [nodejs]

What's the workaround for this?

@AndrewRayCode
Copy link

This is all a special kind of nightmare.

Running an older version of uglifier (3.0.0) on our code was flat out crashing (we've seen this before but have no idea why, other than general crappiness of execjs)

rake aborted!
ExecJS::ProgramError: Unexpected token: operator (>) (line: 165515, col: 37, pos: 6800637)

Error
    at new JS_Parse_Error (/tmp/execjs20190920-13904-1230of4js:3572:11870)
    at js_error (/tmp/execjs20190920-13904-1230of4js:3572:12089)
    at croak (/tmp/execjs20190920-13904-1230of4js:3572:20898)
    at token_error (/tmp/execjs20190920-13904-1230of4js:3572:21035)
    at unexpected (/tmp/execjs20190920-13904-1230of4js:3572:21123)
    at expr_atom (/tmp/execjs20190920-13904-1230of4js:3572:29660)
    at maybe_unary (/tmp/execjs20190920-13904-1230of4js:3573:143)
    at expr_ops (/tmp/execjs20190920-13904-1230of4js:3573:901)
    at maybe_conditional (/tmp/execjs20190920-13904-1230of4js:3573:993)
    at maybe_assign (/tmp/execjs20190920-13904-1230of4js:3573:1436)
new JS_Parse_Error ((execjs):3572:11870)

So we bumped uglifier but then got this error:

Uglifier::Error: Unexpected token: keyword (const). To use ES6 syntax, harmony mode must be enabled with Uglifier.new(:harmony => true).

And enabling the harmony flag appears to consume all available memory and crash

@lautis lautis closed this as completed in 9de67eb Sep 25, 2019
@SamSaffron
Copy link
Author

thanks @lautis !! 🎊

@AndrewRayCode
Copy link

AndrewRayCode commented Oct 4, 2019

Looks like this may still be an issue?

bundle show uglifier
/home/circleci/project/vendor/cache/ruby/2.5.0/gems/uglifier-4.2.0

But trying to run any rspec test that builds a bundle:

  Failure/Error: = stylesheet_link_tag 'application'

     ActionView::Template::Error:

       <--- Last few GCs --->

       [397:0x4172080]    58497 ms: Mark-sweep 1419.8 (1480.2) -> 1419.8 (1449.2) MB, 1126.9 / 0.0 ms  (+ 0.0 ms in 0 steps since start of marking, biggest step 0.0 ms, walltime since start of marking 1127 ms) last resort GC in old space requested
       [397:0x4172080]    59603 ms: Mark-sweep 1419.8 (1449.2) -> 1419.8 (1449.2) MB, 1106.2 / 0.0 ms  last resort GC in old space requested


       <--- JS stacktrace --->

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

       Security context: 0x16200f125879 <JSObject>
           2: /* anonymous */ [0x3b87c8b822d1 <undefined>:1] [bytecode=0x22658bbc7789 offset=0](this=0x393e3eb0c2f1 <JSGlobal Object>)
           5: builtin exit frame: new Function(aka Function)(this=0x3b87c8b822d1 <undefined>,0x36791dfde7c1 <String[118]: switch(str){case "(":case "[":case "+":case "*":case "/":case "-":case ",":case ".":case "`":return true}return false;>,0x16200f1416e9 <String[3]: str>)

        ...

       FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
        1: node::Abort() [nodejs]
        2: 0x8cbf4c [nodejs]
        3: v8::Utils::ReportOOMFailure(char const*, bool) [nodejs]
        4: v8::internal::V8::FatalProcessOutOfMemory(char const*, bool) [nodejs]
        5: v8::internal::Factory::NewCode(v8::internal::CodeDesc const&, unsigned int, v8::internal::Handle<v8::internal::Object>, bool, int) [nodejs]
        6: v8::internal::CodeGenerator::MakeCodeEpilogue(v8::internal::TurboAssembler*, v8::internal::EhFrameWriter*, v8::internal::CompilationInfo*, v8::internal::Handle<v8::internal::Object>) [nodejs]
        7: v8::internal::compiler::CodeGenerator::FinalizeCode() [nodejs]
        8: v8::internal::compiler::PipelineImpl::FinalizeCode() [nodejs]
        9: v8::internal::compiler::PipelineCompilationJob::FinalizeJobImpl() [nodejs]
       10: v8::internal::Compiler::FinalizeCompilationJob(v8::internal::CompilationJob*) [nodejs]
       11: v8::internal::OptimizingCompileDispatcher::InstallOptimizedFunctions() [nodejs]
       12: v8::internal::StackGuard::HandleInterrupts() [nodejs]
       13: v8::internal::Runtime_StackGuard(int, v8::internal::Object**, v8::internal::Isolate*) [nodejs]
       14: 0x51233a042fd
     # (execjs):1
     # ./vendor/cache/ruby/2.5.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:219:in `exec_runtime'
     # ./vendor/cache/ruby/2.5.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:39:in `exec'
     # ./vendor/cache/ruby/2.5.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:21:in `eval'
     # ./vendor/cache/ruby/2.5.0/gems/execjs-2.7.0/lib/execjs/external_runtime.rb:46:in `call'
     # ./vendor/cache/ruby/2.5.0/gems/uglifier-4.2.0/lib/uglifier.rb:221:in `run_uglifyjs'
     # ./vendor/cache/ruby/2.5.0/gems/uglifier-4.2.0/lib/uglifier.rb:166:in `compile'
     # ./vendor/cache/ruby/2.5.0/gems/sprockets-3.7.1/lib/sprockets/compressing.rb:65:in `block in js_compressor='
     # ./vendor/cache/ruby/2.5.0/gems/sprockets-3.7.1/lib/sprockets/legacy_proc_processor.rb:31:in `call'

Entry point to above error is gems/uglifier-4.2.0/lib/uglifier.rb:221:in run_uglifyjs'`

Config:

config.assets.js_compressor = Uglifier.new(harmony: true, mangle: false)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants