@@ -28,13 +28,15 @@ import * as PlatformError from "effect/PlatformError";
2828import * as Path from "effect/Path" ;
2929import * as Queue from "effect/Queue" ;
3030import * as Ref from "effect/Ref" ;
31+ import * as References from "effect/References" ;
3132import * as Schedule from "effect/Schedule" ;
3233import * as Schema from "effect/Schema" ;
3334import * as Scope from "effect/Scope" ;
3435import * as Stream from "effect/Stream" ;
36+ import * as SynchronizedRef from "effect/SynchronizedRef" ;
3537import * as TestClock from "effect/testing/TestClock" ;
3638import { ChildProcessSpawner } from "effect/process" ;
37- import { expect } from "vite-plus/test" ;
39+ import { expect , vi } from "vite-plus/test" ;
3840
3941import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
4042import * as ServerConfig from "../config.ts" ;
@@ -44,6 +46,11 @@ import * as ServerSettings from "../serverSettings.ts";
4446import * as TerminalManager from "./Manager.ts" ;
4547import * as PtyAdapter from "@t3tools/shared/PtyAdapter" ;
4648
49+ vi . mock ( "effect/SynchronizedRef" , async ( importOriginal ) => {
50+ const original = await importOriginal < typeof SynchronizedRef > ( ) ;
51+ return { ...original , make : vi . fn ( original . make ) } ;
52+ } ) ;
53+
4754const encodeUnknownJson = Schema . encodeUnknownSync ( Schema . fromJsonString ( Schema . Unknown ) ) ;
4855
4956class WaitForConditionError extends Data . TaggedError ( "WaitForConditionError" ) < {
@@ -61,6 +68,7 @@ class FakePtyProcess implements PtyAdapter.PtyProcess {
6168 private readonly exitListeners = new Set < ( event : PtyAdapter . PtyExitEvent ) => void > ( ) ;
6269 killed = false ;
6370 exitOnSubscribe : PtyAdapter . PtyExitEvent | undefined ;
71+ onKill : ( ( signal ?: string ) => void ) | undefined ;
6472
6573 constructor ( pid : number ) {
6674 this . pid = pid ;
@@ -83,6 +91,7 @@ class FakePtyProcess implements PtyAdapter.PtyProcess {
8391 kill ( signal ?: string ) : void {
8492 this . killed = true ;
8593 this . killSignals . push ( signal ) ;
94+ this . onKill ?.( signal ) ;
8695 }
8796
8897 onData ( callback : ( data : string ) => void ) : ( ) => void {
@@ -106,6 +115,10 @@ class FakePtyProcess implements PtyAdapter.PtyProcess {
106115 }
107116 }
108117
118+ get exitListenerCount ( ) : number {
119+ return this . exitListeners . size ;
120+ }
121+
109122 emitExit ( event : PtyAdapter . PtyExitEvent ) : void {
110123 for ( const listener of this . exitListeners ) {
111124 listener ( event ) ;
@@ -1792,6 +1805,94 @@ it.layer(
17921805 } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
17931806 ) ;
17941807
1808+ it . effect ( "skips SIGKILL when the process exits during the grace period" , ( ) =>
1809+ Effect . gen ( function * ( ) {
1810+ const { manager, ptyAdapter } = yield * createManager ( 5 , { processKillGraceMs : 10_000 } ) ;
1811+ yield * manager . open ( openInput ( ) ) ;
1812+ const process = ptyAdapter . processes [ 0 ] ;
1813+ expect ( process ) . toBeDefined ( ) ;
1814+ if ( ! process ) return ;
1815+
1816+ const sigterm = yield * Deferred . make < void > ( ) ;
1817+ process . onKill = ( signal ) => {
1818+ if ( signal === "SIGTERM" ) Deferred . doneUnsafe ( sigterm , Effect . void ) ;
1819+ } ;
1820+
1821+ yield * manager . close ( { threadId : "thread-1" } ) ;
1822+ yield * Deferred . await ( sigterm ) ;
1823+ process . emitExit ( { exitCode : 0 , signal : 15 } ) ;
1824+ yield * TestClock . adjust ( "10 seconds" ) ;
1825+
1826+ assert . deepEqual ( process . killSignals , [ "SIGTERM" ] ) ;
1827+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1828+ ) ;
1829+
1830+ it . effect ( "does not retain a kill fiber that finishes before registration" , ( ) =>
1831+ Effect . gen ( function * ( ) {
1832+ let managerState :
1833+ | SynchronizedRef . SynchronizedRef < {
1834+ killFibers : Map < PtyAdapter . PtyProcess , Fiber . Fiber < void , never > > ;
1835+ } >
1836+ | undefined ;
1837+ const originalMake = < A > ( value : A ) => Effect . sync ( ( ) => SynchronizedRef . makeUnsafe ( value ) ) ;
1838+ const makeSpy = vi . spyOn ( SynchronizedRef , "make" ) . mockImplementation ( ( value ) =>
1839+ originalMake ( value ) . pipe (
1840+ Effect . map ( ( ref ) => {
1841+ if ( typeof value === "object" && value !== null && "killFibers" in value ) {
1842+ managerState = ref as typeof managerState ;
1843+ }
1844+ return ref ;
1845+ } ) ,
1846+ ) ,
1847+ ) ;
1848+ yield * Effect . addFinalizer ( ( ) => Effect . sync ( ( ) => makeSpy . mockRestore ( ) ) ) ;
1849+ for ( const budget of [ 10 , 15 , 20 , 25 , 30 , 40 , 50 , 75 , 100 , 150 , 200 , 500 ] ) {
1850+ const { manager, ptyAdapter } = yield * createManager ( ) ;
1851+ yield * manager . open ( openInput ( ) ) ;
1852+ const process = ptyAdapter . processes [ 0 ] ! ;
1853+ process . onKill = ( ) => process . emitExit ( { exitCode : 0 , signal : 15 } ) ;
1854+ // Vary the caller's operation budget to cover child completion before
1855+ // and after the parent resumes its registration step.
1856+ yield * manager
1857+ . close ( { threadId : "thread-1" } )
1858+ . pipe ( Effect . provideService ( References . MaxOpsBeforeYield , budget ) ) ;
1859+ yield * Effect . yieldNow ;
1860+ expect ( managerState ) . toBeDefined ( ) ;
1861+ yield * Fiber . awaitAll ( SynchronizedRef . getUnsafe ( managerState ! ) . killFibers . values ( ) ) ;
1862+ expect ( SynchronizedRef . getUnsafe ( managerState ! ) . killFibers . size , `budget ${ budget } ` ) . toBe (
1863+ 0 ,
1864+ ) ;
1865+ expect ( process . killSignals ) . toEqual ( [ "SIGTERM" ] ) ;
1866+ }
1867+ } ) ,
1868+ ) ;
1869+
1870+ it . effect ( "releases the exit listener when kill escalation is interrupted" , ( ) =>
1871+ Effect . gen ( function * ( ) {
1872+ const scope = yield * Scope . make ( "sequential" ) ;
1873+ const { manager, ptyAdapter } = yield * createManager ( 5 , {
1874+ processKillGraceMs : 10_000 ,
1875+ } ) . pipe ( Effect . provideService ( Scope . Scope , scope ) ) ;
1876+ yield * manager . open ( openInput ( ) ) ;
1877+ const process = ptyAdapter . processes [ 0 ] ;
1878+ expect ( process ) . toBeDefined ( ) ;
1879+ if ( ! process ) return ;
1880+
1881+ const sigterm = yield * Deferred . make < void > ( ) ;
1882+ process . onKill = ( signal ) => {
1883+ if ( signal === "SIGTERM" ) Deferred . doneUnsafe ( sigterm , Effect . void ) ;
1884+ } ;
1885+ yield * manager . close ( { threadId : "thread-1" } ) ;
1886+ yield * Deferred . await ( sigterm ) ;
1887+ assert . isAbove ( process . exitListenerCount , 0 ) ;
1888+
1889+ yield * Scope . close ( scope , Exit . void ) ;
1890+
1891+ assert . equal ( process . exitListenerCount , 0 ) ;
1892+ assert . deepEqual ( process . killSignals , [ "SIGTERM" ] ) ;
1893+ } ) . pipe ( Effect . provide ( TestClock . layer ( ) ) ) ,
1894+ ) ;
1895+
17951896 it . effect ( "publishes closed events when terminals are explicitly closed" , ( ) =>
17961897 Effect . gen ( function * ( ) {
17971898 const { manager, getEvents } = yield * createManager ( ) ;
0 commit comments