11import { sha256 } from "@noble/hashes/sha2" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { describe , expect , it } from "@effect/vitest" ;
4+ import * as Clock from "effect/Clock" ;
45import * as ConfigProvider from "effect/ConfigProvider" ;
56import * as Deferred from "effect/Deferred" ;
67import * as Effect from "effect/Effect" ;
@@ -12,6 +13,7 @@ import * as Option from "effect/Option";
1213import * as PlatformError from "effect/PlatformError" ;
1314import * as Sink from "effect/Sink" ;
1415import * as Stream from "effect/Stream" ;
16+ import * as TestClock from "effect/testing/TestClock" ;
1517import { HttpClient , HttpClientResponse } from "effect/http" ;
1618import { ChildProcess , ChildProcessSpawner } from "effect/process" ;
1719import * as HostProcess from "./HostProcess.ts" ;
@@ -157,12 +159,12 @@ describe("RelayClient", () => {
157159 const manager = yield * RelayClient . makeCloudflaredRelayClient ( { baseDir } ) . pipe (
158160 Effect . provideService ( FileSystem . FileSystem , {
159161 ...fileSystem ,
160- writeFileString : ( path , contents , options ) =>
162+ rename : ( from , to ) =>
161163 fileSystem
162- . writeFileString ( path , contents , options )
164+ . rename ( from , to )
163165 . pipe (
164166 Effect . tapError ( ( ) =>
165- path === lockPath ? Deferred . succeed ( contended , undefined ) : Effect . void ,
167+ to === lockPath ? Deferred . succeed ( contended , undefined ) : Effect . void ,
166168 ) ,
167169 ) ,
168170 } ) ,
@@ -184,44 +186,46 @@ describe("RelayClient", () => {
184186 ) ,
185187 ) ;
186188
187- it . effect ( "keeps an install lock whose owner is still running ", ( ) =>
189+ it . effect . each ( [ "live owner" , "recent legacy" ] as const ) ( "keeps a lock with a %s ", ( kind ) =>
188190 Effect . gen ( function * ( ) {
189191 const fileSystem = yield * FileSystem . FileSystem ;
190192 const baseDir = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-cloudflared-test-" } ) ;
191- const directory = `${ baseDir } /tools/cloudflared/${ RelayClient . CLOUDFLARED_VERSION } /linux-x64` ;
192- const lockPath = `${ directory } /cloudflared.lock` ;
193- const token = `${ process . pid } :still-running` ;
194- yield * fileSystem . makeDirectory ( directory , { recursive : true } ) ;
195- yield * fileSystem . writeFileString ( lockPath , token ) ;
196- const contended = yield * Deferred . make < void > ( ) ;
197- const manager = yield * RelayClient . makeCloudflaredRelayClient ( { baseDir } ) . pipe (
198- Effect . provideService ( FileSystem . FileSystem , {
199- ...fileSystem ,
200- writeFileString : ( path , contents , options ) =>
201- fileSystem
202- . writeFileString ( path , contents , options )
203- . pipe (
204- Effect . tapError ( ( ) =>
205- path === lockPath ? Deferred . succeed ( contended , undefined ) : Effect . void ,
206- ) ,
207- ) ,
193+ const lockPath = `${ baseDir } /tools/cloudflared/${ RelayClient . CLOUDFLARED_VERSION } /linux-x64/cloudflared.lock` ;
194+ const owner = `${ process . pid } -still-running` ;
195+ yield * fileSystem . makeDirectory ( lockPath . slice ( 0 , lockPath . lastIndexOf ( "/" ) ) , {
196+ recursive : true ,
197+ } ) ;
198+ if ( kind === "live owner" ) {
199+ yield * fileSystem . makeDirectory ( lockPath ) ;
200+ yield * fileSystem . writeFileString ( `${ lockPath } /${ owner } ` , "" ) ;
201+ } else {
202+ yield * fileSystem . writeFileString ( lockPath , "" ) ;
203+ }
204+ const written = yield * fileSystem . stat ( lockPath ) ;
205+ yield * TestClock . setTime (
206+ Option . getOrThrow ( written . mtime ) . getTime ( ) + ( kind === "live owner" ? 3_600_000 : 6_000 ) ,
207+ ) ;
208+ const waiting = yield * Deferred . make < void > ( ) ;
209+ const clock = yield * Clock . Clock ;
210+ const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
211+ baseDir,
212+ releaseAsset : testReleaseAsset ,
213+ } ) ;
214+ const installing = yield * manager . install . pipe (
215+ Effect . provideService ( Clock . Clock , {
216+ ...clock ,
217+ sleep : ( ) => Deferred . succeed ( waiting , undefined ) . pipe ( Effect . andThen ( Effect . never ) ) ,
208218 } ) ,
219+ Effect . forkChild ,
209220 ) ;
210- const installing = yield * manager . install . pipe ( Effect . forkChild ) ;
211- yield * Deferred . await ( contended ) ;
221+ yield * Deferred . await ( waiting ) ;
212222 yield * Fiber . interrupt ( installing ) ;
213- expect ( yield * fileSystem . readFileString ( lockPath ) ) . toBe ( token ) ;
214- } ) . pipe (
215- Effect . scoped ,
216- Effect . provide (
217- Layer . mergeAll (
218- NodeServices . layer ,
219- layerHttpClient ( new Uint8Array ( ) ) ,
220- layerSpawner ( [ ] ) ,
221- layerHostRuntime ( { PATH : "" } ) ,
222- ) ,
223- ) ,
224- ) ,
223+ if ( kind === "live owner" ) {
224+ expect ( yield * fileSystem . readDirectory ( lockPath ) ) . toEqual ( [ owner ] ) ;
225+ } else {
226+ expect ( yield * fileSystem . readFileString ( lockPath ) ) . toBe ( "" ) ;
227+ }
228+ } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( "linux" ) ) ) ,
225229 ) ;
226230
227231 it . effect . skipIf ( windowsHost ) ( "replaces an install lock left by an exited installer" , ( ) =>
@@ -234,7 +238,8 @@ describe("RelayClient", () => {
234238 yield * fileSystem . makeDirectory ( lockPath . slice ( 0 , lockPath . lastIndexOf ( "/" ) ) , {
235239 recursive : true ,
236240 } ) ;
237- yield * fileSystem . writeFileString ( lockPath , "2147483647:exited-installer" ) ;
241+ yield * fileSystem . makeDirectory ( lockPath ) ;
242+ yield * fileSystem . writeFileString ( `${ lockPath } /2147483647-exited-installer` , "" ) ;
238243 const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
239244 baseDir,
240245 releaseAsset : testReleaseAsset ,
@@ -257,9 +262,8 @@ describe("RelayClient", () => {
257262 } ) ;
258263 yield * fileSystem . writeFileString ( lockPath , "" ) ;
259264 const written = yield * fileSystem . stat ( lockPath ) ;
260- const mtime = Option . getOrElse ( written . mtime , ( ) => new Date ( 0 ) ) . getTime ( ) ;
261- // The test clock starts at the epoch, so a lock just written looks
262- // newer than the clock. Move past its mtime by more than the grace.
265+ const mtime = Option . getOrThrow ( written . mtime ) . getTime ( ) ;
266+ // File mtimes use wall time; advance the test clock past legacy expiry.
263267 yield * TestClock . setTime ( mtime + 60 * 60 * 1_000 ) ;
264268 const manager = yield * RelayClient . makeCloudflaredRelayClient ( {
265269 baseDir,
@@ -271,6 +275,46 @@ describe("RelayClient", () => {
271275 } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( "linux" ) ) ) ,
272276 ) ;
273277
278+ it . effect ( "keeps a replacement lock when a stale contender resumes cleanup" , ( ) =>
279+ Effect . gen ( function * ( ) {
280+ const fileSystem = yield * FileSystem . FileSystem ;
281+ const baseDir = yield * fileSystem . makeTempDirectoryScoped ( { prefix : "t3-cloudflared-test-" } ) ;
282+ const lockPath = `${ baseDir } /tools/cloudflared/${ RelayClient . CLOUDFLARED_VERSION } /linux-x64/cloudflared.lock` ;
283+ const abandoned = "2147483647-exited-installer" ;
284+ const replacement = `${ process . pid } -replacement` ;
285+ yield * fileSystem . makeDirectory ( lockPath , { recursive : true } ) ;
286+ yield * fileSystem . writeFileString ( `${ lockPath } /${ abandoned } ` , "" ) ;
287+ const checked = yield * Deferred . make < void > ( ) ;
288+ let replaced = false ;
289+ const manager = yield * RelayClient . makeCloudflaredRelayClient ( { baseDir } ) . pipe (
290+ Effect . provideService ( FileSystem . FileSystem , {
291+ ...fileSystem ,
292+ readDirectory : ( path ) =>
293+ fileSystem . readDirectory ( path ) . pipe (
294+ Effect . tap ( ( entries ) =>
295+ Effect . gen ( function * ( ) {
296+ if ( path !== lockPath ) return ;
297+ if ( replaced ) {
298+ expect ( entries ) . toEqual ( [ replacement ] ) ;
299+ yield * Deferred . succeed ( checked , undefined ) ;
300+ return ;
301+ }
302+ replaced = true ;
303+ yield * fileSystem . rename ( lockPath , `${ lockPath } .old` ) ;
304+ yield * fileSystem . makeDirectory ( lockPath ) ;
305+ yield * fileSystem . writeFileString ( `${ lockPath } /${ replacement } ` , "" ) ;
306+ } ) ,
307+ ) ,
308+ ) ,
309+ } ) ,
310+ ) ;
311+ const installing = yield * manager . install . pipe ( Effect . forkChild ) ;
312+ yield * Deferred . await ( checked ) ;
313+ yield * Fiber . interrupt ( installing ) ;
314+ expect ( yield * fileSystem . readDirectory ( lockPath ) ) . toEqual ( [ replacement ] ) ;
315+ } ) . pipe ( Effect . scoped , Effect . provide ( layerInstallRuntime ( "linux" ) ) ) ,
316+ ) ;
317+
274318 it . effect ( "releases the install lock when a download is cancelled" , ( ) =>
275319 Effect . gen ( function * ( ) {
276320 const fileSystem = yield * FileSystem . FileSystem ;
@@ -320,12 +364,12 @@ describe("RelayClient", () => {
320364 } ) . pipe (
321365 Effect . provideService ( FileSystem . FileSystem , {
322366 ...fileSystem ,
323- writeFileString : ( path , contents , options ) =>
367+ rename : ( from , to ) =>
324368 fileSystem
325- . writeFileString ( path , contents , options )
369+ . rename ( from , to )
326370 . pipe (
327371 Effect . tap ( ( ) =>
328- path === lockPath && pauseWrite
372+ to === lockPath && pauseWrite
329373 ? Deferred . succeed ( acquired , undefined ) . pipe (
330374 Effect . andThen ( Deferred . await ( completeWrite ) ) ,
331375 )
0 commit comments