|
| 1 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
1 | 2 | import { describe, expect, it } from "@effect/vitest"; |
2 | 3 | import * as Deferred from "effect/Deferred"; |
3 | 4 | import * as Duration from "effect/Duration"; |
4 | 5 | import * as Effect from "effect/Effect"; |
5 | 6 | import * as Fiber from "effect/Fiber"; |
| 7 | +import * as FileSystem from "effect/FileSystem"; |
6 | 8 | import * as Layer from "effect/Layer"; |
| 9 | +import * as Path from "effect/Path"; |
7 | 10 | import * as PlatformError from "effect/PlatformError"; |
8 | 11 | import * as Sink from "effect/Sink"; |
9 | 12 | import * as Stream from "effect/Stream"; |
10 | 13 | import { TestClock } from "effect/testing"; |
11 | 14 | import { ChildProcessSpawner } from "effect/process"; |
| 15 | +import * as ChildProcess from "effect/process/ChildProcess"; |
12 | 16 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
13 | 17 | import { SpawnExecutableResolution } from "@t3tools/shared/shell"; |
14 | 18 |
|
15 | 19 | import * as ProcessRunner from "./processRunner.ts"; |
| 20 | +import { writeFakeCli } from "@t3tools/provider-testing/fakeCli"; |
| 21 | +import { makeProcessProbe, processProbeSource } from "./testUtils/processProbe.ts"; |
16 | 22 |
|
17 | 23 | type ChildProcessCommand = { |
18 | 24 | readonly command: string; |
@@ -398,6 +404,124 @@ describe("runProcess", () => { |
398 | 404 | }), |
399 | 405 | ); |
400 | 406 |
|
| 407 | + it.effect("kills a helper that ignores SIGTERM when its run times out", () => |
| 408 | + Effect.gen(function* () { |
| 409 | + const fs = yield* FileSystem.FileSystem; |
| 410 | + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-runner-" }); |
| 411 | + const probe = yield* makeProcessProbe; |
| 412 | + const helper = writeFakeCli({ |
| 413 | + directory, |
| 414 | + name: "stubborn", |
| 415 | + source: `process.on("SIGTERM", () => {}); ${processProbeSource(probe.port)}`, |
| 416 | + }); |
| 417 | + const runner = yield* ProcessRunner.ProcessRunner; |
| 418 | + const fiber = yield* runner |
| 419 | + .run({ command: helper, args: [], timeout: "1 second" }) |
| 420 | + .pipe(Effect.flip, Effect.forkScoped); |
| 421 | + yield* probe.started; |
| 422 | + |
| 423 | + yield* TestClock.adjust("1 second"); |
| 424 | + yield* probe.exited; |
| 425 | + expect((yield* Fiber.join(fiber))._tag).toBe("ProcessTimeoutError"); |
| 426 | + }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))), |
| 427 | + ); |
| 428 | + |
| 429 | + for (const wrapperExit of ["timeout", "success"] as const) { |
| 430 | + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 431 | + `terminates descendants after wrapper ${wrapperExit}`, |
| 432 | + () => |
| 433 | + Effect.gen(function* () { |
| 434 | + const fs = yield* FileSystem.FileSystem; |
| 435 | + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-tree-" }); |
| 436 | + const probe = yield* makeProcessProbe; |
| 437 | + const helper = writeFakeCli({ |
| 438 | + directory, |
| 439 | + name: "wrapper", |
| 440 | + source: ` |
| 441 | + import { spawn } from "node:child_process"; |
| 442 | + process.on("SIGTERM", () => {}); |
| 443 | + const child = spawn(process.execPath, ["--input-type=module", "-e", ${JSON.stringify(` |
| 444 | + ${processProbeSource(probe.port)} |
| 445 | + probe.once("connect", () => process.send("ready")); |
| 446 | + `)}], { stdio: ["ignore", "ignore", "ignore", "ipc"] }); |
| 447 | + ${wrapperExit === "success" ? 'child.once("message", () => process.exit(0));' : 'child.on("exit", () => process.exit(0));'} |
| 448 | + `, |
| 449 | + }); |
| 450 | + const runner = yield* ProcessRunner.ProcessRunner; |
| 451 | + const fiber = yield* runner |
| 452 | + .run({ command: helper, args: [], timeout: "1 second" }) |
| 453 | + .pipe(Effect.result, Effect.forkScoped); |
| 454 | + yield* probe.started; |
| 455 | + |
| 456 | + if (wrapperExit === "timeout") yield* TestClock.adjust("1 second"); |
| 457 | + yield* probe.exited; |
| 458 | + const result = yield* Fiber.join(fiber); |
| 459 | + if (wrapperExit === "timeout") { |
| 460 | + expect(result).toMatchObject({ |
| 461 | + _tag: "Failure", |
| 462 | + failure: { _tag: "ProcessTimeoutError" }, |
| 463 | + }); |
| 464 | + } else { |
| 465 | + expect(result).toMatchObject({ |
| 466 | + _tag: "Success", |
| 467 | + success: { code: 0, timedOut: false }, |
| 468 | + }); |
| 469 | + } |
| 470 | + }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer)))), |
| 471 | + ); |
| 472 | + } |
| 473 | + |
| 474 | + it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")( |
| 475 | + "exits the backend and terminates stubborn helpers on graceful group shutdown", |
| 476 | + () => |
| 477 | + Effect.gen(function* () { |
| 478 | + const fs = yield* FileSystem.FileSystem; |
| 479 | + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-process-backend-" }); |
| 480 | + const probe = yield* makeProcessProbe; |
| 481 | + const helper = writeFakeCli({ |
| 482 | + directory, |
| 483 | + name: "helper", |
| 484 | + source: `process.on("SIGTERM", () => {}); ${processProbeSource(probe.port)}`, |
| 485 | + }); |
| 486 | + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; |
| 487 | + const path = yield* Path.Path; |
| 488 | + const backend = yield* spawner.spawn( |
| 489 | + ChildProcess.make( |
| 490 | + process.execPath, |
| 491 | + [ |
| 492 | + "--experimental-strip-types", |
| 493 | + "--input-type=module", |
| 494 | + "-e", |
| 495 | + ` |
| 496 | + import * as Effect from "effect/Effect"; |
| 497 | + import * as Layer from "effect/Layer"; |
| 498 | + import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 499 | + import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 500 | + import * as ProcessRunner from ${JSON.stringify(new URL("./processRunner.ts", import.meta.url).href)}; |
| 501 | + NodeRuntime.runMain(Effect.gen(function* () { |
| 502 | + const runner = yield* ProcessRunner.ProcessRunner; |
| 503 | + yield* runner.run({ command: ${JSON.stringify(helper)}, args: [] }); |
| 504 | + }).pipe(Effect.provide(ProcessRunner.layer.pipe(Layer.provideMerge(NodeServices.layer))))); |
| 505 | + `, |
| 506 | + ], |
| 507 | + { |
| 508 | + detached: true, |
| 509 | + forceKillAfter: "1 second", |
| 510 | + cwd: yield* path.fromFileUrl(new URL("..", import.meta.url)), |
| 511 | + }, |
| 512 | + ), |
| 513 | + ); |
| 514 | + yield* probe.started; |
| 515 | + |
| 516 | + // Desktop allows five seconds for NodeRuntime to finish scope finalizers. |
| 517 | + process.kill(-backend.pid, "SIGTERM"); |
| 518 | + yield* Effect.all( |
| 519 | + [backend.exitCode.pipe(Effect.timeout("4 seconds"), TestClock.withLive), probe.exited], |
| 520 | + { concurrency: "unbounded" }, |
| 521 | + ); |
| 522 | + }).pipe(Effect.provide(NodeServices.layer)), |
| 523 | + ); |
| 524 | + |
401 | 525 | it.effect("returns a synthetic timed out result when timeoutBehavior is timedOutResult", () => |
402 | 526 | Effect.gen(function* () { |
403 | 527 | const spawner = makeSpawner(() => |
|
0 commit comments