t3-code-android-nightly/patches/@effect__platform-node-shared@4.1.6.patch

245 lines
10 KiB
Diff

diff --git a/dist/NodeChildProcessSpawner.d.ts b/dist/NodeChildProcessSpawner.d.ts
index ac7b044..0ad793f 100644
--- a/dist/NodeChildProcessSpawner.d.ts
+++ b/dist/NodeChildProcessSpawner.d.ts
@@ -35,6 +35,8 @@
* @stability unstable
* @since 4.0.0
*/
+import type * as NodeChildProcess from "node:child_process";
+import type { Readable, Writable } from "node:stream";
import type * as Arr from "effect/Array";
import * as Context from "effect/Context";
import * as Duration from "effect/Duration";
@@ -54,6 +56,19 @@ import { ChildProcessSpawner, ProcessId } from "effect/process/ChildProcessSpawn
* @since 4.0.0
*/
export declare const layer: Layer.Layer<ChildProcessSpawner, never, FileSystem.FileSystem | Path.Path>;
+export interface SpawnedChildProcess extends NodeJS.EventEmitter {
+ readonly pid?: number | undefined;
+ readonly exitCode: number | null;
+ readonly signalCode: NodeJS.Signals | null;
+ readonly stdin: Writable | null;
+ readonly stdout: Readable | null;
+ readonly stderr: Readable | null;
+ readonly stdio: ReadonlyArray<Readable | Writable | null | undefined>;
+ kill(signal?: NodeJS.Signals | number): boolean;
+ ref(): void;
+ unref(): void;
+}
+export declare const layerWithSpawn: (spawnProcess: (command: string, args: ReadonlyArray<string>, options: NodeChildProcess.SpawnOptions) => SpawnedChildProcess) => Layer.Layer<ChildProcessSpawner, never, FileSystem.FileSystem | Path.Path>;
declare const ProxyProcess_base: Context.ServiceClass<ProxyProcess, "@effect/platform-node/NodeChildProcessSpawner/ProxyProcess", {
readonly currentPid: Effect.Effect<Option.Option<ProcessId>>;
}>;
diff --git a/dist/NodeChildProcessSpawner.js b/dist/NodeChildProcessSpawner.js
index 0257098..f166bc3 100644
--- a/dist/NodeChildProcessSpawner.js
+++ b/dist/NodeChildProcessSpawner.js
@@ -88,20 +88,36 @@ const isProcessAlive = (command, childProcess) => {
return isUnsignallableProcessGroup(error);
}
};
-const taskkill = (childProcess, onExit = () => {}) => {
+const taskkill = (childProcess, spawnProcess, onExit = () => {}) => {
if (hasExited(childProcess)) {
onExit(null);
return;
}
- NodeChildProcess.execFile("taskkill", ["/pid", String(childProcess.pid), "/T", "/F"], {
+ if (spawnProcess === undefined) {
+ NodeChildProcess.execFile("taskkill", ["/pid", String(childProcess.pid), "/T", "/F"], {
+ windowsHide: true
+ }, onExit);
+ return;
+ }
+ // Starts taskkill through the injected spawner so it gets the same off-thread launch as commands.
+ let done = false;
+ const finish = error => {
+ if (done) return;
+ done = true;
+ onExit(error);
+ };
+ const killer = spawnProcess("taskkill", ["/pid", String(childProcess.pid), "/T", "/F"], {
+ stdio: "ignore",
windowsHide: true
- }, onExit);
+ });
+ killer.on("error", finish);
+ killer.on("exit", code => finish(code === 0 ? null : new Error(`taskkill exited with code ${code}`)));
};
const spawnDirect = (command, args, options) => NodeChildProcess.spawn(command, args, options);
// Returns the function that launches a command. The proxy process layer waits
// here, interruptibly, for a proxy process that is ready, since the launch
// itself cannot be interrupted.
-const makeWith = /*#__PURE__*/Effect.fnUntraced(function* (prepareSpawn) {
+const makeWith = /*#__PURE__*/Effect.fnUntraced(function* (prepareSpawn, killSpawn) {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const resolveWorkingDirectory = Effect.fnUntraced(function* (options) {
@@ -372,7 +388,7 @@ const makeWith = /*#__PURE__*/Effect.fnUntraced(function* (prepareSpawn) {
const killProcessGroup = (command, childProcess, signal) => {
if (globalThis.process.platform === "win32") {
return Effect.callback(resume => {
- taskkill(childProcess, error => {
+ taskkill(childProcess, killSpawn, error => {
if (error) {
resume(Effect.fail(toPlatformError("kill", toError(error), command)));
} else {
@@ -813,6 +829,7 @@ export class ProxyProcess extends /*#__PURE__*/Context.Service()("@effect/platfo
* @category layers
* @since 4.1.6
*/
+export const layerWithSpawn = spawnProcess => Layer.effect(ChildProcessSpawner, makeWith(Effect.succeed(spawnProcess), spawnProcess));
export const layerProxyProcess = options => Layer.effectContext(Effect.gen(function* () {
if (globalThis.process.platform === "win32") {
return yield* Effect.fail(PlatformError.systemError({
diff --git a/dist/internal/proxyProcess.js b/dist/internal/proxyProcess.js
index edd291c..5b14884 100644
--- a/dist/internal/proxyProcess.js
+++ b/dist/internal/proxyProcess.js
@@ -164,6 +164,7 @@ class Proxy {
child;
// Bounds the wait for `Ready`, and for the reply to each sent request
timers = /*#__PURE__*/new Map();
+ heard = 0;
constructor(options) {
this.options = options;
try {
@@ -295,21 +296,31 @@ class Proxy {
}
startTimer(key) {
const millis = this.options.responseTimeoutMillis;
- const timer = setTimeout(() => {
- this.stop(Object.assign(new Error(`The proxy process did not respond within ${millis} ms`), {
- code: "ETIMEDOUT"
- }));
- }, millis);
- // Whatever waits for the reply keeps the application running
- timer.unref();
- this.timers.set(key, timer);
+ // Effect-TS/effect#11008: a blocked event loop runs due timers before it reads waiting replies.
+ const arm = heard => {
+ const timer = setTimeout(() => setImmediate(() => {
+ if (this.timers.get(key) !== timer) return;
+ if (this.heard !== heard) return arm(this.heard);
+ this.stop(Object.assign(new Error(`The proxy process did not respond within ${millis} ms`), {
+ code: "ETIMEDOUT"
+ }));
+ }), millis);
+ // Whatever waits for the reply keeps the application running
+ timer.unref();
+ this.timers.set(key, timer);
+ };
+ arm(this.heard);
}
stopTimer(key) {
clearTimeout(this.timers.get(key));
this.timers.delete(key);
}
disconnect() {
- if (this.child?.connected === true) this.child.disconnect();
+ if (this.child?.connected === true) {
+ // Tells the proxy process this is a deliberate close, not the application's death
+ if (this.ready) this.child.send({ _tag: "Close" });
+ this.child.disconnect();
+ }
this.closeServer();
}
updateRef() {
@@ -322,6 +333,7 @@ class Proxy {
}
}
receive(message) {
+ this.heard++;
if (message._tag === "Ready") return this.onReady();
const command = this.commands.get(message.id);
if (command === undefined) return;
diff --git a/dist/internal/proxyProcessSource.js b/dist/internal/proxyProcessSource.js
index 3cd0dab..3d11435 100644
--- a/dist/internal/proxyProcessSource.js
+++ b/dist/internal/proxyProcessSource.js
@@ -54,6 +54,8 @@ export const run = () => {
socket.write(header, (error) => error ? reject(error) : resolve(socket))
})
})
+ // Each spawned command, with the process or group that kills it
+ const running = new Map()
const spawn = async (socketPath , token , { args, command, id, options } ) => {
const stdio = await Promise.allSettled(
options.stdio.map((mode, fd) => mode === "pipe" ? connect(socketPath, token, id, fd) : mode)
@@ -71,6 +73,8 @@ export const run = () => {
if (errno) throw new Error(\`setBlocking failed: \${Util.getSystemErrorName(errno)}\`)
}
const child = ChildProcess.spawn(command, args, { ...options, stdio: values })
+ child.on("spawn", () => running.set(child, options.detached ? -child.pid : child.pid))
+ child.on("exit", () => running.delete(child))
child.on("error", (error) => send({ _tag: "Error", id, error: serializeError(error) }))
child.on("spawn", () => send({ _tag: "Spawned", id, pid: child.pid }))
child.on("exit", (code, signal) => send({ _tag: "Exit", id, code, signal }))
@@ -85,8 +89,19 @@ export const run = () => {
// the proxy process exits when its commands have exited.
process.on("SIGINT", () => {})
process.on("SIGTERM", () => {})
+ // An application that was killed never stops its commands, so they die with its channel.
+ // A deliberate close leaves them, so an unreferenced command can outlive the application.
+ let closing = false
+ process.on("disconnect", () => {
+ if (closing) return
+ for (const target of running.values()) {
+ try {
+ process.kill(target, "SIGKILL")
+ } catch {}
+ }
+ })
process.once("message", ({ socketPath, token } ) => {
- process.on("message", (request ) => spawn(socketPath, token, request))
+ process.on("message", (request ) => request._tag === "Close" ? closing = true : spawn(socketPath, token, request))
send({ _tag: "Ready" })
})
}
diff --git a/dist/internal/proxyProcessWorker.js b/dist/internal/proxyProcessWorker.js
index 6694313..92dd0a1 100644
--- a/dist/internal/proxyProcessWorker.js
+++ b/dist/internal/proxyProcessWorker.js
@@ -49,6 +49,8 @@ export const run = () => {
socket.write(header, error => error ? reject(error) : resolve(socket));
});
});
+ // Each spawned command, with the process or group that kills it
+ const running = new Map();
const spawn = async (socketPath, token, {
args,
command,
@@ -72,6 +74,8 @@ export const run = () => {
...options,
stdio: values
});
+ child.on("spawn", () => running.set(child, options.detached ? -child.pid : child.pid));
+ child.on("exit", () => running.delete(child));
child.on("error", error => send({
_tag: "Error",
id,
@@ -103,11 +107,22 @@ export const run = () => {
// the proxy process exits when its commands have exited.
process.on("SIGINT", () => {});
process.on("SIGTERM", () => {});
+ // An application that was killed never stops its commands, so they die with its channel.
+ // A deliberate close leaves them, so an unreferenced command can outlive the application.
+ let closing = false;
+ process.on("disconnect", () => {
+ if (closing) return;
+ for (const target of running.values()) {
+ try {
+ process.kill(target, "SIGKILL");
+ } catch {}
+ }
+ });
process.once("message", ({
socketPath,
token
}) => {
- process.on("message", request => spawn(socketPath, token, request));
+ process.on("message", request => request._tag === "Close" ? closing = true : spawn(socketPath, token, request));
send({
_tag: "Ready"
});