Search code examples
node.jsjenkins-pipelineelectron-builderangular10

"API fatal error handler returned after process out of memory on the background thread" when building an angular, electron app


We are using angular 10.2, electron 4.2, electron-builder 22.9 with node 10.16.0. Also we have a jenkins pipeline to be executed every time a Pull request is made. This pipeline creates an AWS windows instance (t2.large) in order to download the code and build an .exe file. The problem is when compiling the code, this error is pop up: Fatal error # API fatal error handler returned after process out of memory on the background thread.

The node variable --max_old_space_size value is 4191.868721008301 MB. If the AWS windows instance is launched manually, run npm install and building the code manually, no error is triggered and the build is completely successfully. The error is only happening when jenkins launch the AWS windows instance.

On a mac environment with --max_old_space_size=1.5GB the build process is completely successfully. As well when tested manually in windows with --max_old_space_size=1.5GB the build is done fine. If the variable is set to --max_old_space_size=512MB the build fails with a different error Javascript heap out of memory.

My guess is something related with jenkins thread memory ? but I haven't found how to increase the thread memory.

Any idea, tips would be helpful.

Jenkins file:

stage ('Build Windows app') {
    node('windows-electron') {
        // Wipe the workspace so we are building completely clean
        deleteDir()

        checkout scm

        bat """
        cd app
        call npm run node-memory
        call node -v
        call npm --no-git-tag-version version \"${version}\"
        call npm install
        REM call npm run test - tests disabled on Windows for now (they run on Mac OS)
        call npm run build"""
    }
}

Package.json build script: "build": "npm run download-translations && ng build --prod && electron-webpack && electron-builder --publish=never",


Solution

  • Due this problem starting in angular 9 and 10 as well we create a linux slave to do the build process, then stash the files and unstash in the mac and windows slaves.

    node('nvm-slave') {
            // Wipe the workspace so we are building completely clean
            deleteDir()
    
            checkout scm
    
            nvm(nvmInstallURL: 'https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh', nvmIoJsOrgMirror: 'https://iojs.org/dist', nvmNodeJsOrgMirror: 'https://nodejs.org/dist', version: '10.16.0') {
                sh '''cd app
                npm install
                npx ng build --prod
                '''
            }
    
            stash name: "folder-to-stash", includes: "path/to/folder/to/stash/**"
    }
    
    stage ('Build Windows app') {
        node('windows-electron') {
            // Wipe the workspace so we are building completely clean
            deleteDir()
    
            checkout scm
    
            unstash "folder-to-stash"
    
            bat """
            cd app
            call node -v
            call npm --no-git-tag-version version \"${version}\"
            call npm install
            REM call npm run test - tests disabled on Windows for now (they run on Mac OS)
            call npm run postbuild"""
    
            archiveArtifacts 'app/dist/*.exe'
        }
    }
    

    Package.json script:

    "build": "ng build --prod",
    "postbuild": "electron-webpack && electron-builder --x64 --publish=never",