@@ -98,8 +98,8 @@ const ARCHIVE = { archiveVersion: "1.2.3-preview.20260911.4" } as const;
9898// Emulates `t3 serve`: it takes the home owner lock at startup, then binds
9999// once a second server has tried the same home (or after 2s), so a concurrent
100100// launch always sees the home still unclaimed. Like the real server, it
101- // publishes server-runtime.json some time after it starts answering HTTP.
102- const FAKE_T3_SERVE = `const fs = require("node:fs");
101+ // publishes server-runtime.json after it starts answering HTTP, if at all .
102+ const fakeT3Serve = ( publishDelayMs : number | null ) => `const fs = require("node:fs");
103103const http = require("node:http");
104104const path = require("node:path");
105105const args = process.argv.slice(2);
@@ -122,9 +122,10 @@ const serve = (tries) => {
122122 }
123123 http.createServer((_, response) => response.end()).listen(port, "127.0.0.1", () => {
124124 const origin = "http://127.0.0.1:" + port;
125+ ${ publishDelayMs === null ? "return;" : "" }
125126 setTimeout(() => {
126127 fs.writeFileSync(path.join(userdata, "server-runtime.json"), JSON.stringify({ pid: process.pid, port, origin }));
127- }, 1_500 );
128+ }, ${ publishDelayMs } );
128129 });
129130};
130131serve(40);
@@ -701,61 +702,71 @@ describe("ssh tunnel scripts", () => {
701702 ) ;
702703
703704 it . live ( "concurrent launches on one remote home record the server that owns it" , ( ) =>
704- Effect . gen ( function * ( ) {
705- const fs = yield * FileSystem . FileSystem ;
706- const path = yield * Path . Path ;
707- const home = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-ssh-launch-" } ) ;
708- yield * Effect . addFinalizer ( ( ) =>
709- fs . readFileString ( path . join ( home , "servers" ) ) . pipe (
710- Effect . map ( ( pids ) => {
711- for ( const pid of pids . split ( "\n" ) . filter ( Boolean ) ) {
712- try {
713- process . kill ( Number ( pid ) , "SIGKILL" ) ;
714- } catch { }
715- }
716- } ) ,
717- Effect . ignore ,
718- ) ,
719- ) ;
720- const serverScript = path . join ( home , "fake-t3.cjs" ) ;
721- yield * fs . writeFileString ( serverScript , FAKE_T3_SERVE ) ;
722- // A launch that died holding the lock must not block or double-admit later ones.
723- const launchLock = path . join ( home , ".t3" , "ssh-launch" , ".launch.lock" ) ;
724- yield * fs . makeDirectory ( path . dirname ( launchLock ) , { recursive : true } ) ;
725- yield * fs . writeFileString ( launchLock , "999999999\n" ) ;
726- const targets = [
727- { alias : "devbox" , hostname : "devbox" , username : null , port : null } ,
728- { alias : "devbox-tailnet" , hostname : "devbox-tailnet" , username : null , port : null } ,
729- ] ;
705+ // The second row's server never publishes server-runtime.json.
706+ Effect . forEach ( [ 1_500 , null ] , ( publishDelayMs ) =>
707+ Effect . gen ( function * ( ) {
708+ const fs = yield * FileSystem . FileSystem ;
709+ const path = yield * Path . Path ;
710+ const home = yield * fs . makeTempDirectoryScoped ( { prefix : "t3-ssh-launch-" } ) ;
711+ const serversFile = path . join ( home , "servers" ) ;
712+ yield * Effect . addFinalizer ( ( ) =>
713+ fs . readFileString ( serversFile ) . pipe (
714+ Effect . map ( ( pids ) => {
715+ for ( const pid of pids . split ( "\n" ) . filter ( Boolean ) ) {
716+ try {
717+ process . kill ( Number ( pid ) , "SIGKILL" ) ;
718+ } catch { }
719+ }
720+ } ) ,
721+ Effect . ignore ,
722+ ) ,
723+ ) ;
724+ const serverScript = path . join ( home , "fake-t3.cjs" ) ;
725+ yield * fs . writeFileString ( serverScript , fakeT3Serve ( publishDelayMs ) ) ;
726+ // A launch that died holding the lock must not block or double-admit later ones.
727+ const launchLock = path . join ( home , ".t3" , "ssh-launch" , ".launch.lock" ) ;
728+ yield * fs . makeDirectory ( path . dirname ( launchLock ) , { recursive : true } ) ;
729+ yield * fs . writeFileString ( launchLock , "999999999\n" ) ;
730+ const targets = [
731+ { alias : "devbox" , hostname : "devbox" , username : null , port : null } ,
732+ { alias : "devbox-tailnet" , hostname : "devbox-tailnet" , username : null , port : null } ,
733+ ] ;
730734
731- const launches = yield * Effect . forEach (
732- targets ,
733- ( target ) =>
734- SshTunnel . launchOrReuseRemoteServer ( target , undefined , { nodeScriptPath : serverScript } ) ,
735- { concurrency : "unbounded" } ,
736- ) . pipe ( Effect . provide ( localRemoteShellLayer ( home ) ) ) ;
735+ const launches = yield * Effect . forEach (
736+ targets ,
737+ ( target ) =>
738+ SshTunnel . launchOrReuseRemoteServer ( target , undefined , {
739+ nodeScriptPath : serverScript ,
740+ } ) ,
741+ { concurrency : "unbounded" } ,
742+ ) . pipe ( Effect . provide ( localRemoteShellLayer ( home ) ) ) ;
737743
738- const ownerPid = Number (
739- yield * fs . readFileString ( path . join ( home , ".t3" , "userdata" , "server.lock" ) ) ,
740- ) ;
741- assert . include (
742- launches . map ( ( launch ) => launch . remoteServerKind ) ,
743- "managed" ,
744- ) ;
745- for ( const [ index , launch ] of launches . entries ( ) ) {
746- if ( launch . remoteServerKind !== "managed" ) continue ;
747- const stateKey = yield * remoteStateKey ( targets [ index ] ! ) ;
748- const recordedPid = yield * fs . readFileString (
749- path . join ( home , ".t3" , "ssh-launch" , stateKey , "pid" ) ,
744+ const ownerPid = Number (
745+ yield * fs . readFileString ( path . join ( home , ".t3" , "userdata" , "server.lock" ) ) ,
750746 ) ;
751- assert . equal (
752- Number ( recordedPid . trim ( ) ) ,
753- ownerPid ,
754- `${ targets [ index ] ! . alias } reported a managed server, but its recorded PID is not the server that owns the remote home` ,
747+ assert . sameMembers (
748+ launches . map ( ( launch ) => launch . remoteServerKind ) ,
749+ [ "managed" , "external" ] ,
755750 ) ;
756- }
757- assert . isFalse ( yield * fs . exists ( launchLock ) ) ;
758- } ) . pipe ( Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) , Effect . scoped ) ,
751+ assert . equal ( launches [ 0 ] ! . remotePort , launches [ 1 ] ! . remotePort ) ;
752+ assert . deepEqual ( ( yield * fs . readFileString ( serversFile ) ) . trim ( ) . split ( "\n" ) , [
753+ String ( ownerPid ) ,
754+ ] ) ;
755+ for ( const [ index , launch ] of launches . entries ( ) ) {
756+ if ( launch . remoteServerKind !== "managed" ) continue ;
757+ const stateKey = yield * remoteStateKey ( targets [ index ] ! ) ;
758+ const recordedPid = yield * fs . readFileString (
759+ path . join ( home , ".t3" , "ssh-launch" , stateKey , "pid" ) ,
760+ ) ;
761+ assert . equal (
762+ Number ( recordedPid . trim ( ) ) ,
763+ ownerPid ,
764+ `${ targets [ index ] ! . alias } reported a managed server, but its recorded PID is not the server that owns the remote home` ,
765+ ) ;
766+ }
767+ assert . isFalse ( yield * fs . exists ( launchLock ) ) ;
768+ } ) . pipe ( Effect . scoped ) ,
769+ ) . pipe ( Effect . provide ( Layer . mergeAll ( NodeServices . layer , NodeCrypto . layer ) ) ) ,
759770 ) ;
760771} ) ;
761772
0 commit comments