1- import { ChildProcess , SpawnOptions } from 'child_process' ;
2- import { default as spawnChild } from 'cross-spawn' ;
3- import path from 'path' ;
1+ import { ChildProcess , fork , spawn , SpawnOptions } from 'child_process' ;
2+ import path , { resolve } from 'path' ;
43import MongoBinary from './MongoBinary' ;
54import { MongoBinaryOpts } from './MongoBinary' ;
65import debug from 'debug' ;
@@ -310,7 +309,7 @@ export class MongoInstance extends EventEmitter {
310309 * @fires MongoInstance#instanceLaunched
311310 */
312311 _launchMongod ( mongoBin : string ) : ChildProcess {
313- const childProcess = spawnChild ( mongoBin , this . prepareCommandArgs ( ) , {
312+ const childProcess = spawn ( resolve ( mongoBin ) , this . prepareCommandArgs ( ) , {
314313 ...this . spawnOpts ,
315314 stdio : 'pipe' , // ensure that stdio is always an pipe, regardless of user input
316315 } ) ;
@@ -337,29 +336,30 @@ export class MongoInstance extends EventEmitter {
337336 _launchKiller ( parentPid : number , childPid : number ) : ChildProcess {
338337 this . debug ( `Called MongoInstance._launchKiller(parent: ${ parentPid } , child: ${ childPid } ):` ) ;
339338 // spawn process which kills itself and mongo process if current process is dead
340- const killer = spawnChild (
341- process . env [ 'NODE' ] ?? process . argv [ 0 ] , // try Environment variable "NODE" before using argv[0]
342- [
343- path . resolve ( __dirname , '../../scripts/mongo_killer.js' ) ,
344- parentPid . toString ( ) ,
345- childPid . toString ( ) ,
346- ] ,
347- { stdio : 'pipe' }
339+ const killer = fork (
340+ path . resolve ( __dirname , '../../scripts/mongo_killer.js' ) ,
341+ [ parentPid . toString ( ) , childPid . toString ( ) ] ,
342+ {
343+ detached : true ,
344+ stdio : 'ignore' , // stdio cannot be done with an detached process cross-systems and without killing the fork on parent termination
345+ }
348346 ) ;
349347
350- killer . stdout ?. on ( 'data' , ( data ) => {
351- this . debug ( `[MongoKiller]: ${ data } ` ) ;
352- } ) ;
348+ // killer.stdout?.on('data', (data) => {
349+ // this.debug(`[MongoKiller]: ${data}`);
350+ // });
353351
354- killer . stderr ?. on ( 'data' , ( data ) => {
355- this . debug ( `[MongoKiller]: ${ data } ` ) ;
356- } ) ;
352+ // killer.stderr?.on('data', (data) => {
353+ // this.debug(`[MongoKiller]: ${data}`);
354+ // });
357355
358- [ 'exit' , 'message' , 'disconnect' , 'error' ] . forEach ( ( type ) => {
359- killer . on ( type , ( ...args ) => {
360- this . debug ( `[MongoKiller]: ${ type } - ${ JSON . stringify ( args ) } ` ) ;
361- } ) ;
362- } ) ;
356+ // ['exit', 'message', 'disconnect', 'error'].forEach((type) => {
357+ // killer.on(type, (...args) => {
358+ // this.debug(`[MongoKiller]: ${type} - ${JSON.stringify(args)}`);
359+ // });
360+ // });
361+
362+ killer . unref ( ) ; // dont force an exit on the fork when parent is exiting
363363
364364 this . emit ( MongoInstanceEvents . killerLaunched ) ;
365365
0 commit comments