@@ -229,9 +229,9 @@ export interface BootServiceManager {
229229 /** Best-effort recovery after a failed repair of an installed service. */
230230 readonly restart : ReadonlyArray < BootServiceStep > ;
231231 /**
232- * Replaces stop + activate when the caller runs inside the service, where a
233- * stop would kill it before the start. The last entry queues the restart
234- * with the service manager instead of waiting for it.
232+ * Replaces stop + activate for `restart` when the caller runs inside the
233+ * service, where a stop would kill it before the start. The last entry
234+ * queues the restart with the service manager instead of waiting for it.
235235 */
236236 readonly handoff ?: ReadonlyArray < BootServiceStep > ;
237237 /** Uninstall, before the unit file is removed. */
@@ -854,13 +854,25 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
854854 // written, from the same read the downgrade check uses; the stop that
855855 // normally serialises against the launcher is skipped on purpose.
856856 const start = options ?. start !== false ;
857- // From inside the service the files are written as for start=false, with
858- // the same guard against a concurrent remote update, and the service
859- // manager restarts onto them.
860- const handoff = installed && start && runningInsideService ? manager . handoff : undefined ;
861- const deferred = installed && ( ! start || handoff !== undefined ) ;
862- const stopped = installed && start && handoff === undefined ;
863- if ( stopped ) {
857+ if ( installed && start && runningInsideService && manager . kind === "systemd" ) {
858+ // Stopping the unit from inside it kills this process before the start.
859+ // A transient unit runs the same install from outside, stop first, so it
860+ // still serialises with a remote update the launcher may be starting.
861+ yield * runStep ( "handing the install to a transient unit" , "systemd-run" , [
862+ "--user" ,
863+ "--collect" ,
864+ "--quiet" ,
865+ `--setenv=${ BOOT_SERVICE_UNIT_ENV } =` ,
866+ runtimePaths . entryPath ,
867+ "service" ,
868+ "install" ,
869+ "--base-dir" ,
870+ input . baseDir ,
871+ ...( options ?. allowDowngrade === true ? [ "--allow-downgrade" ] : [ ] ) ,
872+ ] ) ;
873+ return plan ;
874+ }
875+ if ( installed && start ) {
864876 yield * runSteps ( manager . stop ) ;
865877 }
866878
@@ -889,7 +901,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
889901 yield * fs
890902 . makeDirectory ( path . dirname ( unitPath ) , { recursive : true } )
891903 . pipe ( Effect . mapError ( ( cause ) => new BootServiceInstallError ( { cause } ) ) ) ;
892- if ( deferred ) {
904+ if ( ! start && installed ) {
893905 // Written first: once the files below name the new version, the
894906 // running service is behind them, and a failure between the two
895907 // writes must not leave it looking current. The launcher removes the
@@ -908,7 +920,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
908920 2 ,
909921 ) } \n`,
910922 ) ;
911- if ( deferred ) {
923+ if ( ! start && installed ) {
912924 // The launcher only writes this file while a remote update is in
913925 // flight. One that began after the check above lands either before
914926 // this write (then the launcher's copy in memory is what it keeps
@@ -922,10 +934,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
922934 }
923935 yield * writeDurably ( unitPath , manager . render ( plan ) ) ;
924936
925- if ( handoff !== undefined ) {
926- // The launcher clears the restart marker once it runs this version.
927- yield * runSteps ( handoff ) ;
928- } else if ( start ) {
937+ if ( start ) {
929938 yield * runSteps ( manager . activate ) ;
930939 yield * fs . remove ( restartPendingPath , { force : true } ) ;
931940 }
@@ -934,7 +943,7 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
934943 cause . _tag === "PlatformError" ? new BootServiceInstallError ( { cause } ) : cause ,
935944 ) ,
936945 Effect . tapError ( ( ) =>
937- stopped ? runSteps ( manager . restart ) . pipe ( Effect . ignore ) : Effect . void ,
946+ installed && start ? runSteps ( manager . restart ) . pipe ( Effect . ignore ) : Effect . void ,
938947 ) ,
939948 ) ;
940949 return plan ;
@@ -951,6 +960,8 @@ export const make = Effect.fn("cloud.boot_service.make")(function* (input: {
951960 ) {
952961 return false ;
953962 }
963+ // Writes no state, and the launcher lets an update transition in flight
964+ // finish before the queued stop tears it down.
954965 if ( runningInsideService && manager . handoff !== undefined ) {
955966 yield * runSteps ( manager . handoff ) ;
956967 return true ;
0 commit comments