TypeScript
fix(desktop): force-close stalled Linux MIME cache helpers (#25210)
mariaefb940dbaf97
diff --git a/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts b/apps/desktop/src/app/DesktopLinuxUrlHandler.test.tsindex feedbbd5800..9404b0ac308 100644--- a/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts+++ b/apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts@@ -1,7 +1,11 @@import * as NodeServices from "@effect/platform-node/NodeServices";import { assert, describe, it } from "@effect/vitest";+import * as HostProcess from "@t3tools/shared/HostProcess";+import * as Cause from "effect/Cause";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";+import * as Exit from "effect/Exit";+import * as Fiber from "effect/Fiber";import * as FileSystem from "effect/FileSystem";import * as Layer from "effect/Layer";import * as Option from "effect/Option";@@ -9,6 +13,8 @@ import * as Path from "effect/Path";import * as PlatformError from "effect/PlatformError";import * as Sink from "effect/Sink";import * as Stream from "effect/Stream";+import * as TestClock from "effect/testing/TestClock";+import * as ChildProcess from "effect/process/ChildProcess";import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";import * as DesktopEnvironment from "./DesktopEnvironment.ts";@@ -56,6 +62,7 @@ const layerHandler = (recorded: RecordedRegistration,input: {readonly environment?: Record<string, unknown>;+ readonly processSpawner?: ChildProcessSpawner.ChildProcessSpawner["Service"];readonly updateDesktopDatabaseExitCode?: number;readonly updateDesktopDatabaseStalled?: boolean;readonly updateDesktopDatabaseStarted?: Deferred.Deferred<void>;@@ -106,6 +113,7 @@ const layerHandler = (Layer.succeed(ChildProcessSpawner.ChildProcessSpawner,ChildProcessSpawner.make((command) => {+ if (input.processSpawner) return input.processSpawner.spawn(command);const childProcess = command as unknown as {readonly command: string;readonly args: ReadonlyArray<string>;@@ -358,6 +366,69 @@ describe("DesktopLinuxUrlHandler", () => {assert.deepEqual(writeFailed.commands, []);});});++ it.effect.skipIf(HostProcess.Platform.defaultValue() !== "linux")(+ "continues registration after a timed-out cache helper ignores SIGTERM",+ () =>+ Effect.gen(function* () {+ const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;+ const nodeExecutable = yield* HostProcess.ExecutablePath;+ const recorded = emptyRecording();+ const spawned = yield* Deferred.make<ChildProcessSpawner.ChildProcessHandle>();+ const ready = yield* Deferred.make<void>();+ const processSpawner = ChildProcessSpawner.make((command) =>+ Effect.gen(function* () {+ if (!ChildProcess.isStandardCommand(command)) {+ return yield* Effect.die("expected a single registration command");+ }+ recorded.commands.push({ command: command.command, args: command.args });+ if (command.command !== "update-desktop-database") return mockProcess(0);+ const child = yield* realSpawner.spawn(+ ChildProcess.make(+ nodeExecutable,+ [+ "-e",+ // The watchdog makes the unfixed case exit with a distinguishable code.+ "process.on('SIGTERM', () => setTimeout(() => process.exit(99), 5000));" ++ "process.stdout.write('ready\\n'); setInterval(() => {}, 2147483647);",+ ],+ { ...command.options, stdout: "pipe" },+ ),+ );+ yield* Deferred.succeed(spawned, child);+ yield* Stream.runHead(child.stdout);+ yield* Deferred.succeed(ready, undefined);+ return child;+ }),+ );+ const registration = yield* runRegister(recorded, { processSpawner }).pipe(+ Effect.forkScoped,+ );+ const child = yield* Deferred.await(spawned);+ yield* Effect.addFinalizer(() =>+ child.isRunning.pipe(+ Effect.flatMap((running) =>+ running ? child.kill({ killSignal: "SIGKILL" }) : Effect.void,+ ),+ Effect.ignore,+ ),+ );+ yield* Deferred.await(ready);+ yield* TestClock.adjust("5 seconds");+ yield* Fiber.join(registration);++ const exit = yield* Effect.exit(child.exitCode);+ if (Exit.isFailure(exit)) {+ assert.include(Cause.pretty(exit.cause), "SIGKILL");+ } else {+ assert.fail(`cache helper exited with ${exit.value} instead of being force-killed`);+ }+ assert.deepEqual(+ recorded.commands.map(({ command }) => command),+ ["update-desktop-database", "xdg-mime"],+ );+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );});interface ThemeIconRun {diff --git a/apps/desktop/src/app/DesktopLinuxUrlHandler.ts b/apps/desktop/src/app/DesktopLinuxUrlHandler.tsindex dd38acbc389..ebd7d71b257 100644--- a/apps/desktop/src/app/DesktopLinuxUrlHandler.ts+++ b/apps/desktop/src/app/DesktopLinuxUrlHandler.ts@@ -174,6 +174,7 @@ export const make = Effect.gen(function* () {stdin: "ignore",stdout: "ignore",stderr: "ignore",+ forceKillAfter: "1 second",},);const handle = yield* spawner.spawn(command);