@@ -20,6 +20,9 @@ let currentCAT=null;
2020var WServer ;
2121let wsServer ;
2222let wsClients = new Set ( ) ;
23+ let isShuttingDown = false ;
24+ let activeConnections = new Set ( ) ; // Track active TCP connections
25+ let activeHttpRequests = new Set ( ) ; // Track active HTTP requests for cancellation
2326
2427const 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>' ;
2528
@@ -116,7 +119,8 @@ ipcMain.on("setCAT", async (event,arg) => {
116119} ) ;
117120
118121ipcMain . on ( "quit" , async ( event , arg ) => {
119- app . isQuitting = true ;
122+ console . log ( 'Quit requested from renderer' ) ;
123+ shutdownApplication ( ) ;
120124 app . quit ( ) ;
121125 event . returnValue = true ;
122126} ) ;
@@ -127,6 +131,84 @@ ipcMain.on("radio_status_update", async (event,arg) => {
127131 event . returnValue = true ;
128132} ) ;
129133
134+ function cleanupConnections ( ) {
135+ console . log ( 'Cleaning up active TCP connections...' ) ;
136+
137+ // Close all tracked TCP connections
138+ activeConnections . forEach ( connection => {
139+ try {
140+ if ( connection && ! connection . destroyed ) {
141+ connection . destroy ( ) ;
142+ console . log ( 'Closed TCP connection' ) ;
143+ }
144+ } catch ( error ) {
145+ console . error ( 'Error closing TCP connection:' , error ) ;
146+ }
147+ } ) ;
148+
149+ // Clear the connections set
150+ activeConnections . clear ( ) ;
151+ console . log ( 'All TCP connections cleaned up' ) ;
152+
153+ // Abort all in-flight HTTP requests
154+ activeHttpRequests . forEach ( request => {
155+ try {
156+ request . abort ( ) ;
157+ console . log ( 'Aborted HTTP request' ) ;
158+ } catch ( error ) {
159+ console . error ( 'Error aborting HTTP request:' , error ) ;
160+ }
161+ } ) ;
162+
163+ // Clear the HTTP requests set
164+ activeHttpRequests . clear ( ) ;
165+ console . log ( 'All HTTP requests aborted' ) ;
166+ }
167+
168+ function shutdownApplication ( ) {
169+ if ( isShuttingDown ) {
170+ console . log ( 'Shutdown already in progress, ignoring duplicate request' ) ;
171+ return ;
172+ }
173+
174+ isShuttingDown = true ;
175+ console . log ( 'Initiating application shutdown...' ) ;
176+
177+ try {
178+ // Signal renderer to clear timers and connections
179+ if ( s_mainWindow && ! s_mainWindow . isDestroyed ( ) ) {
180+ console . log ( 'Sending cleanup signal to renderer...' ) ;
181+ s_mainWindow . webContents . send ( 'cleanup' ) ;
182+ }
183+
184+ // Clean up TCP connections
185+ cleanupConnections ( ) ;
186+
187+ // Close all servers
188+ if ( WServer ) {
189+ console . log ( 'Closing UDP server...' ) ;
190+ WServer . close ( ) ;
191+ }
192+ if ( httpServer ) {
193+ console . log ( 'Closing HTTP server...' ) ;
194+ httpServer . close ( ) ;
195+ }
196+ if ( wsServer ) {
197+ console . log ( 'Closing WebSocket server and clients...' ) ;
198+ // Close all WebSocket client connections
199+ wsClients . forEach ( client => {
200+ if ( client . readyState === WebSocket . OPEN ) {
201+ client . close ( ) ;
202+ }
203+ } ) ;
204+ wsClients . clear ( ) ;
205+ wsServer . close ( ) ;
206+ }
207+ } catch ( error ) {
208+ console . error ( 'Error during server shutdown:' , error ) ;
209+ }
210+ }
211+
130212function show_noti ( arg ) {
131213 if ( Notification . isSupported ( ) ) {
132214 try {
@@ -165,19 +247,13 @@ ipcMain.on("test", async (event,arg) => {
165247} ) ;
166248
167249app . on ( 'before-quit' , ( ) => {
168- console . log ( 'Shutting down servers...' ) ;
169- if ( WServer ) {
170- WServer . close ( ) ;
171- }
172- if ( httpServer ) {
173- httpServer . close ( ) ;
174- }
250+ console . log ( 'before-quit event triggered' ) ;
251+ shutdownApplication ( ) ;
175252} ) ;
176253
177254process . on ( 'SIGINT' , ( ) => {
178- console . log ( 'SIGINT received, closing servers...' ) ;
179- if ( WServer ) WServer . close ( ) ;
180- if ( httpServer ) httpServer . close ( ) ;
255+ console . log ( 'SIGINT received, initiating shutdown...' ) ;
256+ shutdownApplication ( ) ;
181257 process . exit ( 0 ) ;
182258} ) ;
183259
@@ -209,8 +285,12 @@ if (!gotTheLock) {
209285}
210286
211287app . on ( 'window-all-closed' , function ( ) {
288+ console . log ( 'All windows closed, initiating shutdown...' ) ;
289+ if ( ! isShuttingDown ) {
290+ shutdownApplication ( ) ;
291+ }
212292 if ( process . platform !== 'darwin' ) app . quit ( ) ;
213- app . quit ( ) ;
293+ else app . quit ( ) ;
214294} )
215295
216296function normalizeTxPwr ( adifdata ) {
@@ -310,6 +390,9 @@ function send2wavelog(o_cfg,adif, dryrun = false) {
310390 const body = [ ] ;
311391 res . on ( 'data' , ( chunk ) => body . push ( chunk ) ) ;
312392 res . on ( 'end' , ( ) => {
393+ // Remove request from tracking when completed
394+ activeHttpRequests . delete ( req ) ;
395+
313396 let resString = Buffer . concat ( body ) . toString ( ) ;
314397 if ( rej ) {
315398 if ( resString . indexOf ( 'html>' ) > 0 ) {
@@ -325,19 +408,26 @@ function send2wavelog(o_cfg,adif, dryrun = false) {
325408 } )
326409
327410 req . on ( 'error' , ( err ) => {
411+ // Remove request from tracking on error
412+ activeHttpRequests . delete ( req ) ;
328413 rej = true ;
329414 req . destroy ( ) ;
330415 result . resString = '{"status":"failed","reason":"internet problem"}' ;
331416 reject ( result ) ;
332417 } )
333418
334419 req . on ( 'timeout' , ( err ) => {
420+ // Remove request from tracking on timeout
421+ activeHttpRequests . delete ( req ) ;
335422 rej = true ;
336423 req . destroy ( ) ;
337424 result . resString = '{"status":"failed","reason":"timeout"}' ;
338425 reject ( result ) ;
339426 } )
340427
428+ // Track the HTTP request for cleanup
429+ activeHttpRequests . add ( req ) ;
430+
341431 req . write ( postData ) ;
342432 req . end ( ) ;
343433 } ) ;
@@ -619,8 +709,15 @@ async function settrx(qrg, mode = '') {
619709 client . end ( ) ;
620710 } ) ;
621711
622- client . on ( "error" , ( err ) => { } ) ;
623- client . on ( "close" , ( ) => { } ) ;
712+ // Track the connection for cleanup
713+ activeConnections . add ( client ) ;
714+
715+ client . on ( "error" , ( err ) => {
716+ activeConnections . delete ( client ) ;
717+ } ) ;
718+ client . on ( "close" , ( ) => {
719+ activeConnections . delete ( client ) ;
720+ } ) ;
624721 }
625722
626723 // Broadcast frequency/mode change to WebSocket clients
0 commit comments