@@ -11,6 +11,8 @@ let cfg={};
1111let active_cfg = 0 ;
1212let trxpoll = undefined ;
1313let 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
1517const { ipcRenderer} = require ( 'electron' )
1618const 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
268270async 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
422478function updateUtcTime ( ) {
0 commit comments