Skip to content

Commit 254f779

Browse files
committed
More Cleanup on quit
1 parent 3c3610c commit 254f779

File tree

2 files changed

+120
-8
lines changed

2 files changed

+120
-8
lines changed

main.js

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ var WServer;
1717
let wsServer;
1818
let wsClients = new Set();
1919
let isShuttingDown = false;
20+
let activeConnections = new Set(); // Track active TCP connections
21+
let activeHttpRequests = new Set(); // Track active HTTP requests for cancellation
2022

2123
const DemoAdif='<call:5>DJ7NT <gridsquare:4>JO30 <mode:3>FT8 <rst_sent:3>-15 <rst_rcvd:2>33 <qso_date:8>20240110 <time_on:6>051855 <qso_date_off:8>20240110 <time_off:6>051855 <band:3>40m <freq:8>7.155783 <station_callsign:5>TE1ST <my_gridsquare:6>JO30OO <eor>';
2224

@@ -125,6 +127,40 @@ ipcMain.on("radio_status_update", async (event,arg) => {
125127
event.returnValue=true;
126128
});
127129

130+
function cleanupConnections() {
131+
console.log('Cleaning up active TCP connections...');
132+
133+
// Close all tracked TCP connections
134+
activeConnections.forEach(connection => {
135+
try {
136+
if (connection && !connection.destroyed) {
137+
connection.destroy();
138+
console.log('Closed TCP connection');
139+
}
140+
} catch (error) {
141+
console.error('Error closing TCP connection:', error);
142+
}
143+
});
144+
145+
// Clear the connections set
146+
activeConnections.clear();
147+
console.log('All TCP connections cleaned up');
148+
149+
// Abort all in-flight HTTP requests
150+
activeHttpRequests.forEach(request => {
151+
try {
152+
request.abort();
153+
console.log('Aborted HTTP request');
154+
} catch (error) {
155+
console.error('Error aborting HTTP request:', error);
156+
}
157+
});
158+
159+
// Clear the HTTP requests set
160+
activeHttpRequests.clear();
161+
console.log('All HTTP requests aborted');
162+
}
163+
128164
function shutdownApplication() {
129165
if (isShuttingDown) {
130166
console.log('Shutdown already in progress, ignoring duplicate request');
@@ -135,12 +171,15 @@ function shutdownApplication() {
135171
console.log('Initiating application shutdown...');
136172

137173
try {
138-
// Signal renderer to clear timers
174+
// Signal renderer to clear timers and connections
139175
if (s_mainWindow && !s_mainWindow.isDestroyed()) {
140176
console.log('Sending cleanup signal to renderer...');
141177
s_mainWindow.webContents.send('cleanup');
142178
}
143179

180+
// Clean up TCP connections
181+
cleanupConnections();
182+
144183
// Close all servers
145184
if (WServer) {
146185
console.log('Closing UDP server...');
@@ -347,6 +386,9 @@ function send2wavelog(o_cfg,adif, dryrun = false) {
347386
const body = [];
348387
res.on('data', (chunk) => body.push(chunk));
349388
res.on('end', () => {
389+
// Remove request from tracking when completed
390+
activeHttpRequests.delete(req);
391+
350392
let resString = Buffer.concat(body).toString();
351393
if (rej) {
352394
if (resString.indexOf('html>')>0) {
@@ -362,19 +404,26 @@ function send2wavelog(o_cfg,adif, dryrun = false) {
362404
})
363405

364406
req.on('error', (err) => {
407+
// Remove request from tracking on error
408+
activeHttpRequests.delete(req);
365409
rej=true;
366410
req.destroy();
367411
result.resString='{"status":"failed","reason":"internet problem"}';
368412
reject(result);
369413
})
370414

371415
req.on('timeout', (err) => {
416+
// Remove request from tracking on timeout
417+
activeHttpRequests.delete(req);
372418
rej=true;
373419
req.destroy();
374420
result.resString='{"status":"failed","reason":"timeout"}';
375421
reject(result);
376422
})
377423

424+
// Track the HTTP request for cleanup
425+
activeHttpRequests.add(req);
426+
378427
req.write(postData);
379428
req.end();
380429
});
@@ -656,8 +705,15 @@ async function settrx(qrg, mode = '') {
656705
client.end();
657706
});
658707

659-
client.on("error", (err) => {});
660-
client.on("close", () => {});
708+
// Track the connection for cleanup
709+
activeConnections.add(client);
710+
711+
client.on("error", (err) => {
712+
activeConnections.delete(client);
713+
});
714+
client.on("close", () => {
715+
activeConnections.delete(client);
716+
});
661717
}
662718

663719
// Broadcast frequency/mode change to WebSocket clients

renderer.js

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ let cfg={};
1111
let active_cfg=0;
1212
let trxpoll=undefined;
1313
let utcTimeInterval=undefined;
14+
let activeConnections = new Set(); // Track active TCP connections in renderer
15+
let activeAbortControllers = new Set(); // Track active HTTP requests for cancellation
1416

1517
const {ipcRenderer} = require('electron')
1618
const net = require('net');
@@ -82,7 +84,7 @@ $(document).ready(function() {
8284
});
8385

8486
bt_quit.addEventListener('click', () => {
85-
cleanupTimers(); // Clear all timers before quit
87+
cleanup(); // Clear all timers and connections before quit
8688
const x=ipcRenderer.sendSync("quit", '');
8789
});
8890

@@ -143,7 +145,7 @@ $(document).ready(function() {
143145

144146
// Handle cleanup request from main process
145147
ipcRenderer.on('cleanup', () => {
146-
cleanupTimers();
148+
cleanup();
147149
});
148150

149151
// Dropdown change handler
@@ -267,6 +269,9 @@ async function get_trx() {
267269

268270
async function getInfo(which) {
269271
if (cfg.profiles[active_cfg].flrig_ena){
272+
const abortController = new AbortController();
273+
activeAbortControllers.add(abortController);
274+
270275
try {
271276
const response = await fetch(
272277
"http://"+$("#radio_host").val()+':'+$("#radio_port").val(), {
@@ -277,8 +282,10 @@ async function getInfo(which) {
277282
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
278283
},
279284
body: '<?xml version="1.0"?><methodCall><methodName>'+which+'</methodName></methodCall>',
285+
signal: abortController.signal
280286
}
281287
);
288+
282289
const data = await response.text();
283290
const parser = new DOMParser();
284291
const xmlDoc = parser.parseFromString(data, "application/xml");
@@ -299,6 +306,9 @@ async function getInfo(which) {
299306
}
300307
} catch (e) {
301308
return '';
309+
} finally {
310+
// Always clean up abort controller when done
311+
activeAbortControllers.delete(abortController);
302312
}
303313
}
304314
if (cfg.profiles[active_cfg].hamlib_ena) {
@@ -310,6 +320,10 @@ async function getInfo(which) {
310320
return new Promise((resolve, reject) => {
311321
if (commands[which]) {
312322
const client = net.createConnection({ host, port }, () => client.write(commands[which]));
323+
324+
// Track the connection for cleanup
325+
activeConnections.add(client);
326+
313327
client.on('data', (data) => {
314328
data = data.toString()
315329
if(data.startsWith("RPRT")){
@@ -319,8 +333,13 @@ async function getInfo(which) {
319333
}
320334
client.end();
321335
});
322-
client.on('error', (err) => reject());
323-
client.on("close", () => {});
336+
client.on('error', (err) => {
337+
activeConnections.delete(client);
338+
reject();
339+
});
340+
client.on("close", () => {
341+
activeConnections.delete(client);
342+
});
324343
} else {
325344
resolve(undefined);
326345
}
@@ -403,7 +422,41 @@ async function informWavelog(CAT) {
403422
return x;
404423
}
405424

406-
function cleanupTimers() {
425+
function cleanupConnections() {
426+
console.log('Cleaning up renderer TCP connections...');
427+
428+
// Close all tracked TCP connections
429+
activeConnections.forEach(connection => {
430+
try {
431+
if (connection && !connection.destroyed) {
432+
connection.destroy();
433+
console.log('Closed renderer TCP connection');
434+
}
435+
} catch (error) {
436+
console.error('Error closing renderer TCP connection:', error);
437+
}
438+
});
439+
440+
// Clear the connections set
441+
activeConnections.clear();
442+
console.log('All renderer TCP connections cleaned up');
443+
444+
// Abort all in-flight HTTP requests
445+
activeAbortControllers.forEach(controller => {
446+
try {
447+
controller.abort();
448+
console.log('Aborted HTTP request');
449+
} catch (error) {
450+
console.error('Error aborting HTTP request:', error);
451+
}
452+
});
453+
454+
// Clear the abort controllers set
455+
activeAbortControllers.clear();
456+
console.log('All HTTP requests aborted');
457+
}
458+
459+
function cleanup() {
407460
// Clear radio polling timeout
408461
if (trxpoll) {
409462
clearTimeout(trxpoll);
@@ -417,6 +470,9 @@ function cleanupTimers() {
417470
utcTimeInterval = undefined;
418471
console.log('Cleared UTC time update interval');
419472
}
473+
474+
// Clean up TCP connections
475+
cleanupConnections();
420476
}
421477

422478
function updateUtcTime() {

0 commit comments

Comments
 (0)