nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

processes remaining open and leaking memory after killing main process

Open sysrage opened this issue 3 years ago • 8 comments

NWJS Version : 0.62.0 Operating System : Windows 10

Expected behavior

When the main nw.exe process is killed, all child processes should also be killed. No nw.exe processes should continue leaking memory until the system crashes.

Actual behavior

Intermittently, 2-3 nw.exe processes will remain running after the "main" process is terminated. When this happens, those processes will continue increasing memory usage until the system runs out of memory and begins killing active processes.

How to reproduce

  1. Put the following files in a directory.
  2. Run: node test.js
  3. Press CTRL+C in the terminal where step 2 was execute.
  4. Note that the NW.js application will "quit" but sometimes phantom nw.exe processes will remain running.
  5. Watch those phantom nw.exe processes slowly chew through all available system memory.

package.json

{
  "name": "bug",
  "main": "bug.html"
}

bug.html

Hello

test.js

const { spawn } = require('child_process');

// Launch NW.js application
const proc = spawn('nw', ['.']);
console.log(`PID of child_process.exec() process: ${proc.pid}`);

// Kill app on CTRL+C
process.on('SIGINT', () => {
  process.kill(proc.pid);
  process.exit();
})

// Keep alive until NW.js application exits
let quit = false;
const keepAliveCallback = () => {
  if (!quit) setTimeout(keepAliveCallback, 500);
};
keepAliveCallback();
proc.stdout.on('data', (data) => {
  process.stdout.write(data);
});
proc.stderr.on('data', (data) => {
  process.stderr.write(data);
});
proc.on('exit', () => {
  quit = true;
});

sysrage avatar Mar 10 '22 03:03 sysrage

happening to me as well kind of frequently, i also spawn processes & use stdin, stderr, etc .. but also happens without those things involved - currently investigating if related to media streams open, event handlers between main & renderer or objects shared between renderers ... i am clueless really but would be glad to help to find the issue. Also wondering if a workaround would be possible, so after the application exits a script kills the helper processes if they stay alive.

nebular avatar Mar 18 '22 12:03 nebular

We has same issue, crash or running some process after "close" app.

@rogerwang

panther7 avatar Apr 13 '22 13:04 panther7

long discutions and theory over there https://github.com/nwjs/nw.js/issues/7633

jonlepage avatar Apr 26 '22 15:04 jonlepage

long discutions and theory over there #7633

no solution, though. @rogerwang can you let us know what's needed to get this behavior fixed?

sysrage avatar Apr 26 '22 18:04 sysrage

no solution, though. @rogerwang can you let us know what's needed to get this behavior fixed?

the only solution i use is taskkill /F /IM nw.exe /T 🤣 Is dirty and not pro but no one have hint where this come from !....

jonlepage avatar Apr 26 '22 19:04 jonlepage

the only solution i use is taskkill /F /IM nw.exe /T 🤣 Is dirty and not pro but no one have hint where this come from !....

Unfortunately, that is not a solution. It's an ugly workaround that doesn't work in many cases. That also kills valid nw.exe processes (from other apps), so can't be done "automatically".

sysrage avatar Apr 26 '22 19:04 sysrage

my application creates a socket server on the main process which renderers then connect to - funny thing is, if there has been a previous dirty exit with hung renderers, if you reopen the application, the new instance´s renderers connect to the older instance socket server, in the main thread that should not exist - so it also stays alive in my case and apparently responsive - however I cannot connect a debugger to it or its former renderers - the debugger does not connect.

nebular avatar Apr 30 '22 15:04 nebular