@@ -12,6 +12,7 @@ import type * as Electron from "electron";
1212import * as HostProcess from "@t3tools/shared/HostProcess" ;
1313
1414import * as DesktopEnvironment from "./DesktopEnvironment.ts" ;
15+ import { stopChildProcessesBeforeExit } from "./DesktopExitChildren.ts" ;
1516import { makeComponentLogger } from "./DesktopObservability.ts" ;
1617import * as DesktopShutdown from "./DesktopShutdown.ts" ;
1718import * as ElectronApp from "../electron/ElectronApp.ts" ;
@@ -69,15 +70,18 @@ function addScopedListener<Args extends ReadonlyArray<unknown>>(
6970 target : unknown ,
7071 eventName : string ,
7172 listener : ( ...args : Args ) => void ,
73+ options : { readonly prepend ?: boolean } = { } ,
7274) : Effect . Effect < void , never , Scope . Scope > {
7375 const eventTarget = target as {
7476 on : ( eventName : string , listener : ( ...args : Array < unknown > ) => void ) => unknown ;
77+ prependListener : ( eventName : string , listener : ( ...args : Array < unknown > ) => void ) => unknown ;
7578 removeListener : ( eventName : string , listener : ( ...args : Array < unknown > ) => void ) => unknown ;
7679 } ;
7780 const untypedListener = listener as unknown as ( ...args : Array < unknown > ) => void ;
7881 return Effect . acquireRelease (
7982 Effect . sync ( ( ) => {
80- eventTarget . on ( eventName , untypedListener ) ;
83+ if ( options . prepend ) eventTarget . prependListener ( eventName , untypedListener ) ;
84+ else eventTarget . on ( eventName , untypedListener ) ;
8185 } ) ,
8286 ( ) =>
8387 Effect . sync ( ( ) => {
@@ -300,6 +304,34 @@ export const make = DesktopLifecycle.of({
300304 ) ;
301305 } ) ;
302306
307+ // Electron can exit without before-quit, e.g. when the X11/Wayland connection drops.
308+ // Children (the backend too) get SIGTERM while main still holds an AppImage mount open.
309+ // Prepended so the record is buffered before the trace sink's own exit flush runs.
310+ yield * addScopedListener (
311+ process ,
312+ "exit" ,
313+ ( exitCode : number ) => {
314+ Effect . runSyncWith ( context ) (
315+ Effect . gen ( function * ( ) {
316+ const shutdown = yield * DesktopShutdown . DesktopShutdown ;
317+ // "allowed" without a finished shutdown is the updater's quit; its relaunched app is a child.
318+ if ( quit . phase === "allowed" || ( yield * shutdown . isComplete ) ) return ;
319+ const attributes = { exitCode, quitPhase : quit . phase } ;
320+ yield * Effect . gen ( function * ( ) {
321+ yield * logLifecycleError (
322+ "desktop main process exiting before shutdown completed" ,
323+ attributes ,
324+ ) ;
325+ if ( environment . platform !== "linux" ) return ;
326+ const killed = stopChildProcessesBeforeExit ( process . pid ) ;
327+ yield * Effect . annotateCurrentSpan ( { killedChildPids : killed } ) ;
328+ } ) . pipe ( Effect . withSpan ( "desktop.lifecycle.exit" , { attributes } ) ) ;
329+ } ) ,
330+ ) ;
331+ } ,
332+ { prepend : true } ,
333+ ) ;
334+
303335 if ( environment . platform !== "win32" ) {
304336 yield * addScopedListener ( process , "SIGINT" , ( ) => {
305337 quitFromSignal ( "SIGINT" , runEffect , quit ) ;
0 commit comments