@@ -851,7 +851,7 @@ describe("CloudManagedEndpointRuntime", () => {
851851 ) ;
852852
853853 it . effect (
854- "runs an older relay client while the pinned one installs, then restarts on it and prunes" ,
854+ "runs an older relay client while the pinned one installs, then hands off to it and prunes" ,
855855 ( ) =>
856856 Effect . gen ( function * ( ) {
857857 const olderClient = {
@@ -870,15 +870,19 @@ describe("CloudManagedEndpointRuntime", () => {
870870 const releaseInstall = yield * Deferred . make < void > ( ) ;
871871 const pruned = yield * Deferred . make < void > ( ) ;
872872 const spawned = yield * Queue . unbounded < string > ( ) ;
873- const killed : Array < string > = [ ] ;
873+ const olderStopped = yield * Deferred . make < void > ( ) ;
874874 const output = yield * Queue . unbounded < Uint8Array > ( ) ;
875875 const spawner = ChildProcessSpawner . make ( ( command ) =>
876876 Effect . gen ( function * ( ) {
877877 const executable = ChildProcess . isStandardCommand ( command ) ? command . command : "" ;
878878 yield * Queue . offer ( spawned , executable ) ;
879879 const handle = makeHandle ( {
880880 pid : executable === pinnedClient . executablePath ? 2 : 1 ,
881- onKill : ( ) => killed . push ( executable ) ,
881+ onKill : ( ) => {
882+ if ( executable === olderClient . executablePath ) {
883+ Deferred . doneUnsafe ( olderStopped , Effect . void ) ;
884+ }
885+ } ,
882886 ...( executable === pinnedClient . executablePath
883887 ? { output : Stream . fromQueue ( output ) }
884888 : { } ) ,
@@ -916,14 +920,16 @@ describe("CloudManagedEndpointRuntime", () => {
916920
917921 yield * Deferred . succeed ( releaseInstall , undefined ) ;
918922 expect ( yield * Queue . take ( spawned ) ) . toBe ( pinnedClient . executablePath ) ;
919- expect ( killed ) . toEqual ( [ olderClient . executablePath ] ) ;
923+ // The older connector keeps serving until the pinned one registers.
924+ expect ( Option . isNone ( yield * Deferred . poll ( olderStopped ) ) ) . toBe ( true ) ;
920925
921926 yield * Queue . offer (
922927 output ,
923928 new TextEncoder ( ) . encode (
924929 "2026-10-08T00:00:00Z INF Registered tunnel connection connIndex=0\n" ,
925930 ) ,
926931 ) ;
932+ yield * Deferred . await ( olderStopped ) ;
927933 yield * Deferred . await ( pruned ) ;
928934 } ) ,
929935 ) ;
@@ -1118,7 +1124,7 @@ describe("CloudManagedEndpointRuntime", () => {
11181124 } ) ,
11191125 ) ;
11201126
1121- it . effect ( "requests recovery when the updated relay client fails to start" , ( ) =>
1127+ it . effect ( "keeps the older connector when the updated relay client fails to start" , ( ) =>
11221128 Effect . gen ( function * ( ) {
11231129 const olderClient = {
11241130 status : "available" ,
@@ -1133,18 +1139,26 @@ describe("CloudManagedEndpointRuntime", () => {
11331139 version : RelayClient . CLOUDFLARED_VERSION ,
11341140 } as const ;
11351141 let installed = false ;
1142+ let olderKilled = false ;
11361143 const releaseInstall = yield * Deferred . make < void > ( ) ;
1144+ const pinnedAttempted = yield * Deferred . make < void > ( ) ;
11371145 const spawner = ChildProcessSpawner . make ( ( command ) =>
11381146 Effect . gen ( function * ( ) {
11391147 const executable = ChildProcess . isStandardCommand ( command ) ? command . command : "" ;
11401148 if ( executable === pinnedClient . executablePath ) {
1149+ yield * Deferred . succeed ( pinnedAttempted , undefined ) ;
11411150 return yield * PlatformError . systemError ( {
11421151 _tag : "PermissionDenied" ,
11431152 module : "ChildProcess" ,
11441153 method : "spawn" ,
11451154 } ) ;
11461155 }
1147- const handle = makeHandle ( { pid : 6 , onKill : ( ) => { } } ) ;
1156+ const handle = makeHandle ( {
1157+ pid : 6 ,
1158+ onKill : ( ) => {
1159+ olderKilled = true ;
1160+ } ,
1161+ } ) ;
11481162 yield * Effect . addFinalizer ( ( ) => handle . kill ( ) . pipe ( Effect . ignore ) ) ;
11491163 return handle ;
11501164 } ) ,
@@ -1171,6 +1185,68 @@ describe("CloudManagedEndpointRuntime", () => {
11711185 const config = { providerKind : "cloudflare_tunnel" , connectorToken : "token" } as const ;
11721186 expect ( yield * runtime . applyConfig ( config ) ) . toMatchObject ( { status : "running" , pid : 6 } ) ;
11731187
1188+ yield * Deferred . succeed ( releaseInstall , undefined ) ;
1189+ yield * Deferred . await ( pinnedAttempted ) ;
1190+ // The failed swap leaves the older connector active and running.
1191+ expect ( yield * runtime . applyConfig ( config ) ) . toMatchObject ( { status : "running" , pid : 6 } ) ;
1192+ expect ( olderKilled ) . toBe ( false ) ;
1193+ } ) ,
1194+ ) ;
1195+
1196+ it . effect ( "requests recovery when the older connector exited during a failed swap" , ( ) =>
1197+ Effect . gen ( function * ( ) {
1198+ const olderClient = {
1199+ status : "available" ,
1200+ executablePath : "/managed/2025.9.0/cloudflared" ,
1201+ source : "managed" ,
1202+ version : "2025.9.0" ,
1203+ } as const ;
1204+ const pinnedClient = {
1205+ status : "available" ,
1206+ executablePath : "/managed/pinned/cloudflared" ,
1207+ source : "managed" ,
1208+ version : RelayClient . CLOUDFLARED_VERSION ,
1209+ } as const ;
1210+ let installed = false ;
1211+ const releaseInstall = yield * Deferred . make < void > ( ) ;
1212+ const spawner = ChildProcessSpawner . make ( ( command ) =>
1213+ Effect . gen ( function * ( ) {
1214+ const executable = ChildProcess . isStandardCommand ( command ) ? command . command : "" ;
1215+ if ( executable === pinnedClient . executablePath ) {
1216+ return yield * PlatformError . systemError ( {
1217+ _tag : "PermissionDenied" ,
1218+ module : "ChildProcess" ,
1219+ method : "spawn" ,
1220+ } ) ;
1221+ }
1222+ // The older connector has exited by the time the swap fails.
1223+ const handle = makeHandle ( { pid : 12 , onKill : ( ) => { } , isRunning : ( ) => ! installed } ) ;
1224+ yield * Effect . addFinalizer ( ( ) => handle . kill ( ) . pipe ( Effect . ignore ) ) ;
1225+ return handle ;
1226+ } ) ,
1227+ ) ;
1228+ const runtime = yield * buildCloudManagedEndpointRuntime (
1229+ spawner ,
1230+ Layer . succeed (
1231+ RelayClient . RelayClient ,
1232+ RelayClient . RelayClient . of ( {
1233+ resolve : Effect . sync ( ( ) => ( installed ? pinnedClient : olderClient ) ) ,
1234+ install : Deferred . await ( releaseInstall ) . pipe (
1235+ Effect . andThen (
1236+ Effect . sync ( ( ) => {
1237+ installed = true ;
1238+ return pinnedClient ;
1239+ } ) ,
1240+ ) ,
1241+ ) ,
1242+ installWithProgress : ( ) => Effect . die ( "unused" ) ,
1243+ pruneManagedVersions : Effect . void ,
1244+ } ) ,
1245+ ) ,
1246+ ) ;
1247+ const config = { providerKind : "cloudflare_tunnel" , connectorToken : "token" } as const ;
1248+ expect ( yield * runtime . applyConfig ( config ) ) . toMatchObject ( { status : "running" , pid : 12 } ) ;
1249+
11741250 yield * Deferred . succeed ( releaseInstall , undefined ) ;
11751251 expect ( Option . getOrThrow ( yield * Stream . runHead ( runtime . recoveryRequests ) ) ) . toEqual ( config ) ;
11761252 } ) ,
0 commit comments