Fix event emitter leak

This commit is contained in:
Mashintime 2023-01-27 10:07:32 -05:00
parent 7556de25cf
commit 393f4f5d3a

View file

@ -16,7 +16,9 @@ const mainLog = log.scope('main');
const daemonProcessLog = log.scope('freedata-daemon'); const daemonProcessLog = log.scope('freedata-daemon');
const mime = require('mime'); const mime = require('mime');
const net = require('net'); const net = require('net');
//Useful for debugging event emitter memory leaks
//require('events').EventEmitter.defaultMaxListeners = 10;
//process.traceProcessWarnings=true;
const sysInfo = log.scope('system information'); const sysInfo = log.scope('system information');
sysInfo.info("SYSTEM INFORMATION ----------------------------- "); sysInfo.info("SYSTEM INFORMATION ----------------------------- ");
@ -875,6 +877,8 @@ ipcMain.on('request-stop-rigctld',(event,data)=>{
// create new socket so we are not reopening every time a new one // create new socket so we are not reopening every time a new one
var rigctld_connection = new net.Socket(); var rigctld_connection = new net.Socket();
var rigctld_connection_state = false; var rigctld_connection_state = false;
var rigctld_events_wired = false;
ipcMain.on('request-check-rigctld',(event, data)=>{ ipcMain.on('request-check-rigctld',(event, data)=>{
try{ try{
@ -885,41 +889,44 @@ ipcMain.on('request-check-rigctld',(event, data)=>{
if(!rigctld_connection_state){ if(!rigctld_connection_state){
rigctld_connection = new net.Socket(); rigctld_connection = new net.Socket();
rigctld_events_wired = false;
rigctld_connection.connect(data.port, data.ip) rigctld_connection.connect(data.port, data.ip)
} }
// check if we have created a new socket object // Check if we have created a new socket object and attach listeners if not already created
if (typeof(rigctld_connection) != 'undefined') { if (typeof(rigctld_connection) != 'undefined' && !rigctld_events_wired) {
rigctld_connection.on('connect', function() { rigctld_connection.on('connect', function() {
rigctld_connection_state = true; rigctld_events_wired=true;
Data["state"] = "connection possible - (" + data.ip + ":" + data.port + ")"; mainLog.info("Starting rigctld event listeners");
if (win !== null && win !== '' && typeof(win) != 'undefined'){ rigctld_connection_state = true;
// try catch for being sure we have a clean app close Data["state"] = "connection possible - (" + data.ip + ":" + data.port + ")";
try{ if (win !== null && win !== '' && typeof(win) != 'undefined'){
win.webContents.send('action-check-rigctld', Data); // try catch for being sure we have a clean app close
} catch(e){ try{
console.log(e) win.webContents.send('action-check-rigctld', Data);
} catch(e){
console.log(e)
}
} }
} })
})
rigctld_connection.on('error', function() { rigctld_connection.on('error', function() {
rigctld_connection_state = false; rigctld_connection_state = false;
Data["state"] = "unknown/stopped - (" + data.ip + ":" + data.port + ")"; Data["state"] = "unknown/stopped - (" + data.ip + ":" + data.port + ")";
if (win !== null && win !== '' && typeof(win) != 'undefined'){ if (win !== null && win !== '' && typeof(win) != 'undefined'){
// try catch for being sure we have a clean app close // try catch for being sure we have a clean app close
try{ try{
win.webContents.send('action-check-rigctld', Data); win.webContents.send('action-check-rigctld', Data);
} catch(e){ } catch(e){
console.log(e) console.log(e)
}
} }
} })
})
rigctld_connection.on('end', function() { rigctld_connection.on('end', function() {
rigctld_connection_state = false; rigctld_connection_state = false;
}) })
} }