@@ -125,6 +125,14 @@ export interface WindowsBootServicePlan extends BootServicePlan {
125125export interface WindowsPreflight {
126126 readonly shellRunning : boolean ;
127127 readonly entryDisabled : boolean ;
128+ /** Absent when no shortcut exists yet. */
129+ readonly shortcutTarget ?: string ;
130+ readonly shortcutArguments ?: string ;
131+ }
132+
133+ /** Windows only. What the shortcut must point at for the service to work. */
134+ export function expectedShortcutArguments ( plan : WindowsBootServicePlan ) : string {
135+ return `-NoLogo -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "${ plan . startupScriptPath } "` ;
128136}
129137
130138/**
@@ -197,9 +205,7 @@ export function renderShortcutScript(plan: WindowsBootServicePlan): string {
197205 "$shell = New-Object -ComObject WScript.Shell" ,
198206 "$shortcut = $shell.CreateShortcut($shortcutPath)" ,
199207 "$shortcut.TargetPath = Join-Path $env:SystemRoot 'System32\\WindowsPowerShell\\v1.0\\powershell.exe'" ,
200- `$shortcut.Arguments = ${ quotePowerShellLiteral (
201- `-NoLogo -NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File "${ plan . startupScriptPath } "` ,
202- ) } `,
208+ `$shortcut.Arguments = ${ quotePowerShellLiteral ( expectedShortcutArguments ( plan ) ) } ` ,
203209 "$shortcut.WorkingDirectory = $env:USERPROFILE" ,
204210 `$shortcut.Description = ${ quotePowerShellLiteral ( `${ SHORTCUT_NAME } , started at sign-in` ) } ` ,
205211 "# Minimized, because PowerShell paints a console before it hides itself." ,
@@ -222,11 +228,18 @@ export function parseProbeOutput(stdout: string): WindowsPreflight | undefined {
222228 const match = new RegExp ( `^${ key } =(True|False)\\s*$` , "im" ) . exec ( stdout ) ?. [ 1 ] ;
223229 return match === undefined ? undefined : match === "True" ;
224230 } ;
231+ const readText = ( key : string ) => new RegExp ( `^${ key } =(.*)$` , "im" ) . exec ( stdout ) ?. [ 1 ] ?. trim ( ) ;
225232 const shellRunning = read ( "shell" ) ;
226233 const entryDisabled = read ( "disabled" ) ;
227- return shellRunning === undefined || entryDisabled === undefined
228- ? undefined
229- : { shellRunning, entryDisabled } ;
234+ if ( shellRunning === undefined || entryDisabled === undefined ) return undefined ;
235+ const shortcutTarget = readText ( "target" ) ;
236+ const shortcutArguments = readText ( "arguments" ) ;
237+ return {
238+ shellRunning,
239+ entryDisabled,
240+ ...( shortcutTarget === undefined ? { } : { shortcutTarget } ) ,
241+ ...( shortcutArguments === undefined ? { } : { shortcutArguments } ) ,
242+ } ;
230243}
231244
232245export interface WindowsBootServiceInput {
@@ -409,29 +422,28 @@ export const make = Effect.fn("cloud.boot_service_windows.make")(function* (
409422 */
410423 const requestStop = Effect . gen ( function * ( ) {
411424 const recordedPid = yield * fs . readFileString ( pidPath ) . pipe ( Effect . option ) ;
412- if ( Option . isNone ( recordedPid ) ) return ;
425+ if ( Option . isNone ( recordedPid ) ) return false ;
413426 const pid = Number . parseInt ( recordedPid . value . trim ( ) , 10 ) ;
414427 if ( ! Number . isInteger ( pid ) || pid <= 0 || ! ( yield * processIsAlive ( pid ) ) ) {
415428 // The launcher died without cleaning up. Nothing to wait for.
416429 yield * fs . remove ( pidPath , { force : true } ) . pipe ( Effect . ignore ) ;
417- return ;
430+ return false ;
418431 }
419432
420- // A missing runtime directory must not block an uninstall: the shortcut is
421- // the thing the user asked to remove, and it lives elsewhere.
422- const requested = yield * fs . writeFileString ( stopRequestPath , "" ) . pipe (
423- Effect . as ( true ) ,
424- Effect . orElseSucceed ( ( ) => false ) ,
425- ) ;
426- if ( ! requested ) return ;
433+ // A launcher is confirmed alive, so a failed write must fail the whole
434+ // operation. Swallowing it would let the caller carry on and start a second
435+ // server against the same database.
436+ yield * fs
437+ . writeFileString ( stopRequestPath , "" )
438+ . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
427439
428440 const timeoutMs = Duration . toMillis ( input . stopRequestTimeout ?? STOP_REQUEST_TIMEOUT ) ;
429441 const pollMs = Math . min ( timeoutMs , Duration . toMillis ( STOP_REQUEST_ACK_POLL ) ) ;
430442 const attempts = Math . max ( 1 , Math . ceil ( timeoutMs / pollMs ) ) ;
431443 for ( let attempt = 0 ; attempt < attempts ; attempt += 1 ) {
432444 yield * Effect . sleep ( Duration . millis ( pollMs ) ) ;
433445 const stillRunning = yield * fs . exists ( pidPath ) . pipe ( Effect . orElseSucceed ( ( ) => true ) ) ;
434- if ( ! stillRunning ) return ;
446+ if ( ! stillRunning ) return true ;
435447 }
436448 // Refusing here is the whole point. Carrying on would write over a launcher
437449 // we could not confirm dead.
@@ -543,19 +555,18 @@ export const make = Effect.fn("cloud.boot_service_windows.make")(function* (
543555 const installed = yield * fs
544556 . exists ( unitPath )
545557 . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
546- if ( installed ) {
547- yield * requestStop ;
548- }
558+ // Keyed on the pid file, never on the shortcut. A launcher outlives a
559+ // manually deleted shortcut, and starting a second one alongside it would
560+ // put two servers on the same database.
561+ const stopped = yield * requestStop ;
549562
550563 yield * Effect . gen ( function * ( ) {
551- if ( installed ) {
552- const previousStateText = yield * fs . readFileString ( statePath ) . pipe ( Effect . option ) ;
553- if (
554- Option . isSome ( previousStateText ) &&
555- serviceStateHasPendingUpdate ( previousStateText . value )
556- ) {
557- return yield * new BootServiceUpdatePendingError ( ) ;
558- }
564+ const previousStateText = yield * fs . readFileString ( statePath ) . pipe ( Effect . option ) ;
565+ if (
566+ Option . isSome ( previousStateText ) &&
567+ serviceStateHasPendingUpdate ( previousStateText . value )
568+ ) {
569+ return yield * new BootServiceUpdatePendingError ( ) ;
559570 }
560571 yield * writeDurably ( launcherPath , launcherSource ) ;
561572 yield * writeDurably (
@@ -577,7 +588,7 @@ export const make = Effect.fn("cloud.boot_service_windows.make")(function* (
577588 yield * runPowerShellScript ( "starting the service" , startupScriptPath ) ;
578589 } ) . pipe (
579590 Effect . tapError ( ( ) =>
580- installed
591+ installed || stopped
581592 ? runPowerShellScript (
582593 "restarting the service after a failed update" ,
583594 startupScriptPath ,
@@ -590,14 +601,16 @@ export const make = Effect.fn("cloud.boot_service_windows.make")(function* (
590601
591602 const uninstall : BootService [ "Service" ] [ "uninstall" ] = Effect . gen ( function * ( ) {
592603 yield * requireWindows ;
593- if (
594- ! ( yield * fs
595- . exists ( unitPath )
596- . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) )
597- )
598- return false ;
604+ const installed = yield * fs
605+ . exists ( unitPath )
606+ . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
607+
608+ // Stop first, and decide that on the pid file rather than the shortcut. A
609+ // launcher survives someone deleting the shortcut by hand, and leaving it
610+ // running is exactly what the user asked us not to do.
611+ const stopped = yield * requestStop ;
612+ if ( ! installed && ! stopped ) return false ;
599613
600- yield * requestStop ;
601614 // A shortcut is an ordinary file, so removing it needs no PowerShell. That
602615 // also means a missing or broken generated script cannot strand the entry.
603616 yield * fs
@@ -632,13 +645,26 @@ export const make = Effect.fn("cloud.boot_service_windows.make")(function* (
632645 fs . readFileString ( statePath ) . pipe ( Effect . option ) ,
633646 ] ) ;
634647 const state = Option . isSome ( stateText ) ? parseServiceState ( stateText . value ) : undefined ;
648+ // Read the shortcut itself back, because the script that wrote it matching
649+ // proves nothing about the .lnk someone may have edited or replaced since.
650+ const shortcut = yield * runShortcutScript (
651+ "checking the Windows Startup shortcut" ,
652+ "Probe" ,
653+ ) . pipe (
654+ Effect . map ( ( probe ) => parseProbeOutput ( probe . stdout ) ) ,
655+ Effect . orElseSucceed ( ( ) => undefined ) ,
656+ ) ;
657+ const shortcutMatches =
658+ shortcut ?. shortcutTarget === powershell &&
659+ shortcut . shortcutArguments === expectedShortcutArguments ( plan ) ;
635660 // Duplicated from the Linux status. The runtime and state checks are the
636661 // same, but the Linux copy also compares the systemd unit, so it cannot be
637662 // shared without changing the Linux path.
638663 return {
639664 supported : true ,
640665 installed : true ,
641666 current :
667+ shortcutMatches &&
642668 Option . isSome ( startupScript ) &&
643669 startupScript . value === renderStartupScript ( plan ) &&
644670 // The shortcut script embeds the shortcut path, the interpreter and the
0 commit comments