@@ -4,12 +4,14 @@ const ipc = require('electron').ipcMain;
44const path = require ( 'path' ) ;
55const util = require ( 'util' ) ;
66const exec = require ( 'child_process' ) . exec ;
7+ const psTree = require ( 'ps-tree' ) ;
78
89// console.log('loading functions-rpc.js');
910
1011// const spawn = require('child_process').spawn;
1112
1213global . filename = undefined ;
14+ var child ;
1315
1416function openFileFromSidebar ( filepath ) {
1517 // console.log(filepath);
@@ -245,6 +247,31 @@ ipc.on('close-app',function(event){
245247 app . quit ( ) ;
246248} )
247249
250+ function killProcess ( pid , signal , callback ) {
251+ signal = signal || 'SIGKILL' ;
252+ callback = callback || function ( ) { } ;
253+ var killTree = true ;
254+ if ( killTree ) {
255+ psTree ( pid , function ( err , children ) {
256+ [ pid ] . concat (
257+ children . map ( function ( p ) {
258+ return p . PID ;
259+ } )
260+ ) . forEach ( function ( tpid ) {
261+ try { process . kill ( tpid , signal ) }
262+ catch ( ex ) { }
263+ } ) ;
264+ callback ( ) ;
265+ } ) ;
266+ } else {
267+ try { process . kill ( pid , signal ) }
268+ catch ( ex ) { }
269+ callback ( ) ;
270+ }
271+ } ;
272+
273+
274+
248275function makeCommand ( filepath ) {
249276 // console.log(filepath)
250277 let filename = path . basename ( filepath ) ;
@@ -319,24 +346,37 @@ ipc.on('runProgram',function(event,input,filepath){
319346 mainWindow . webContents . send ( 'runProgramStatus' , err . message ) ;
320347 // console.log(err.message);
321348 } else {
322- let child = exec ( command , { maxBuffer : 1024 * 500 } , function ( err , stdout , stderr ) {
349+ child = exec ( command , { maxBuffer : 1024 * 10000 } , function ( err , stdout , stderr ) {
323350 if ( err ) {
351+ console . log ( err , 'line 324' ) ;
352+ killProcess ( child . pid ) ;
353+ // child.kill();
354+ // console.log(child);
324355 console . log ( err ) ;
356+ if ( child . killed ) {
357+ err . message = "Process has been killed"
358+ }
325359 mainWindow . webContents . send ( 'runProgramStatus' , err . message ) ;
326360 // console.log(err.message.split('\n')[1]);
327361 } else {
328362 mainWindow . webContents . send ( 'runProgramStatus' , stdout ) ;
329363 // console.log(stdout);
330- var to = setTimeout ( function ( ) {
331- child . kill ( ) ;
332- } , 2000 ) ;
333364 }
334365 } ) ;
335366 }
336367 } ) ;
337368 }
338369 }
339370} ) ;
371+
372+ ipc . on ( 'stopProgram' , function ( event ) {
373+ if ( child ) {
374+ child . killed = true ;
375+ killProcess ( child . pid ) ;
376+
377+ }
378+ } ) ;
379+
340380ipc . on ( 'settingsChangeTheme' , ( event , data ) => {
341381 let settings_file = fs . readFileSync ( path . join ( getUserDataPath ( ) , 'settings.json' ) ) ;
342382 settings_file = JSON . parse ( settings_file ) ;
0 commit comments