@smoothbricks/nx-plugin 0.4.25 → 0.4.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1078 -98
- package/dist/bin/smoo-nx-bound-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-bound-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-bound-failures.js +31 -0
- package/dist/bin/smoo-nx-cargo-hash.js +28 -3
- package/dist/bin/smoo-nx-exec.js +16 -85
- package/dist/bin/smoo-nx-failures.d.ts +3 -0
- package/dist/bin/smoo-nx-failures.d.ts.map +1 -0
- package/dist/bin/smoo-nx-failures.js +3 -0
- package/dist/bin/smoo-nx-nextest-extract.js +3 -2
- package/dist/bin/smoo-nx-test-walks.d.ts +3 -0
- package/dist/bin/smoo-nx-test-walks.d.ts.map +1 -0
- package/dist/bin/smoo-nx-test-walks.js +34 -0
- package/dist/bounded-test-policy.d.ts +7 -1
- package/dist/bounded-test-policy.d.ts.map +1 -1
- package/dist/bounded-test-policy.js +101 -40
- package/dist/build-state-output-policy.d.ts +25 -0
- package/dist/build-state-output-policy.d.ts.map +1 -0
- package/dist/build-state-output-policy.js +64 -0
- package/dist/cargo-closure-input.d.ts +16 -17
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +37 -44
- package/dist/cargo-dev-profile.d.ts +37 -0
- package/dist/cargo-dev-profile.d.ts.map +1 -0
- package/dist/cargo-dev-profile.js +108 -0
- package/dist/cargo-source-hash.d.ts +50 -8
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +691 -71
- package/dist/cargo-toolchain-policy.d.ts +4 -0
- package/dist/cargo-toolchain-policy.d.ts.map +1 -1
- package/dist/cargo-toolchain-policy.js +5 -0
- package/dist/cargo-workspace.d.ts +108 -12
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +299 -18
- package/dist/cross-check-policy.d.ts +21 -3
- package/dist/cross-check-policy.d.ts.map +1 -1
- package/dist/cross-check-policy.js +24 -6
- package/dist/disk-command-policy.d.ts +7 -0
- package/dist/disk-command-policy.d.ts.map +1 -0
- package/dist/disk-command-policy.js +98 -0
- package/dist/ensure-built-receipt.d.ts +25 -0
- package/dist/ensure-built-receipt.d.ts.map +1 -0
- package/dist/ensure-built-receipt.js +261 -0
- package/dist/ensure-built-witness.d.ts +57 -0
- package/dist/ensure-built-witness.d.ts.map +1 -0
- package/dist/ensure-built-witness.js +412 -0
- package/dist/ensure-built.d.ts +213 -12
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +624 -177
- package/dist/eslint-file-set.d.ts +7 -0
- package/dist/eslint-file-set.d.ts.map +1 -0
- package/dist/eslint-file-set.js +12 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts +63 -0
- package/dist/executors/bounded-exec/cpu-pool.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-pool.js +379 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts +82 -0
- package/dist/executors/bounded-exec/cpu-tokens.d.ts.map +1 -0
- package/dist/executors/bounded-exec/cpu-tokens.js +114 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts +61 -0
- package/dist/executors/bounded-exec/disk-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/disk-lease.js +91 -0
- package/dist/executors/bounded-exec/executor.d.ts +45 -2
- package/dist/executors/bounded-exec/executor.d.ts.map +1 -1
- package/dist/executors/bounded-exec/executor.js +447 -52
- package/dist/executors/bounded-exec/gateway-lease.d.ts +52 -0
- package/dist/executors/bounded-exec/gateway-lease.d.ts.map +1 -0
- package/dist/executors/bounded-exec/gateway-lease.js +143 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts +35 -0
- package/dist/executors/bounded-exec/hang-diagnostics.d.ts.map +1 -0
- package/dist/executors/bounded-exec/hang-diagnostics.js +231 -0
- package/dist/executors/bounded-exec/progress.d.ts +35 -0
- package/dist/executors/bounded-exec/progress.d.ts.map +1 -0
- package/dist/executors/bounded-exec/progress.js +79 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts +3 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-fixtures.js +12 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts +91 -0
- package/dist/executors/bounded-exec/ram-temp-held.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp-held.js +152 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts +223 -0
- package/dist/executors/bounded-exec/ram-temp.d.ts.map +1 -0
- package/dist/executors/bounded-exec/ram-temp.js +1528 -0
- package/dist/executors/bounded-exec/schema.d.ts +29 -11
- package/dist/executors/bounded-exec/schema.d.ts.map +1 -1
- package/dist/executors/bounded-exec/verdict.d.ts +162 -0
- package/dist/executors/bounded-exec/verdict.d.ts.map +1 -0
- package/dist/executors/bounded-exec/verdict.js +318 -0
- package/dist/executors/shard-timings/executor.d.ts +14 -0
- package/dist/executors/shard-timings/executor.d.ts.map +1 -0
- package/dist/executors/shard-timings/executor.js +25 -0
- package/dist/executors/shard-timings/schema.d.ts +4 -0
- package/dist/executors/shard-timings/schema.d.ts.map +1 -0
- package/dist/executors/shard-timings/schema.js +1 -0
- package/dist/failure-summary.d.ts +5 -0
- package/dist/failure-summary.d.ts.map +1 -0
- package/dist/failure-summary.js +118 -0
- package/dist/generators/create-package/generator.d.ts.map +1 -1
- package/dist/generators/create-package/generator.js +6 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +435 -230
- package/dist/managed-assets.d.ts +6 -0
- package/dist/managed-assets.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.d.ts +13 -0
- package/dist/managed-files/ci-workflow.d.ts.map +1 -1
- package/dist/managed-files/ci-workflow.js +44 -13
- package/dist/managed-files/files.d.ts +7 -1
- package/dist/managed-files/files.d.ts.map +1 -1
- package/dist/managed-files/files.js +62 -8
- package/dist/managed-files/json-edit.d.ts +21 -0
- package/dist/managed-files/json-edit.d.ts.map +1 -0
- package/dist/managed-files/json-edit.js +295 -0
- package/dist/managed-files/managed-format.d.ts +33 -1
- package/dist/managed-files/managed-format.d.ts.map +1 -1
- package/dist/managed-files/managed-format.js +82 -2
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts +7 -0
- package/dist/managed-files/pr-preview-cleanup-workflow.d.ts.map +1 -1
- package/dist/managed-files/pr-preview-cleanup-workflow.js +68 -4
- package/dist/managed-files/publish-workflow.d.ts.map +1 -1
- package/dist/managed-files/publish-workflow.js +231 -48
- package/dist/managed-files/tree.d.ts +5 -0
- package/dist/managed-files/tree.d.ts.map +1 -1
- package/dist/managed-files/tree.js +5 -3
- package/dist/manifest-hash.js +3 -2
- package/dist/nextest-code-bound.d.ts +62 -0
- package/dist/nextest-code-bound.d.ts.map +1 -0
- package/dist/nextest-code-bound.js +154 -0
- package/dist/nextest-extraction.d.ts +24 -8
- package/dist/nextest-extraction.d.ts.map +1 -1
- package/dist/nextest-extraction.js +42 -7
- package/dist/package-target-policy.d.ts.map +1 -1
- package/dist/package-target-policy.js +3 -5
- package/dist/release-config-policy.d.ts.map +1 -1
- package/dist/release-config-policy.js +3 -5
- package/dist/runtime-input.d.ts +14 -0
- package/dist/runtime-input.d.ts.map +1 -0
- package/dist/runtime-input.js +15 -0
- package/dist/rust-tokens.d.ts +11 -0
- package/dist/rust-tokens.d.ts.map +1 -0
- package/dist/rust-tokens.js +81 -0
- package/dist/task-results.d.ts +6 -0
- package/dist/task-results.d.ts.map +1 -0
- package/dist/task-results.js +61 -0
- package/dist/test-program.d.ts +54 -0
- package/dist/test-program.d.ts.map +1 -0
- package/dist/test-program.js +292 -0
- package/dist/test-timings.d.ts +71 -0
- package/dist/test-timings.d.ts.map +1 -0
- package/dist/test-timings.js +393 -0
- package/dist/test-walk-policy.d.ts +103 -0
- package/dist/test-walk-policy.d.ts.map +1 -0
- package/dist/test-walk-policy.js +657 -0
- package/dist/testing.d.ts +177 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +824 -0
- package/dist/type-guards.d.ts +3 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +4 -0
- package/dist/typecheck-test-policy.d.ts +8 -5
- package/dist/typecheck-test-policy.d.ts.map +1 -1
- package/dist/typecheck-test-policy.js +113 -100
- package/dist/workspace-config-policy.d.ts +8 -0
- package/dist/workspace-config-policy.d.ts.map +1 -1
- package/dist/workspace-config-policy.js +175 -63
- package/dist/workspace-index.d.ts +12 -0
- package/dist/workspace-index.d.ts.map +1 -0
- package/dist/workspace-index.js +17 -0
- package/dist/workspace-manifest.d.ts +37 -1
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/executors.json +5 -0
- package/loader.js +13 -11
- package/managed/raw/envrc +33 -62
- package/managed/raw/git-format-staged.yml +30 -35
- package/managed/raw/patches/nx@23.2.1.patch +3484 -0
- package/managed/raw/tooling/direnv/apple-developer.sh +83 -0
- package/managed/raw/tooling/direnv/developer-links.ts +607 -17
- package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -92
- package/managed/raw/tooling/direnv/github-actions-bootstrap.sh +20 -11
- package/managed/raw/tooling/direnv/linked-snapshots.ts +845 -0
- package/managed/raw/tooling/direnv/nx-socket-dir.sh +82 -0
- package/managed/raw/tooling/direnv/repo-path +14 -2
- package/managed/raw/tooling/direnv/setup-environment.ts +1009 -118
- package/managed/raw/tooling/direnv/toolchain-stamp.ts +45 -3
- package/managed/raw/tooling/direnv/ttsc-env.sh +39 -0
- package/managed/raw/tooling/git-hooks/commit-msg.sh +5 -6
- package/managed/raw/tooling/git-hooks/pre-commit.sh +7 -6
- package/managed/raw/tooling/git-hooks/pre-push.sh +20 -15
- package/managed/templates/github/actions/cache-nix-devenv/action.yml +6 -6
- package/managed/templates/github/actions/cache-node-modules/action.yml +6 -4
- package/managed/templates/github/actions/cache-nx/action.yml +1 -1
- package/managed/templates/github/actions/cache-ttsc-plugins/action.yml +1 -1
- package/managed/templates/github/actions/forgejo-artifact.cjs +99 -0
- package/managed/templates/github/actions/forgejo-download-artifact/action.yml +49 -0
- package/managed/templates/github/actions/forgejo-download-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/action.yml +65 -0
- package/managed/templates/github/actions/forgejo-upload-artifact/index.cjs +7 -0
- package/managed/templates/github/actions/setup-devenv/action.yml +21 -3
- package/managed/templates/github/workflows/managed-files.yml +1 -2
- package/nextest.toml +8 -75
- package/package.json +75 -13
- package/runtime-input.sh +81 -0
- package/src/__tests__/counted-cargo-process.ts +286 -0
- package/src/__tests__/counted-cargo.ts +730 -0
- package/src/__tests__/fixture-cargo-home.ts +36 -0
- package/src/__tests__/fixture-nx-env.ts +157 -2
- package/src/__tests__/progress-clock.ts +43 -0
- package/src/__tests__/runtime-input.ts +23 -0
- package/src/bin/smoo-nx-bound-failures.test.ts +127 -0
- package/src/bin/smoo-nx-bound-failures.ts +33 -0
- package/src/bin/smoo-nx-cargo-hash.ts +28 -3
- package/src/bin/smoo-nx-exec.ts +17 -86
- package/src/bin/smoo-nx-failures.ts +4 -0
- package/src/bin/smoo-nx-nextest-extract.ts +3 -2
- package/src/bin/smoo-nx-test-walks.ts +42 -0
- package/src/bounded-test-policy.test.ts +268 -22
- package/src/bounded-test-policy.ts +114 -42
- package/src/build-state-output-policy.test.ts +115 -0
- package/src/build-state-output-policy.ts +90 -0
- package/src/bun-bootstrap.test.ts +57 -3
- package/src/bun-file-packages.test.ts +47 -0
- package/src/cargo-closure-input.test.ts +432 -62
- package/src/cargo-closure-input.ts +51 -57
- package/src/cargo-dev-profile.ts +137 -0
- package/src/cargo-resolve-cache.test.ts +1044 -0
- package/src/cargo-source-hash.test.ts +203 -93
- package/src/cargo-source-hash.ts +762 -75
- package/src/cargo-toolchain-policy.ts +6 -0
- package/src/cargo-workspace.test.ts +320 -0
- package/src/cargo-workspace.ts +344 -18
- package/src/cross-check-policy.test.ts +59 -1
- package/src/cross-check-policy.ts +25 -6
- package/src/disk-command-policy.test.ts +61 -0
- package/src/disk-command-policy.ts +107 -0
- package/src/ensure-built-receipt.test.ts +184 -0
- package/src/ensure-built-receipt.ts +263 -0
- package/src/ensure-built-witness.test.ts +344 -0
- package/src/ensure-built-witness.ts +252 -0
- package/src/ensure-built.test.ts +1043 -483
- package/src/ensure-built.ts +870 -197
- package/src/eslint-file-set.ts +9 -0
- package/src/executors/bounded-exec/cpu-pool.test.ts +454 -0
- package/src/executors/bounded-exec/cpu-pool.ts +417 -0
- package/src/executors/bounded-exec/cpu-tokens.test.ts +269 -0
- package/src/executors/bounded-exec/cpu-tokens.ts +177 -0
- package/src/executors/bounded-exec/disk-lease.test.ts +185 -0
- package/src/executors/bounded-exec/disk-lease.ts +139 -0
- package/src/executors/bounded-exec/executor-host.test.ts +778 -0
- package/src/executors/bounded-exec/executor.test.ts +1116 -63
- package/src/executors/bounded-exec/executor.ts +565 -67
- package/src/executors/bounded-exec/gateway-lease.ts +176 -0
- package/src/executors/bounded-exec/hang-diagnostics.test.ts +285 -0
- package/src/executors/bounded-exec/hang-diagnostics.ts +283 -0
- package/src/executors/bounded-exec/progress.test.ts +79 -0
- package/src/executors/bounded-exec/progress.ts +118 -0
- package/src/executors/bounded-exec/ram-temp-fixtures.ts +12 -0
- package/src/executors/bounded-exec/ram-temp-held.test.ts +264 -0
- package/src/executors/bounded-exec/ram-temp-held.ts +242 -0
- package/src/executors/bounded-exec/ram-temp-host.test.ts +605 -0
- package/src/executors/bounded-exec/ram-temp.test.ts +674 -0
- package/src/executors/bounded-exec/ram-temp.ts +1918 -0
- package/src/executors/bounded-exec/schema.json +11 -5
- package/src/executors/bounded-exec/schema.ts +27 -11
- package/src/executors/bounded-exec/verdict.test.ts +440 -0
- package/src/executors/bounded-exec/verdict.ts +433 -0
- package/src/executors/shard-timings/executor.ts +36 -0
- package/src/executors/shard-timings/schema.json +21 -0
- package/src/executors/shard-timings/schema.ts +4 -0
- package/src/failure-summary.ts +119 -0
- package/src/generators/bounded-test-targets/generator.test.ts +4 -4
- package/src/generators/bun-test-tracing/generator.test.ts +2 -2
- package/src/generators/create-package/generator.test.ts +6 -0
- package/src/generators/create-package/generator.ts +6 -2
- package/src/host-controller-wiring.test.ts +31 -0
- package/src/index.test.ts +1070 -360
- package/src/index.ts +524 -255
- package/src/linked-snapshots.nx.test.ts +172 -0
- package/src/managed-assets.ts +12 -0
- package/src/managed-files/ci-workflow.ts +54 -13
- package/src/managed-files/files.ts +65 -9
- package/src/managed-files/forgejo-artifact.test.ts +395 -0
- package/src/managed-files/generator.test.ts +60 -1
- package/src/managed-files/json-edit.test.ts +216 -0
- package/src/managed-files/json-edit.ts +290 -0
- package/src/managed-files/managed-format-json.test.ts +308 -0
- package/src/managed-files/managed-format.ts +90 -2
- package/src/managed-files/pr-preview-cleanup-workflow.ts +69 -3
- package/src/managed-files/publish-workflow.ts +285 -67
- package/src/managed-files/repo-path.test.ts +129 -0
- package/src/managed-files/trace-db-artifact.test.ts +7 -0
- package/src/managed-files/tree.ts +11 -3
- package/src/manifest-hash.ts +3 -2
- package/src/nextest-code-bound.test.ts +155 -0
- package/src/nextest-code-bound.ts +195 -0
- package/src/nextest-extraction.test.ts +37 -5
- package/src/nextest-extraction.ts +51 -9
- package/src/nextest-partitions.test.ts +495 -0
- package/src/nx-cache-bound.test.ts +109 -0
- package/src/nx-cache-restore-mtime.test.ts +170 -0
- package/src/nx-daemon-claim.test.ts +277 -0
- package/src/nx-daemon-idle-cpu.test.ts +81 -0
- package/src/nx-daemon-idle-policy.test.ts +51 -0
- package/src/nx-daemon-ignore-files.test.ts +210 -0
- package/src/nx-daemon-root-normalization.test.ts +105 -0
- package/src/nx-daemon-validity.test.ts +925 -0
- package/src/nx-dot-input-policy.test.ts +353 -0
- package/src/nx-failure-life-cycles.test.ts +207 -0
- package/src/nx-files-archive-racy.test.ts +97 -0
- package/src/nx-foreign-workspace-env.test.ts +329 -0
- package/src/nx-incomplete-run-summary.test.ts +70 -0
- package/src/nx-plugin-load-errors.test.ts +120 -0
- package/src/nx-project-resolution-cache.test.ts +176 -0
- package/src/nx-raw-inputs.test.ts +166 -0
- package/src/nx-resident-graph-workers.test.ts +112 -0
- package/src/nx-run-information.test.ts +118 -0
- package/src/nx-skipped-task-exit.test.ts +180 -0
- package/src/nx-state-location.test.ts +63 -0
- package/src/nx-store-resolution.test.ts +99 -0
- package/src/nx-task-graph-cycles.test.ts +148 -0
- package/src/nx-task-output-lease.test.ts +414 -0
- package/src/nx-use-local.test.ts +103 -0
- package/src/package-target-policy.ts +3 -6
- package/src/release-config-policy.ts +3 -6
- package/src/runtime-input.test.ts +215 -0
- package/src/runtime-input.ts +17 -0
- package/src/rust-tokens.ts +78 -0
- package/src/serial-group-wiring.test.ts +326 -0
- package/src/task-history-namespace.test.ts +181 -0
- package/src/task-results.test.ts +226 -0
- package/src/task-results.ts +75 -0
- package/src/test-program.test.ts +303 -0
- package/src/test-program.ts +341 -0
- package/src/test-timings.test.ts +240 -0
- package/src/test-timings.ts +311 -0
- package/src/test-walk-policy.test.ts +585 -0
- package/src/test-walk-policy.ts +731 -0
- package/src/testing.test.ts +509 -0
- package/src/testing.ts +922 -0
- package/src/type-guards.ts +4 -0
- package/src/typecheck-test-policy.test.ts +496 -11
- package/src/typecheck-test-policy.ts +137 -103
- package/src/workspace-config-policy.test.ts +212 -13
- package/src/workspace-config-policy.ts +101 -73
- package/src/workspace-index.ts +18 -0
- package/src/workspace-manifest.ts +41 -1
- package/test-timings.json +85 -28
- package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -615
- package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
- package/src/__tests__/in-process-nx-index.ts +0 -9
|
@@ -1,23 +1,56 @@
|
|
|
1
1
|
import { afterEach, describe, expect, it } from 'bun:test';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { spawnSync } from 'node:child_process';
|
|
3
|
+
import { access, mkdir, mkdtemp, open, readFile, rm, writeFile } from 'node:fs/promises';
|
|
4
|
+
import { createServer } from 'node:net';
|
|
5
|
+
import { availableParallelism, tmpdir } from 'node:os';
|
|
4
6
|
import { dirname, join } from 'node:path';
|
|
5
|
-
|
|
7
|
+
import { onProgressClock } from '../../__tests__/progress-clock.js';
|
|
8
|
+
import { ownedSocketDirectory } from '../../testing.js';
|
|
9
|
+
import { CPU_POOL_ENV, type CpuPool, createCpuPool, type InheritedCpuPool, readInheritedCpuPool } from './cpu-pool.js';
|
|
10
|
+
import { CPU_TOKENS_ENV, type CpuBudget, parseInheritedTokens, type TokenGrant } from './cpu-tokens.js';
|
|
6
11
|
import {
|
|
7
12
|
type BoundedExecContext,
|
|
8
13
|
createProcessTreeKiller,
|
|
9
14
|
type ProcessTreeKiller,
|
|
10
15
|
runBoundedExec,
|
|
16
|
+
type TempVolume,
|
|
11
17
|
} from './executor.js';
|
|
18
|
+
import type { HangDiagnostics } from './hang-diagnostics.js';
|
|
19
|
+
import type { RamTempAcquisition, RamTempError, RamTempLease, Result } from './ram-temp.js';
|
|
20
|
+
import { VERDICT_FILE } from './verdict.js';
|
|
12
21
|
|
|
13
22
|
const workspaces: string[] = [];
|
|
23
|
+
const ownedProcessGroups = new Set<number>();
|
|
14
24
|
const originalStdoutWrite = process.stdout.write;
|
|
15
25
|
const originalStderrWrite = process.stderr.write;
|
|
16
26
|
|
|
27
|
+
const nextestWant = Math.min(8, parseInheritedTokens(process.env.NEXTEST_TEST_THREADS) ?? 8);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Prints the budget variables it sees, then what a grandchild sees: a command's environment reaches its descendants,
|
|
31
|
+
* so a budget that leaked past its grant shows on both lines.
|
|
32
|
+
*/
|
|
33
|
+
const BUDGET_PROBE = `const { execFileSync } = require('node:child_process');
|
|
34
|
+
const read = 'JSON.stringify([process.env.${CPU_POOL_ENV} ?? null, process.env.${CPU_TOKENS_ENV} ?? null])';
|
|
35
|
+
console.log(JSON.stringify([process.env.${CPU_POOL_ENV} ?? null, process.env.${CPU_TOKENS_ENV} ?? null]));
|
|
36
|
+
console.log(execFileSync('node', ['-p', read]).toString().trim());
|
|
37
|
+
`;
|
|
38
|
+
|
|
17
39
|
describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
18
40
|
afterEach(async () => {
|
|
19
41
|
process.stdout.write = originalStdoutWrite;
|
|
20
42
|
process.stderr.write = originalStderrWrite;
|
|
43
|
+
for (const pid of ownedProcessGroups) {
|
|
44
|
+
try {
|
|
45
|
+
process.kill(-pid, 'SIGKILL');
|
|
46
|
+
} catch (cause) {
|
|
47
|
+
if (!(cause instanceof Error && 'code' in cause && cause.code === 'ESRCH')) {
|
|
48
|
+
throw new Error(`cleanup owned readiness process group pgid=${pid} failed`, { cause });
|
|
49
|
+
}
|
|
50
|
+
} finally {
|
|
51
|
+
ownedProcessGroups.delete(pid);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
21
54
|
await Promise.all(workspaces.splice(0).map((workspace) => rm(workspace, { recursive: true, force: true })));
|
|
22
55
|
});
|
|
23
56
|
|
|
@@ -37,6 +70,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
37
70
|
},
|
|
38
71
|
workspace.context,
|
|
39
72
|
createProcessTreeKiller(),
|
|
73
|
+
null,
|
|
74
|
+
null,
|
|
40
75
|
);
|
|
41
76
|
|
|
42
77
|
expect(result.success).toBe(true);
|
|
@@ -51,6 +86,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
51
86
|
{ command: 'node -e "process.exit(7)"', timeoutMs: 5_000 },
|
|
52
87
|
workspace.context,
|
|
53
88
|
createProcessTreeKiller(),
|
|
89
|
+
null,
|
|
90
|
+
null,
|
|
54
91
|
);
|
|
55
92
|
|
|
56
93
|
expect(result.success).toBe(false);
|
|
@@ -68,6 +105,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
68
105
|
{ command: "node -e \"console.log('out-value'); console.error('err-value')\"", timeoutMs: 5_000 },
|
|
69
106
|
workspace.context,
|
|
70
107
|
createProcessTreeKiller(),
|
|
108
|
+
null,
|
|
109
|
+
null,
|
|
71
110
|
);
|
|
72
111
|
|
|
73
112
|
expect(result.success).toBe(true);
|
|
@@ -84,6 +123,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
84
123
|
{ command: 'node -e "setTimeout(() => {}, 5000)"', timeoutMs: 50, killAfterMs: 0 },
|
|
85
124
|
workspace.context,
|
|
86
125
|
createProcessTreeKiller(),
|
|
126
|
+
null,
|
|
127
|
+
null,
|
|
87
128
|
);
|
|
88
129
|
|
|
89
130
|
expect(result.success).toBe(false);
|
|
@@ -92,75 +133,247 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
92
133
|
expect(result.terminalOutput).toContain(`cwd=${workspace.root}`);
|
|
93
134
|
});
|
|
94
135
|
|
|
95
|
-
it('kills a silent command on the progress bound
|
|
136
|
+
it('kills a silent command on the progress bound, with no ceiling at all', async () => {
|
|
96
137
|
const workspace = await createWorkspace();
|
|
97
138
|
|
|
98
139
|
const result = await runBoundedExec(
|
|
99
|
-
{ command: 'node -e "setTimeout(() => {}, 30000)"',
|
|
140
|
+
{ command: 'node -e "setTimeout(() => {}, 30000)"', idleTimeoutMs: 100, killAfterMs: 0 },
|
|
100
141
|
workspace.context,
|
|
101
142
|
createProcessTreeKiller(),
|
|
143
|
+
null,
|
|
144
|
+
null,
|
|
102
145
|
);
|
|
103
146
|
|
|
104
147
|
expect(result.success).toBe(false);
|
|
105
|
-
expect(result.terminalOutput).toContain('Command made no progress: no
|
|
148
|
+
expect(result.terminalOutput).toContain('Command made no progress: no test finished for 100ms');
|
|
106
149
|
expect(result.terminalOutput).toContain('idleTimeoutMs=100');
|
|
107
|
-
|
|
150
|
+
expect(result.terminalOutput).toContain('the runner named no test still running');
|
|
151
|
+
// The bound that fired is named, so a hang is never reported as a slow run.
|
|
108
152
|
expect(result.terminalOutput).not.toContain('Command timed out after');
|
|
109
153
|
});
|
|
110
154
|
|
|
111
|
-
it('
|
|
155
|
+
it('kills a runner that keeps warning of a slow test but finishes none, and names the test', async () => {
|
|
112
156
|
const workspace = await createWorkspace();
|
|
113
|
-
//
|
|
114
|
-
// idleTimeoutMs, so passing proves the bound measures silence, not duration.
|
|
157
|
+
// nextest's own lines: one test finishes, then the other only ever says SLOW, every 50ms.
|
|
115
158
|
await workspace.write(
|
|
116
|
-
'
|
|
159
|
+
'slow-runner.js',
|
|
117
160
|
[
|
|
118
|
-
'
|
|
119
|
-
'
|
|
120
|
-
|
|
121
|
-
'
|
|
122
|
-
|
|
161
|
+
"console.error(' PASS [ 0.010s] (1/2) fake tests::quick');",
|
|
162
|
+
'let periods = 0;',
|
|
163
|
+
"setInterval(() => console.error(' SLOW [> ' + ++periods * 0.05 + 's] (───) fake tests::hangs'), 50);",
|
|
164
|
+
'',
|
|
165
|
+
].join('\n'),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
// The window ends only once the runner has said SLOW, however long node takes to start.
|
|
169
|
+
const { result } = await onProgressClock(
|
|
170
|
+
(chunk) => chunk.includes('SLOW [>'),
|
|
171
|
+
400,
|
|
172
|
+
() =>
|
|
173
|
+
runBoundedExec(
|
|
174
|
+
{ command: 'node slow-runner.js', idleTimeoutMs: 400, killAfterMs: 0 },
|
|
175
|
+
workspace.context,
|
|
176
|
+
createProcessTreeKiller(),
|
|
177
|
+
null,
|
|
178
|
+
null,
|
|
179
|
+
),
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
expect(result.success).toBe(false);
|
|
183
|
+
expect(result.terminalOutput).toContain('SLOW [>');
|
|
184
|
+
expect(result.terminalOutput).toContain('Command made no progress: no test finished for 400ms (idleTimeoutMs=400)');
|
|
185
|
+
expect(result.terminalOutput).toContain('still running: fake tests::hangs (cwd=');
|
|
186
|
+
expect(result.terminalOutput).not.toContain('fake tests::quick (cwd=');
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it('emits a completed pre-expiry snapshot or an explicit incomplete capture without waiting for it', async () => {
|
|
190
|
+
const workspace = await createWorkspace();
|
|
191
|
+
// Child output drives the fake executor clock; the heartbeat period is not a timing oracle.
|
|
192
|
+
await workspace.write('snapshot-clock.js', "setInterval(() => console.log('snapshot-clock'), 20);\n");
|
|
193
|
+
for (const completes of [true, false]) {
|
|
194
|
+
let pid: number | undefined;
|
|
195
|
+
let signal: AbortSignal | undefined;
|
|
196
|
+
const { result } = await onProgressClock(
|
|
197
|
+
(chunk) => chunk.includes('snapshot-clock\n'),
|
|
198
|
+
5_000,
|
|
199
|
+
() =>
|
|
200
|
+
runBoundedExec(
|
|
201
|
+
{ command: 'node snapshot-clock.js', idleTimeoutMs: 10_000, killAfterMs: 0 },
|
|
202
|
+
workspace.context,
|
|
203
|
+
createProcessTreeKiller(),
|
|
204
|
+
null,
|
|
205
|
+
null,
|
|
206
|
+
null,
|
|
207
|
+
async (request) => {
|
|
208
|
+
pid = request.pid;
|
|
209
|
+
signal = request.signal;
|
|
210
|
+
if (!completes) return new Promise<HangDiagnostics>(() => {});
|
|
211
|
+
const capturedAtMs = Date.now();
|
|
212
|
+
return {
|
|
213
|
+
startedAtMs: capturedAtMs,
|
|
214
|
+
capturedAtMs,
|
|
215
|
+
offsetFromExpiryMs: request.expiresAtMs - capturedAtMs,
|
|
216
|
+
outcome: 'holders',
|
|
217
|
+
text: 'owned pre-signal process observation\n',
|
|
218
|
+
};
|
|
219
|
+
},
|
|
220
|
+
),
|
|
221
|
+
);
|
|
222
|
+
expect(result.success).toBe(false);
|
|
223
|
+
expect(pid).toBeGreaterThan(0);
|
|
224
|
+
expect(signal?.aborted).toBe(true);
|
|
225
|
+
expect(result.terminalOutput).toContain(`Hang diagnostics: runner pid=${pid}`);
|
|
226
|
+
if (completes) {
|
|
227
|
+
expect(result.terminalOutput).toContain('outcome=holders capturedAtMs=');
|
|
228
|
+
expect(result.terminalOutput).toContain('actualExpiryOffsetMs=5000');
|
|
229
|
+
expect(result.terminalOutput).toContain('owned pre-signal process observation');
|
|
230
|
+
} else {
|
|
231
|
+
expect(result.terminalOutput).toContain('capture incomplete at expiry');
|
|
232
|
+
expect(result.terminalOutput).toContain('no empty-holder conclusion');
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('discards the previous snapshot when a test finishes and captures the next idle interval', async () => {
|
|
238
|
+
const workspace = await createWorkspace();
|
|
239
|
+
// Only observed child output advances the fake clock, never elapsed host time.
|
|
240
|
+
await workspace.write(
|
|
241
|
+
'snapshot-progress.js',
|
|
242
|
+
[
|
|
243
|
+
"const { existsSync } = require('node:fs');",
|
|
244
|
+
'let progressed = false;',
|
|
245
|
+
'setInterval(() => {',
|
|
246
|
+
" if (!progressed && existsSync('progress')) {",
|
|
247
|
+
" console.log(' PASS [ 0.010s] (1/1) fake tests::progress');",
|
|
248
|
+
' progressed = true;',
|
|
123
249
|
' }',
|
|
250
|
+
" console.log('snapshot-clock');",
|
|
251
|
+
'}, 20);',
|
|
252
|
+
'',
|
|
253
|
+
].join('\n'),
|
|
254
|
+
);
|
|
255
|
+
const signals: Array<AbortSignal | undefined> = [];
|
|
256
|
+
const { result } = await onProgressClock(
|
|
257
|
+
(chunk) => chunk.includes('snapshot-clock\n'),
|
|
258
|
+
5_000,
|
|
259
|
+
() =>
|
|
260
|
+
runBoundedExec(
|
|
261
|
+
{ command: 'node snapshot-progress.js', idleTimeoutMs: 10_000, killAfterMs: 0 },
|
|
262
|
+
workspace.context,
|
|
263
|
+
createProcessTreeKiller(),
|
|
264
|
+
null,
|
|
265
|
+
null,
|
|
266
|
+
null,
|
|
267
|
+
async (request) => {
|
|
268
|
+
signals.push(request.signal);
|
|
269
|
+
const ordinal = signals.length;
|
|
270
|
+
if (ordinal === 1) await workspace.write('progress', 'go');
|
|
271
|
+
const capturedAtMs = Date.now();
|
|
272
|
+
return {
|
|
273
|
+
startedAtMs: capturedAtMs,
|
|
274
|
+
capturedAtMs,
|
|
275
|
+
offsetFromExpiryMs: request.expiresAtMs - capturedAtMs,
|
|
276
|
+
outcome: 'holders',
|
|
277
|
+
text: `capture-interval-${ordinal}\n`,
|
|
278
|
+
};
|
|
279
|
+
},
|
|
280
|
+
),
|
|
281
|
+
);
|
|
282
|
+
expect(result.success).toBe(false);
|
|
283
|
+
expect(signals).toHaveLength(2);
|
|
284
|
+
expect(signals.every((signal) => signal?.aborted)).toBe(true);
|
|
285
|
+
expect(result.terminalOutput).not.toContain('capture-interval-1');
|
|
286
|
+
expect(result.terminalOutput).toContain('capture-interval-2');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('kills a command whose only output is a heartbeat', async () => {
|
|
290
|
+
const workspace = await createWorkspace();
|
|
291
|
+
await workspace.write('heartbeat.js', ["setInterval(() => console.log('still waiting...'), 20);", ''].join('\n'));
|
|
292
|
+
|
|
293
|
+
const { result } = await onProgressClock(
|
|
294
|
+
(chunk) => chunk.includes('still waiting...'),
|
|
295
|
+
300,
|
|
296
|
+
() =>
|
|
297
|
+
runBoundedExec(
|
|
298
|
+
{ command: 'node heartbeat.js', idleTimeoutMs: 300, killAfterMs: 0 },
|
|
299
|
+
workspace.context,
|
|
300
|
+
createProcessTreeKiller(),
|
|
301
|
+
null,
|
|
302
|
+
null,
|
|
303
|
+
),
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
expect(result.success).toBe(false);
|
|
307
|
+
expect(result.terminalOutput).toContain('still waiting...');
|
|
308
|
+
expect(result.terminalOutput).toContain('Command made no progress: no test finished for 300ms');
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it('lets a run that keeps finishing tests outlive its progress bound', async () => {
|
|
312
|
+
const workspace = await createWorkspace();
|
|
313
|
+
// Ten tests under a 400ms progress bound and no ceiling, the clock moving 300ms at each: the run outlives the
|
|
314
|
+
// bound, so passing proves the bound measures the gap between finished tests, not duration.
|
|
315
|
+
await workspace.write(
|
|
316
|
+
'progressing.js',
|
|
317
|
+
[
|
|
318
|
+
'let done = 0;',
|
|
319
|
+
'const timer = setInterval(() => {',
|
|
320
|
+
' done += 1;',
|
|
321
|
+
" console.error(' PASS [ 0.120s] (' + done + '/10) fake tests::t' + done);",
|
|
322
|
+
' if (done === 10) clearInterval(timer);',
|
|
124
323
|
'}, 120);',
|
|
125
324
|
'',
|
|
126
325
|
].join('\n'),
|
|
127
326
|
);
|
|
128
327
|
|
|
129
|
-
const result = await
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
328
|
+
const { result, movedMs } = await onProgressClock(
|
|
329
|
+
(chunk) => chunk.includes('PASS ['),
|
|
330
|
+
300,
|
|
331
|
+
() =>
|
|
332
|
+
runBoundedExec(
|
|
333
|
+
{ command: 'node progressing.js', idleTimeoutMs: 400 },
|
|
334
|
+
workspace.context,
|
|
335
|
+
createProcessTreeKiller(),
|
|
336
|
+
null,
|
|
337
|
+
null,
|
|
338
|
+
),
|
|
133
339
|
);
|
|
134
340
|
|
|
135
341
|
expect(result.success).toBe(true);
|
|
136
|
-
expect(result.terminalOutput).toContain('
|
|
342
|
+
expect(result.terminalOutput).toContain('(10/10) fake tests::t10');
|
|
137
343
|
expect(result.terminalOutput).not.toContain('made no progress');
|
|
344
|
+
expect(movedMs).toBeGreaterThan(400);
|
|
138
345
|
});
|
|
139
346
|
|
|
140
|
-
it('
|
|
347
|
+
it("counts bun's dots on stderr as finished tests", async () => {
|
|
141
348
|
const workspace = await createWorkspace();
|
|
349
|
+
// bun --dots writes one dot per finished test and no newline until another stream writes.
|
|
142
350
|
await workspace.write(
|
|
143
|
-
'
|
|
351
|
+
'dots.js',
|
|
144
352
|
[
|
|
145
|
-
'let
|
|
353
|
+
'let done = 0;',
|
|
146
354
|
'const timer = setInterval(() => {',
|
|
147
|
-
|
|
148
|
-
' if (
|
|
149
|
-
' clearInterval(timer);',
|
|
150
|
-
' }',
|
|
355
|
+
" process.stderr.write('.');",
|
|
356
|
+
' if (++done === 8) { clearInterval(timer); process.stderr.write("\\n"); }',
|
|
151
357
|
'}, 120);',
|
|
152
358
|
'',
|
|
153
359
|
].join('\n'),
|
|
154
360
|
);
|
|
155
361
|
|
|
156
|
-
const result = await
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
362
|
+
const { result } = await onProgressClock(
|
|
363
|
+
(chunk) => /^\.+$/.test(chunk),
|
|
364
|
+
300,
|
|
365
|
+
() =>
|
|
366
|
+
runBoundedExec(
|
|
367
|
+
{ command: 'node dots.js', idleTimeoutMs: 400 },
|
|
368
|
+
workspace.context,
|
|
369
|
+
createProcessTreeKiller(),
|
|
370
|
+
null,
|
|
371
|
+
null,
|
|
372
|
+
),
|
|
160
373
|
);
|
|
161
374
|
|
|
162
375
|
expect(result.success).toBe(true);
|
|
163
|
-
expect(result.terminalOutput).toContain('
|
|
376
|
+
expect(result.terminalOutput).toContain('........');
|
|
164
377
|
});
|
|
165
378
|
|
|
166
379
|
it('applies the absolute ceiling to a command that keeps talking forever', async () => {
|
|
@@ -171,6 +384,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
171
384
|
{ command: 'node runaway.js', timeoutMs: 300, idleTimeoutMs: 30_000, killAfterMs: 0 },
|
|
172
385
|
workspace.context,
|
|
173
386
|
createProcessTreeKiller(),
|
|
387
|
+
null,
|
|
388
|
+
null,
|
|
174
389
|
);
|
|
175
390
|
|
|
176
391
|
expect(result.success).toBe(false);
|
|
@@ -186,6 +401,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
186
401
|
{ command: 'node -e "setTimeout(() => console.log(\'late\'), 400)"', timeoutMs: 30_000 },
|
|
187
402
|
workspace.context,
|
|
188
403
|
createProcessTreeKiller(),
|
|
404
|
+
null,
|
|
405
|
+
null,
|
|
189
406
|
);
|
|
190
407
|
|
|
191
408
|
expect(result.success).toBe(true);
|
|
@@ -198,27 +415,42 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
198
415
|
}
|
|
199
416
|
|
|
200
417
|
const workspace = await createWorkspace();
|
|
201
|
-
const
|
|
202
|
-
const
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
418
|
+
const ready = join(workspace.root, 'cooperative-ready.fifo');
|
|
419
|
+
const handled = join(workspace.root, 'handled.txt');
|
|
420
|
+
expect(spawnSync('mkfifo', [ready]).status).toBe(0);
|
|
421
|
+
await workspace.write(
|
|
422
|
+
'cooperative.js',
|
|
423
|
+
[
|
|
424
|
+
"const fs = require('node:fs');",
|
|
425
|
+
"process.on('SIGTERM', () => {",
|
|
426
|
+
" fs.writeFileSync('handled.txt', 'handled pid=' + process.pid + '\\n');",
|
|
427
|
+
' process.exit(0);',
|
|
428
|
+
'});',
|
|
429
|
+
"fs.writeFileSync('cooperative-ready.fifo', 'ready pid=' + process.pid + '\\n');",
|
|
430
|
+
'setTimeout(() => {}, 5000);',
|
|
431
|
+
'',
|
|
432
|
+
].join('\n'),
|
|
433
|
+
);
|
|
434
|
+
let handlerPid: number | undefined;
|
|
435
|
+
const termination = armedKiller('cooperative SIGTERM handler', async (pid) => {
|
|
436
|
+
handlerPid = await armedPid(ready, pid, 'cooperative SIGTERM handler');
|
|
437
|
+
expect(handlerPid, `handler readiness must name the command leader pid=${pid}`).toBe(pid);
|
|
438
|
+
});
|
|
208
439
|
|
|
209
440
|
const result = await runBoundedExec(
|
|
210
|
-
{
|
|
211
|
-
command: 'node -e "process.on(\'SIGTERM\', () => process.exit(0)); setTimeout(() => {}, 5000)"',
|
|
212
|
-
timeoutMs: 50,
|
|
213
|
-
killAfterMs: 500,
|
|
214
|
-
},
|
|
441
|
+
{ command: 'exec node cooperative.js', timeoutMs: 50, killAfterMs: 500 },
|
|
215
442
|
workspace.context,
|
|
216
|
-
killer,
|
|
443
|
+
termination.killer,
|
|
444
|
+
null,
|
|
445
|
+
null,
|
|
217
446
|
);
|
|
218
447
|
|
|
448
|
+
await termination.armed();
|
|
219
449
|
expect(result.success).toBe(false);
|
|
220
|
-
expect(
|
|
221
|
-
|
|
450
|
+
expect(await readFile(handled, 'utf8'), `cooperative handler pid=${handlerPid} must handle SIGTERM`).toBe(
|
|
451
|
+
`handled pid=${handlerPid}\n`,
|
|
452
|
+
);
|
|
453
|
+
expect(termination.calls).toEqual(['SIGTERM']);
|
|
222
454
|
});
|
|
223
455
|
|
|
224
456
|
it('force-kills after killAfterMs when graceful termination is ignored on POSIX', async () => {
|
|
@@ -227,30 +459,26 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
227
459
|
}
|
|
228
460
|
|
|
229
461
|
const workspace = await createWorkspace();
|
|
462
|
+
const ignoring = ignoresTerm(workspace.root);
|
|
230
463
|
const calls: string[] = [];
|
|
231
464
|
const killer: ProcessTreeKiller = {
|
|
232
465
|
async kill(pid, signal) {
|
|
466
|
+
if (signal === 'SIGTERM') await ignoring.armed();
|
|
233
467
|
calls.push(signal);
|
|
234
468
|
process.kill(-pid, signal);
|
|
235
469
|
},
|
|
236
470
|
};
|
|
237
471
|
|
|
238
|
-
// `trap "" TERM` is a shell builtin, so the ignore disposition is in place
|
|
239
|
-
// within a few ms of `sh` starting and is inherited by `sleep`. The previous
|
|
240
|
-
// `node -e "process.on('SIGTERM', ...)"` needed p50 27.8ms just to reach
|
|
241
|
-
// handler-installed, against a 50ms timeout — 1.8x margin on an idle
|
|
242
|
-
// 18-core host, and none at all on a loaded or 3-core runner. When SIGTERM
|
|
243
|
-
// arrives first the child dies on the default disposition, the run settles,
|
|
244
|
-
// and the SIGKILL branch never executes, so `calls` is ['SIGTERM'] and the
|
|
245
|
-
// assertion below fails for a reason that has nothing to do with escalation.
|
|
246
472
|
const result = await runBoundedExec(
|
|
247
473
|
{
|
|
248
|
-
command:
|
|
249
|
-
timeoutMs:
|
|
474
|
+
command: ignoring.command,
|
|
475
|
+
timeoutMs: 50,
|
|
250
476
|
killAfterMs: 50,
|
|
251
477
|
},
|
|
252
478
|
workspace.context,
|
|
253
479
|
killer,
|
|
480
|
+
null,
|
|
481
|
+
null,
|
|
254
482
|
);
|
|
255
483
|
|
|
256
484
|
expect(result.success).toBe(false);
|
|
@@ -275,6 +503,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
275
503
|
{ command: 'node -e "setTimeout(() => {}, 30000)"', timeoutMs: 50, killAfterMs: 10 },
|
|
276
504
|
workspace.context,
|
|
277
505
|
killer,
|
|
506
|
+
null,
|
|
507
|
+
null,
|
|
278
508
|
);
|
|
279
509
|
expect(Date.now() - started).toBeLessThan(5_000);
|
|
280
510
|
expect(result.success).toBe(false);
|
|
@@ -305,6 +535,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
305
535
|
},
|
|
306
536
|
workspace.context,
|
|
307
537
|
createProcessTreeKiller(),
|
|
538
|
+
null,
|
|
539
|
+
null,
|
|
308
540
|
);
|
|
309
541
|
const suppressed = await runBoundedExec(
|
|
310
542
|
{
|
|
@@ -316,6 +548,8 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
316
548
|
},
|
|
317
549
|
workspace.context,
|
|
318
550
|
createProcessTreeKiller(),
|
|
551
|
+
null,
|
|
552
|
+
null,
|
|
319
553
|
);
|
|
320
554
|
|
|
321
555
|
expect(forwarded.success).toBe(true);
|
|
@@ -332,27 +566,698 @@ describe('@smoothbricks/nx-plugin:bounded-exec', () => {
|
|
|
332
566
|
|
|
333
567
|
const workspace = await createWorkspace();
|
|
334
568
|
const marker = join(workspace.root, 'marker.txt');
|
|
569
|
+
const ready = join(workspace.root, 'descendant-ready.fifo');
|
|
570
|
+
const lifetime = join(workspace.root, 'descendant-lifetime.fifo');
|
|
571
|
+
expect(spawnSync('mkfifo', [ready, lifetime]).status).toBe(0);
|
|
572
|
+
await workspace.write(
|
|
573
|
+
'group-child.js',
|
|
574
|
+
[
|
|
575
|
+
"const fs = require('node:fs');",
|
|
576
|
+
"fs.openSync('descendant-lifetime.fifo', 'w');",
|
|
577
|
+
"fs.writeFileSync('descendant-ready.fifo', 'ready pid=' + process.pid + '\\n');",
|
|
578
|
+
"setTimeout(() => fs.writeFileSync(process.argv[2], 'alive'), 700);",
|
|
579
|
+
'',
|
|
580
|
+
].join('\n'),
|
|
581
|
+
);
|
|
335
582
|
await workspace.write(
|
|
336
583
|
'spawn-child.js',
|
|
337
584
|
[
|
|
338
585
|
"import { spawn } from 'node:child_process';",
|
|
339
|
-
"spawn(process.execPath, ['-
|
|
586
|
+
"spawn(process.execPath, ['group-child.js', process.argv[2]], { stdio: 'ignore' });",
|
|
340
587
|
'setTimeout(() => {}, 5000);',
|
|
341
588
|
'',
|
|
342
589
|
].join('\n'),
|
|
343
590
|
);
|
|
591
|
+
// Only the descendant owns the writer; EOF observes its actual terminal closure, not a time horizon.
|
|
592
|
+
const closed = readToEnd(lifetime);
|
|
593
|
+
let descendantPid: number | undefined;
|
|
594
|
+
const termination = armedKiller('process-group descendant', async (pid) => {
|
|
595
|
+
descendantPid = await armedPid(ready, pid, 'process-group descendant');
|
|
596
|
+
expect(descendantPid, `ready descendant must differ from group leader pid=${pid}`).not.toBe(pid);
|
|
597
|
+
});
|
|
598
|
+
|
|
599
|
+
const result = await runBoundedExec(
|
|
600
|
+
{ command: `exec node spawn-child.js ${marker}`, timeoutMs: 50, killAfterMs: 50 },
|
|
601
|
+
workspace.context,
|
|
602
|
+
termination.killer,
|
|
603
|
+
null,
|
|
604
|
+
null,
|
|
605
|
+
);
|
|
606
|
+
|
|
607
|
+
await termination.armed();
|
|
608
|
+
expect(await closed, `ready descendant pid=${descendantPid} must close its lifetime FIFO on exit`).toBe('');
|
|
609
|
+
expect(result.success).toBe(false);
|
|
610
|
+
expect(await exists(marker)).toBe(false);
|
|
611
|
+
});
|
|
612
|
+
|
|
613
|
+
it('force-kills what outlives a timed-out command in its process group', async () => {
|
|
614
|
+
if (process.platform === 'win32') {
|
|
615
|
+
return;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
const workspace = await createWorkspace();
|
|
619
|
+
const fifo = join(workspace.root, 'straggler.fifo');
|
|
620
|
+
const ready = join(workspace.root, 'straggler-ready.fifo');
|
|
621
|
+
expect(spawnSync('mkfifo', [fifo, ready]).status).toBe(0);
|
|
622
|
+
// The leader exits on SIGTERM. The subshell beside it ignores SIGTERM, a disposition its
|
|
623
|
+
// `sleep` inherits, and announces itself only once it holds the FIFO: from then on the
|
|
624
|
+
// graceful signal cannot end it. A process forked while that signal goes out is the same
|
|
625
|
+
// case, without the trap.
|
|
626
|
+
await workspace.write(
|
|
627
|
+
'straggle.sh',
|
|
628
|
+
[
|
|
629
|
+
'( trap "" TERM; exec 3>"$1"; echo ready; echo armed >"$2"; sleep 10; echo survived >&3 ) | {',
|
|
630
|
+
' read -r line; echo "$line"; exec sleep 30',
|
|
631
|
+
'}',
|
|
632
|
+
'',
|
|
633
|
+
].join('\n'),
|
|
634
|
+
);
|
|
635
|
+
// The straggler is the FIFO's only writer, so the read ends when it is gone: with nothing
|
|
636
|
+
// written if it was killed, with "survived" if it lived to finish its sleep.
|
|
637
|
+
const straggler = readToEnd(fifo);
|
|
638
|
+
const termination = armedKiller('TERM-ignoring FIFO straggler', async (pid) => {
|
|
639
|
+
expect(await readToEnd(ready), `straggler readiness pgid=${pid} must follow trap and FIFO open`).toBe('armed\n');
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
const result = await runBoundedExec(
|
|
643
|
+
{ command: `sh straggle.sh ${fifo} ${ready}`, timeoutMs: 5_000, killAfterMs: 30_000 },
|
|
644
|
+
workspace.context,
|
|
645
|
+
termination.killer,
|
|
646
|
+
null,
|
|
647
|
+
null,
|
|
648
|
+
);
|
|
649
|
+
|
|
650
|
+
await termination.armed();
|
|
651
|
+
expect(result.terminalOutput).toContain('ready');
|
|
652
|
+
expect(await straggler).toBe('');
|
|
653
|
+
expect(result.success).toBe(false);
|
|
654
|
+
expect(result.terminalOutput).toContain('Force-killing what the timed-out command left in its process group');
|
|
655
|
+
}, 60_000);
|
|
656
|
+
|
|
657
|
+
it('runs the command with its RAM lease as TMPDIR, names held dead leases, and ends the lease after it exits', async () => {
|
|
658
|
+
const workspace = await createWorkspace();
|
|
659
|
+
const directory = join(workspace.root, 'lease');
|
|
660
|
+
await mkdir(directory);
|
|
661
|
+
const held = '/v/9-abcdef is kept: /v/9-abcdef/x.asif (/dev/disk9) still attached from below it';
|
|
662
|
+
const volume = scriptedVolume({
|
|
663
|
+
ok: true,
|
|
664
|
+
value: { kind: 'leased', lease: leaseAt(directory), held: [held], reaped: [] },
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
const result = await runBoundedExec(
|
|
668
|
+
{ command: 'node -e "console.log(process.env.TMPDIR)"', timeoutMs: 5_000 },
|
|
669
|
+
workspace.context,
|
|
670
|
+
createProcessTreeKiller(),
|
|
671
|
+
volume,
|
|
672
|
+
null,
|
|
673
|
+
);
|
|
674
|
+
|
|
675
|
+
expect(result.success).toBe(true);
|
|
676
|
+
expect(result.terminalOutput).toContain(directory);
|
|
677
|
+
expect(result.terminalOutput).toContain(`RAM temp volume: dead lease ${held}\n`);
|
|
678
|
+
expect(volume.released).toEqual([directory]);
|
|
679
|
+
});
|
|
680
|
+
|
|
681
|
+
it('stops what the command left working in its lease before the lease ends, and says so', async () => {
|
|
682
|
+
const workspace = await createWorkspace();
|
|
683
|
+
const directory = join(workspace.root, 'lease');
|
|
684
|
+
await mkdir(directory);
|
|
685
|
+
const volume = scriptedVolume(
|
|
686
|
+
{
|
|
687
|
+
ok: true,
|
|
688
|
+
value: { kind: 'leased', lease: leaseAt(directory), held: [], reaped: ['4242 (nx daemon of a dead task)'] },
|
|
689
|
+
},
|
|
690
|
+
null,
|
|
691
|
+
['31337 (bun nx daemon --start)'],
|
|
692
|
+
);
|
|
344
693
|
|
|
345
694
|
const result = await runBoundedExec(
|
|
346
|
-
{ command:
|
|
695
|
+
{ command: 'node -e "0"', timeoutMs: 5_000 },
|
|
347
696
|
workspace.context,
|
|
348
697
|
createProcessTreeKiller(),
|
|
698
|
+
volume,
|
|
699
|
+
null,
|
|
349
700
|
);
|
|
350
701
|
|
|
351
|
-
|
|
702
|
+
expect(result.success).toBe(true);
|
|
703
|
+
expect(result.terminalOutput).toContain(
|
|
704
|
+
'RAM temp volume: stopped 31337 (bun nx daemon --start), which the command left working in its lease\n',
|
|
705
|
+
);
|
|
706
|
+
expect(result.terminalOutput).toContain(
|
|
707
|
+
'RAM temp volume: stopped 4242 (nx daemon of a dead task), which a dead task left working in its lease\n',
|
|
708
|
+
);
|
|
709
|
+
expect(volume.order).toEqual([`reap ${directory}`, `release ${directory}`]);
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it('keeps the inherited TMPDIR inside a sandbox and says so once', async () => {
|
|
713
|
+
const workspace = await createWorkspace();
|
|
714
|
+
const volume = scriptedVolume({ ok: true, value: { kind: 'sandboxed', detail: 'lock: EPERM' } });
|
|
715
|
+
|
|
716
|
+
const result = await runBoundedExec(
|
|
717
|
+
{ command: `node -e 'console.log("tmp=" + process.env.TMPDIR)'`, timeoutMs: 5_000, env: { TMPDIR: '/shed/tmp' } },
|
|
718
|
+
workspace.context,
|
|
719
|
+
createProcessTreeKiller(),
|
|
720
|
+
volume,
|
|
721
|
+
null,
|
|
722
|
+
);
|
|
723
|
+
|
|
724
|
+
expect(result.success).toBe(true);
|
|
725
|
+
expect(result.terminalOutput).toContain('tmp=/shed/tmp');
|
|
726
|
+
expect(result.terminalOutput.match(/RAM temp volume unavailable in this sandbox \(lock: EPERM\)/g)).toHaveLength(1);
|
|
727
|
+
expect(volume.released).toEqual([]);
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
it('does not run the command when the volume cannot be provisioned', async () => {
|
|
731
|
+
const workspace = await createWorkspace();
|
|
732
|
+
const marker = join(workspace.root, 'ran');
|
|
733
|
+
const volume = scriptedVolume({
|
|
734
|
+
ok: false,
|
|
735
|
+
error: { kind: 'provision-failed', step: 'hdiutil attach', detail: 'exit 1: Device not configured' },
|
|
736
|
+
});
|
|
737
|
+
|
|
738
|
+
const result = await runBoundedExec(
|
|
739
|
+
{ command: `touch ${marker}`, timeoutMs: 5_000 },
|
|
740
|
+
workspace.context,
|
|
741
|
+
createProcessTreeKiller(),
|
|
742
|
+
volume,
|
|
743
|
+
null,
|
|
744
|
+
);
|
|
352
745
|
|
|
353
746
|
expect(result.success).toBe(false);
|
|
747
|
+
expect(result.terminalOutput).toBe('RAM temp volume: hdiutil attach failed: exit 1: Device not configured\n');
|
|
354
748
|
expect(await exists(marker)).toBe(false);
|
|
355
749
|
});
|
|
750
|
+
|
|
751
|
+
it('names the full volume when a failed command left it without space', async () => {
|
|
752
|
+
const workspace = await createWorkspace();
|
|
753
|
+
const directory = join(workspace.root, 'lease');
|
|
754
|
+
await mkdir(directory);
|
|
755
|
+
const volume = scriptedVolume(
|
|
756
|
+
{ ok: true, value: { kind: 'leased', lease: leaseAt(directory), held: [], reaped: [] } },
|
|
757
|
+
{ kind: 'volume-full', mountpoint: workspace.root, capacityBytes: 1024 * 1024 * 1024, freeBytes: 1024 * 1024 },
|
|
758
|
+
);
|
|
759
|
+
|
|
760
|
+
const result = await runBoundedExec(
|
|
761
|
+
{ command: 'node -e "process.exit(1)"', timeoutMs: 5_000 },
|
|
762
|
+
workspace.context,
|
|
763
|
+
createProcessTreeKiller(),
|
|
764
|
+
volume,
|
|
765
|
+
null,
|
|
766
|
+
);
|
|
767
|
+
|
|
768
|
+
expect(result.success).toBe(false);
|
|
769
|
+
expect(result.terminalOutput).toContain(`RAM temp volume ${workspace.root} is full: 1 MiB free of 1024 MiB`);
|
|
770
|
+
expect(volume.released).toEqual([directory]);
|
|
771
|
+
});
|
|
772
|
+
|
|
773
|
+
it('keeps inherited nextest and Cargo ceilings under oversized grants', async () => {
|
|
774
|
+
const previousNextest = process.env.NEXTEST_TEST_THREADS;
|
|
775
|
+
const previousCargo = process.env.CARGO_BUILD_JOBS;
|
|
776
|
+
const previousRust = process.env.RUST_TEST_THREADS;
|
|
777
|
+
process.env.NEXTEST_TEST_THREADS = '6';
|
|
778
|
+
process.env.CARGO_BUILD_JOBS = '4';
|
|
779
|
+
process.env.RUST_TEST_THREADS = '6';
|
|
780
|
+
try {
|
|
781
|
+
for (const tokens of [15, 18]) {
|
|
782
|
+
const workspace = await createWorkspace();
|
|
783
|
+
const asked: number[] = [];
|
|
784
|
+
const released: number[] = [];
|
|
785
|
+
const budget: CpuBudget = {
|
|
786
|
+
async take(want) {
|
|
787
|
+
asked.push(want);
|
|
788
|
+
return heldGrant(tokens, released);
|
|
789
|
+
},
|
|
790
|
+
};
|
|
791
|
+
for (const scenario of [
|
|
792
|
+
{ runner: 'nextest run', variable: 'NEXTEST_TEST_THREADS', cap: 6 },
|
|
793
|
+
{ runner: 'cargo test', variable: 'CARGO_BUILD_JOBS', cap: 4 },
|
|
794
|
+
]) {
|
|
795
|
+
const command = `node -e "console.log('cap=' + process.env.${scenario.variable} + ' held=' + process.env.BOUNDED_EXEC_CPU_TOKENS)" # ${scenario.runner}`;
|
|
796
|
+
const result = await runBoundedExec(
|
|
797
|
+
{
|
|
798
|
+
command,
|
|
799
|
+
timeoutMs: 5_000,
|
|
800
|
+
parallelism: 18,
|
|
801
|
+
env: { NEXTEST_TEST_THREADS: '64', CARGO_BUILD_JOBS: '64', RUST_TEST_THREADS: '64' },
|
|
802
|
+
},
|
|
803
|
+
workspace.context,
|
|
804
|
+
createProcessTreeKiller(),
|
|
805
|
+
null,
|
|
806
|
+
budget,
|
|
807
|
+
);
|
|
808
|
+
expect(result.success).toBe(true);
|
|
809
|
+
expect(asked.at(-1)).toBe(scenario.cap);
|
|
810
|
+
expect(result.terminalOutput).toContain(`cap=${scenario.cap} held=${tokens}`);
|
|
811
|
+
}
|
|
812
|
+
expect(released).toEqual([tokens, tokens]);
|
|
813
|
+
}
|
|
814
|
+
} finally {
|
|
815
|
+
if (previousNextest === undefined) delete process.env.NEXTEST_TEST_THREADS;
|
|
816
|
+
else process.env.NEXTEST_TEST_THREADS = previousNextest;
|
|
817
|
+
if (previousCargo === undefined) delete process.env.CARGO_BUILD_JOBS;
|
|
818
|
+
else process.env.CARGO_BUILD_JOBS = previousCargo;
|
|
819
|
+
if (previousRust === undefined) delete process.env.RUST_TEST_THREADS;
|
|
820
|
+
else process.env.RUST_TEST_THREADS = previousRust;
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
|
|
824
|
+
it('starts the runner once granted, sized to its grant, and returns the tokens when it exits', async () => {
|
|
825
|
+
const workspace = await createWorkspace();
|
|
826
|
+
const asked: { want: number; checkout: string; command: string }[] = [];
|
|
827
|
+
const released: number[] = [];
|
|
828
|
+
const budget: CpuBudget = {
|
|
829
|
+
async take(want, checkout, command) {
|
|
830
|
+
asked.push({ want, checkout, command });
|
|
831
|
+
return heldGrant(3, released);
|
|
832
|
+
},
|
|
833
|
+
};
|
|
834
|
+
// `nextest run` in a trailing comment makes the shell command a nextest runner by invocation.
|
|
835
|
+
const command =
|
|
836
|
+
"node -e \"console.log('threads=' + process.env.NEXTEST_TEST_THREADS + ' tokens=' + process.env.BOUNDED_EXEC_CPU_TOKENS)\" # nextest run";
|
|
837
|
+
|
|
838
|
+
const result = await runBoundedExec(
|
|
839
|
+
{ command, timeoutMs: 5_000, parallelism: 8, env: { NEXTEST_TEST_THREADS: '64' } },
|
|
840
|
+
workspace.context,
|
|
841
|
+
createProcessTreeKiller(),
|
|
842
|
+
null,
|
|
843
|
+
budget,
|
|
844
|
+
);
|
|
845
|
+
|
|
846
|
+
expect(result.success).toBe(true);
|
|
847
|
+
expect(asked).toEqual([{ want: nextestWant, checkout: workspace.root, command }]);
|
|
848
|
+
expect(result.terminalOutput).toContain(`threads=${Math.min(3, nextestWant)} tokens=3`);
|
|
849
|
+
expect(result.terminalOutput).toMatch(
|
|
850
|
+
new RegExp(
|
|
851
|
+
`^cowshed: cpu-tokens wait done elapsed=\\d+ms status=ok tokens=3/${nextestWant} runner=nextest$`,
|
|
852
|
+
'm',
|
|
853
|
+
),
|
|
854
|
+
);
|
|
855
|
+
expect(released).toEqual([3]);
|
|
856
|
+
});
|
|
857
|
+
|
|
858
|
+
it('runs the command as asked, saying why, when the budget refuses', async () => {
|
|
859
|
+
const workspace = await createWorkspace();
|
|
860
|
+
const budget: CpuBudget = {
|
|
861
|
+
async take() {
|
|
862
|
+
return { granted: false, cause: 'predates', reason: 'the gateway predates the CPU budget' };
|
|
863
|
+
},
|
|
864
|
+
};
|
|
865
|
+
|
|
866
|
+
// A nextest runner configured for 64 threads keeps them: nothing sizes it without a grant.
|
|
867
|
+
const result = await runBoundedExec(
|
|
868
|
+
{
|
|
869
|
+
command: 'node -e "console.log(\'threads=\' + process.env.NEXTEST_TEST_THREADS)" # nextest run',
|
|
870
|
+
timeoutMs: 5_000,
|
|
871
|
+
env: { NEXTEST_TEST_THREADS: '64' },
|
|
872
|
+
},
|
|
873
|
+
workspace.context,
|
|
874
|
+
createProcessTreeKiller(),
|
|
875
|
+
null,
|
|
876
|
+
budget,
|
|
877
|
+
);
|
|
878
|
+
|
|
879
|
+
expect(result.success).toBe(true);
|
|
880
|
+
expect(result.terminalOutput).toContain('threads=64');
|
|
881
|
+
expect(result.terminalOutput).toContain(
|
|
882
|
+
'status=err runner=nextest runs unbudgeted: the gateway predates the CPU budget',
|
|
883
|
+
);
|
|
884
|
+
});
|
|
885
|
+
|
|
886
|
+
it('says nothing and runs as asked where no gateway listens', async () => {
|
|
887
|
+
const workspace = await createWorkspace();
|
|
888
|
+
const budget: CpuBudget = {
|
|
889
|
+
async take() {
|
|
890
|
+
return { granted: false, cause: 'absent', reason: 'no socket' };
|
|
891
|
+
},
|
|
892
|
+
};
|
|
893
|
+
|
|
894
|
+
const result = await runBoundedExec(
|
|
895
|
+
{ command: 'node -e "console.log(\'ran\')"', timeoutMs: 5_000 },
|
|
896
|
+
workspace.context,
|
|
897
|
+
createProcessTreeKiller(),
|
|
898
|
+
null,
|
|
899
|
+
budget,
|
|
900
|
+
);
|
|
901
|
+
|
|
902
|
+
expect(result.success).toBe(true);
|
|
903
|
+
expect(result.terminalOutput).toBe('ran\n');
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
it('sizes a runner to the grant its parent pool lent, takes nothing from the host, and returns nothing to it', async () => {
|
|
907
|
+
const workspace = await createWorkspace();
|
|
908
|
+
const asked: number[] = [];
|
|
909
|
+
const released: number[] = [];
|
|
910
|
+
const budget: CpuBudget = {
|
|
911
|
+
async take(want) {
|
|
912
|
+
asked.push(want);
|
|
913
|
+
return heldGrant(1, released);
|
|
914
|
+
},
|
|
915
|
+
};
|
|
916
|
+
const lent = lentCount(nextestWant, 3);
|
|
917
|
+
|
|
918
|
+
const result = await withPool(3, (pool) =>
|
|
919
|
+
runBoundedExec(
|
|
920
|
+
{
|
|
921
|
+
command: 'node -e "console.log(\'tokens=\' + process.env.BOUNDED_EXEC_CPU_TOKENS)" # nextest run',
|
|
922
|
+
timeoutMs: 5_000,
|
|
923
|
+
parallelism: 8,
|
|
924
|
+
},
|
|
925
|
+
workspace.context,
|
|
926
|
+
createProcessTreeKiller(),
|
|
927
|
+
null,
|
|
928
|
+
budget,
|
|
929
|
+
inheritedFrom(pool),
|
|
930
|
+
),
|
|
931
|
+
);
|
|
932
|
+
|
|
933
|
+
expect(result.success).toBe(true);
|
|
934
|
+
expect(asked).toEqual([]);
|
|
935
|
+
expect(released).toEqual([]);
|
|
936
|
+
expect(result.terminalOutput).toContain(`tokens=${lent}\n`);
|
|
937
|
+
expect(result.terminalOutput).toContain(`inherited=true tokens=${lent}/${nextestWant} runner=nextest`);
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
it('lends a runner no more than it asks for, even with no budget client', async () => {
|
|
941
|
+
const workspace = await createWorkspace();
|
|
942
|
+
const lent = lentCount(2, 16);
|
|
943
|
+
|
|
944
|
+
const result = await withPool(16, (pool) =>
|
|
945
|
+
runBoundedExec(
|
|
946
|
+
{
|
|
947
|
+
command: 'node -e "console.log(\'threads=\' + process.env.NEXTEST_TEST_THREADS)" # nextest run',
|
|
948
|
+
timeoutMs: 5_000,
|
|
949
|
+
parallelism: 2,
|
|
950
|
+
},
|
|
951
|
+
workspace.context,
|
|
952
|
+
createProcessTreeKiller(),
|
|
953
|
+
null,
|
|
954
|
+
null,
|
|
955
|
+
inheritedFrom(pool),
|
|
956
|
+
),
|
|
957
|
+
);
|
|
958
|
+
|
|
959
|
+
expect(result.success).toBe(true);
|
|
960
|
+
expect(result.terminalOutput).toContain(`threads=${lent}\n`);
|
|
961
|
+
expect(result.terminalOutput).toContain(`inherited=true tokens=${lent}/2 runner=nextest`);
|
|
962
|
+
});
|
|
963
|
+
|
|
964
|
+
it('takes and returns its own grant when no parent lent one', async () => {
|
|
965
|
+
const workspace = await createWorkspace();
|
|
966
|
+
const asked: number[] = [];
|
|
967
|
+
const released: number[] = [];
|
|
968
|
+
const budget: CpuBudget = {
|
|
969
|
+
async take(want) {
|
|
970
|
+
asked.push(want);
|
|
971
|
+
return heldGrant(3, released);
|
|
972
|
+
},
|
|
973
|
+
};
|
|
974
|
+
|
|
975
|
+
const result = await runBoundedExec(
|
|
976
|
+
{ command: 'node -e "console.log(1)" # nextest run', timeoutMs: 5_000, parallelism: 8 },
|
|
977
|
+
workspace.context,
|
|
978
|
+
createProcessTreeKiller(),
|
|
979
|
+
null,
|
|
980
|
+
budget,
|
|
981
|
+
null,
|
|
982
|
+
);
|
|
983
|
+
|
|
984
|
+
expect(result.success).toBe(true);
|
|
985
|
+
expect(asked).toEqual([nextestWant]);
|
|
986
|
+
expect(released).toEqual([3]);
|
|
987
|
+
expect(result.terminalOutput).toMatch(
|
|
988
|
+
new RegExp(
|
|
989
|
+
`^cowshed: cpu-tokens wait done elapsed=\\d+ms status=ok tokens=3/${nextestWant} runner=nextest$`,
|
|
990
|
+
'm',
|
|
991
|
+
),
|
|
992
|
+
);
|
|
993
|
+
expect(result.terminalOutput).not.toContain('inherited');
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
it('records a borrowed grant as inherited, and an owned grant without that mark', async () => {
|
|
997
|
+
const workspace = await createWorkspace();
|
|
998
|
+
const command = 'node -e "0" # nextest run';
|
|
999
|
+
const task = { id: 'app:test', hash: null };
|
|
1000
|
+
const borrowedDir = join(workspace.root, 'borrowed');
|
|
1001
|
+
const ownedDir = join(workspace.root, 'owned');
|
|
1002
|
+
const budget: CpuBudget = {
|
|
1003
|
+
async take() {
|
|
1004
|
+
return heldGrant(3, []);
|
|
1005
|
+
},
|
|
1006
|
+
};
|
|
1007
|
+
const lent = lentCount(nextestWant, 3);
|
|
1008
|
+
|
|
1009
|
+
const borrowed = await withPool(3, (pool) =>
|
|
1010
|
+
runBoundedExec(
|
|
1011
|
+
{ command, timeoutMs: 5_000, parallelism: 8 },
|
|
1012
|
+
{ ...workspace.context, record: { task, directory: borrowedDir } },
|
|
1013
|
+
createProcessTreeKiller(),
|
|
1014
|
+
null,
|
|
1015
|
+
null,
|
|
1016
|
+
inheritedFrom(pool),
|
|
1017
|
+
),
|
|
1018
|
+
);
|
|
1019
|
+
const owned = await runBoundedExec(
|
|
1020
|
+
{ command, timeoutMs: 5_000, parallelism: 8 },
|
|
1021
|
+
{ ...workspace.context, record: { task, directory: ownedDir } },
|
|
1022
|
+
createProcessTreeKiller(),
|
|
1023
|
+
null,
|
|
1024
|
+
budget,
|
|
1025
|
+
null,
|
|
1026
|
+
);
|
|
1027
|
+
|
|
1028
|
+
expect(borrowed.success).toBe(true);
|
|
1029
|
+
expect(owned.success).toBe(true);
|
|
1030
|
+
const borrowedRecord: unknown = JSON.parse(await readFile(join(borrowedDir, VERDICT_FILE), 'utf8'));
|
|
1031
|
+
expect(borrowedRecord).toHaveProperty('cpu', {
|
|
1032
|
+
tokens: lent,
|
|
1033
|
+
want: nextestWant,
|
|
1034
|
+
waitedMs: expect.any(Number),
|
|
1035
|
+
inherited: true,
|
|
1036
|
+
});
|
|
1037
|
+
const ownedRecord: unknown = JSON.parse(await readFile(join(ownedDir, VERDICT_FILE), 'utf8'));
|
|
1038
|
+
expect(ownedRecord).toHaveProperty('cpu', { tokens: 3, want: nextestWant, waitedMs: expect.any(Number) });
|
|
1039
|
+
expect(ownedRecord).not.toHaveProperty('cpu.inherited');
|
|
1040
|
+
});
|
|
1041
|
+
|
|
1042
|
+
it('records borrowed grant loss during timeout termination as an ordinary failure, not a bound', async () => {
|
|
1043
|
+
if (process.platform === 'win32') return;
|
|
1044
|
+
const workspace = await createWorkspace();
|
|
1045
|
+
const directory = join(workspace.root, 'lost-during-timeout');
|
|
1046
|
+
const ignoring = ignoresTerm(workspace.root);
|
|
1047
|
+
const calls: NodeJS.Signals[] = [];
|
|
1048
|
+
const result = await withPool(1, (pool) =>
|
|
1049
|
+
runBoundedExec(
|
|
1050
|
+
{
|
|
1051
|
+
command: ignoring.command,
|
|
1052
|
+
timeoutMs: 50,
|
|
1053
|
+
killAfterMs: 50,
|
|
1054
|
+
},
|
|
1055
|
+
{ ...workspace.context, record: { task: { id: 'lost-during-timeout', hash: null }, directory } },
|
|
1056
|
+
{
|
|
1057
|
+
async kill(pid, signal) {
|
|
1058
|
+
if (signal === 'SIGTERM') {
|
|
1059
|
+
await ignoring.armed();
|
|
1060
|
+
const closed = await pool.close();
|
|
1061
|
+
if (!closed.ok) throw new Error(closed.error);
|
|
1062
|
+
}
|
|
1063
|
+
calls.push(signal);
|
|
1064
|
+
await createProcessTreeKiller().kill(pid, signal);
|
|
1065
|
+
},
|
|
1066
|
+
},
|
|
1067
|
+
null,
|
|
1068
|
+
null,
|
|
1069
|
+
inheritedFrom(pool),
|
|
1070
|
+
),
|
|
1071
|
+
);
|
|
1072
|
+
expect(result.success).toBe(false);
|
|
1073
|
+
expect(calls).toEqual(['SIGTERM', 'SIGKILL']);
|
|
1074
|
+
expect(result.terminalOutput).toContain('cpu-tokens grant lost');
|
|
1075
|
+
const record: unknown = JSON.parse(await readFile(join(directory, VERDICT_FILE), 'utf8'));
|
|
1076
|
+
expect(record).toHaveProperty('verdict.outcome', 'failed');
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
it('asks the host budget when a parent pool refuses its descriptor, and says why', async () => {
|
|
1080
|
+
const workspace = await createWorkspace();
|
|
1081
|
+
const asked: number[] = [];
|
|
1082
|
+
const released: number[] = [];
|
|
1083
|
+
const budget: CpuBudget = {
|
|
1084
|
+
async take(want) {
|
|
1085
|
+
asked.push(want);
|
|
1086
|
+
return heldGrant(3, released);
|
|
1087
|
+
},
|
|
1088
|
+
};
|
|
1089
|
+
const gone = { socket: join(workspace.root, 'gone.sock'), tokens: 3 };
|
|
1090
|
+
|
|
1091
|
+
const result = await runBoundedExec(
|
|
1092
|
+
{ command: 'node -e "console.log(\'ran\')" # nextest run', timeoutMs: 5_000, parallelism: 8 },
|
|
1093
|
+
workspace.context,
|
|
1094
|
+
createProcessTreeKiller(),
|
|
1095
|
+
null,
|
|
1096
|
+
budget,
|
|
1097
|
+
gone,
|
|
1098
|
+
);
|
|
1099
|
+
|
|
1100
|
+
expect(result.success).toBe(true);
|
|
1101
|
+
expect(asked).toEqual([nextestWant]);
|
|
1102
|
+
expect(released).toEqual([3]);
|
|
1103
|
+
expect(result.terminalOutput).toContain('cowshed: cpu-tokens parent pool refused: ');
|
|
1104
|
+
expect(result.terminalOutput).toContain(`status=ok tokens=3/${nextestWant} runner=nextest`);
|
|
1105
|
+
expect(result.terminalOutput).not.toContain('inherited=true');
|
|
1106
|
+
});
|
|
1107
|
+
|
|
1108
|
+
it('ignores a target env that names the CPU budget, when no budget grants it', async () => {
|
|
1109
|
+
const workspace = await createWorkspace();
|
|
1110
|
+
|
|
1111
|
+
const result = await runBoundedExec(
|
|
1112
|
+
{
|
|
1113
|
+
command: 'node -e "console.log(\'tokens=\' + process.env.BOUNDED_EXEC_CPU_TOKENS)"',
|
|
1114
|
+
timeoutMs: 5_000,
|
|
1115
|
+
env: { BOUNDED_EXEC_CPU_TOKENS: '977' },
|
|
1116
|
+
},
|
|
1117
|
+
workspace.context,
|
|
1118
|
+
createProcessTreeKiller(),
|
|
1119
|
+
null,
|
|
1120
|
+
null,
|
|
1121
|
+
);
|
|
1122
|
+
|
|
1123
|
+
expect(result.success).toBe(true);
|
|
1124
|
+
expect(result.terminalOutput).toContain('tokens=undefined\n');
|
|
1125
|
+
expect(result.terminalOutput).toContain(
|
|
1126
|
+
`cowshed: BOUNDED_EXEC_CPU_TOKENS in the target's env is ignored: the CPU budget sets it`,
|
|
1127
|
+
);
|
|
1128
|
+
});
|
|
1129
|
+
|
|
1130
|
+
it('sizes the command from its own grant, whatever the target env names', async () => {
|
|
1131
|
+
const workspace = await createWorkspace();
|
|
1132
|
+
const released: number[] = [];
|
|
1133
|
+
const budget: CpuBudget = {
|
|
1134
|
+
async take() {
|
|
1135
|
+
return heldGrant(2, released);
|
|
1136
|
+
},
|
|
1137
|
+
};
|
|
1138
|
+
|
|
1139
|
+
const result = await runBoundedExec(
|
|
1140
|
+
{
|
|
1141
|
+
command: 'node -e "console.log(\'tokens=\' + process.env.BOUNDED_EXEC_CPU_TOKENS)" # nextest run',
|
|
1142
|
+
timeoutMs: 5_000,
|
|
1143
|
+
parallelism: 8,
|
|
1144
|
+
env: { BOUNDED_EXEC_CPU_TOKENS: '977' },
|
|
1145
|
+
},
|
|
1146
|
+
workspace.context,
|
|
1147
|
+
createProcessTreeKiller(),
|
|
1148
|
+
null,
|
|
1149
|
+
budget,
|
|
1150
|
+
);
|
|
1151
|
+
|
|
1152
|
+
expect(result.success).toBe(true);
|
|
1153
|
+
expect(result.terminalOutput).toContain('tokens=2\n');
|
|
1154
|
+
expect(result.terminalOutput).not.toContain('977');
|
|
1155
|
+
expect(released).toEqual([2]);
|
|
1156
|
+
});
|
|
1157
|
+
|
|
1158
|
+
it('refuses the run when its parent pool answers with a refusal, asking no host budget', async () => {
|
|
1159
|
+
const socketDirectory = await ownedSocketDirectory('cpu-refuse-', 'p.sock');
|
|
1160
|
+
workspaces.push(socketDirectory);
|
|
1161
|
+
const socket = join(socketDirectory, 'p.sock');
|
|
1162
|
+
// A live pool that refuses answers `other`, not `absent`: the run must fail, not ask the host.
|
|
1163
|
+
const refusing = createServer((connection) => {
|
|
1164
|
+
connection.once('data', () => {
|
|
1165
|
+
connection.end('{"ok":false,"code":"refused","error":"the pool is shutting down"}\n');
|
|
1166
|
+
});
|
|
1167
|
+
});
|
|
1168
|
+
await new Promise<void>((resolve) => refusing.listen(socket, resolve));
|
|
1169
|
+
const workspace = await createWorkspace();
|
|
1170
|
+
const asked: number[] = [];
|
|
1171
|
+
const budget: CpuBudget = {
|
|
1172
|
+
async take(want) {
|
|
1173
|
+
asked.push(want);
|
|
1174
|
+
return heldGrant(1, []);
|
|
1175
|
+
},
|
|
1176
|
+
};
|
|
1177
|
+
try {
|
|
1178
|
+
const result = await runBoundedExec(
|
|
1179
|
+
{ command: 'touch ran.marker', timeoutMs: 5_000 },
|
|
1180
|
+
{
|
|
1181
|
+
...workspace.context,
|
|
1182
|
+
record: { task: { id: 'refused', hash: null }, directory: join(workspace.root, 'refused') },
|
|
1183
|
+
},
|
|
1184
|
+
createProcessTreeKiller(),
|
|
1185
|
+
null,
|
|
1186
|
+
budget,
|
|
1187
|
+
{ socket, tokens: 3 },
|
|
1188
|
+
);
|
|
1189
|
+
|
|
1190
|
+
expect(result.success).toBe(false);
|
|
1191
|
+
expect(asked).toEqual([]);
|
|
1192
|
+
expect(await exists(join(workspace.root, 'ran.marker'))).toBe(false);
|
|
1193
|
+
expect(result.terminalOutput).toContain('cowshed: cpu-tokens parent pool refused: ');
|
|
1194
|
+
expect(result.terminalOutput).toContain('refusing to run the command without asking the host budget');
|
|
1195
|
+
expect(await readFile(join(workspace.root, 'refused', VERDICT_FILE), 'utf8')).toMatch(/"outcome":\s*"failed"/);
|
|
1196
|
+
} finally {
|
|
1197
|
+
await new Promise<void>((resolve) => refusing.close(() => resolve()));
|
|
1198
|
+
}
|
|
1199
|
+
});
|
|
1200
|
+
|
|
1201
|
+
it('never gives a command a budget it was not granted, whatever pool or count its environment names', async () => {
|
|
1202
|
+
const workspace = await createWorkspace();
|
|
1203
|
+
await workspace.write('probe.js', BUDGET_PROBE);
|
|
1204
|
+
const previousPool = process.env[CPU_POOL_ENV];
|
|
1205
|
+
const previousTokens = process.env[CPU_TOKENS_ENV];
|
|
1206
|
+
// A stale descriptor in this process's own environment is no budget either.
|
|
1207
|
+
process.env[CPU_POOL_ENV] = '/stale.sock';
|
|
1208
|
+
process.env[CPU_TOKENS_ENV] = '5';
|
|
1209
|
+
try {
|
|
1210
|
+
const absent: CpuBudget = {
|
|
1211
|
+
async take() {
|
|
1212
|
+
return { granted: false, cause: 'absent', reason: 'no budget socket' };
|
|
1213
|
+
},
|
|
1214
|
+
};
|
|
1215
|
+
const unbudgeted = await withPool(3, (pool) =>
|
|
1216
|
+
runBoundedExec(
|
|
1217
|
+
{ command: 'node probe.js', timeoutMs: 10_000, env: { ...pool.environment, [CPU_TOKENS_ENV]: '9' } },
|
|
1218
|
+
workspace.context,
|
|
1219
|
+
createProcessTreeKiller(),
|
|
1220
|
+
null,
|
|
1221
|
+
absent,
|
|
1222
|
+
),
|
|
1223
|
+
);
|
|
1224
|
+
expect(unbudgeted.terminalOutput).toContain('[null,null]\n[null,null]\n');
|
|
1225
|
+
|
|
1226
|
+
const released: number[] = [];
|
|
1227
|
+
let injected = '';
|
|
1228
|
+
const granted = await withPool(3, (pool) => {
|
|
1229
|
+
injected = inheritedFrom(pool).socket;
|
|
1230
|
+
return runBoundedExec(
|
|
1231
|
+
{ command: 'node probe.js', timeoutMs: 10_000, env: { ...pool.environment, [CPU_TOKENS_ENV]: '9' } },
|
|
1232
|
+
workspace.context,
|
|
1233
|
+
createProcessTreeKiller(),
|
|
1234
|
+
null,
|
|
1235
|
+
{
|
|
1236
|
+
async take() {
|
|
1237
|
+
return heldGrant(2, released);
|
|
1238
|
+
},
|
|
1239
|
+
},
|
|
1240
|
+
);
|
|
1241
|
+
});
|
|
1242
|
+
const [own, grandchild] = granted.terminalOutput.split('\n').filter((line) => line.startsWith('['));
|
|
1243
|
+
expect(own).toBe(grandchild);
|
|
1244
|
+
expect(own).toMatch(/,"2"\]$/);
|
|
1245
|
+
expect(own).not.toContain(injected);
|
|
1246
|
+
expect(own).not.toContain('/stale.sock');
|
|
1247
|
+
expect(released).toEqual([2]);
|
|
1248
|
+
} finally {
|
|
1249
|
+
if (previousPool === undefined) {
|
|
1250
|
+
delete process.env[CPU_POOL_ENV];
|
|
1251
|
+
} else {
|
|
1252
|
+
process.env[CPU_POOL_ENV] = previousPool;
|
|
1253
|
+
}
|
|
1254
|
+
if (previousTokens === undefined) {
|
|
1255
|
+
delete process.env[CPU_TOKENS_ENV];
|
|
1256
|
+
} else {
|
|
1257
|
+
process.env[CPU_TOKENS_ENV] = previousTokens;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
});
|
|
356
1261
|
});
|
|
357
1262
|
|
|
358
1263
|
interface WorkspaceFixture {
|
|
@@ -361,6 +1266,77 @@ interface WorkspaceFixture {
|
|
|
361
1266
|
write(filePath: string, contents: string): Promise<void>;
|
|
362
1267
|
}
|
|
363
1268
|
|
|
1269
|
+
/** Everything written to the FIFO at `path` until its last writer closes it. */
|
|
1270
|
+
async function readToEnd(path: string): Promise<string> {
|
|
1271
|
+
const handle = await open(path, 'r');
|
|
1272
|
+
try {
|
|
1273
|
+
return await handle.readFile('utf8');
|
|
1274
|
+
} finally {
|
|
1275
|
+
await handle.close();
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/** Reuses the real killer, delaying delivery (not the bound) until the named fixture is armed. */
|
|
1280
|
+
function armedKiller(subject: string, ready: (pid: number) => Promise<void>) {
|
|
1281
|
+
const real = createProcessTreeKiller();
|
|
1282
|
+
const calls: NodeJS.Signals[] = [];
|
|
1283
|
+
let armed: Promise<void> | undefined;
|
|
1284
|
+
return {
|
|
1285
|
+
calls,
|
|
1286
|
+
killer: {
|
|
1287
|
+
async kill(pid: number, signal: NodeJS.Signals) {
|
|
1288
|
+
ownedProcessGroups.add(pid);
|
|
1289
|
+
armed ??= ready(pid);
|
|
1290
|
+
await armed;
|
|
1291
|
+
calls.push(signal);
|
|
1292
|
+
await real.kill(pid, signal);
|
|
1293
|
+
},
|
|
1294
|
+
} satisfies ProcessTreeKiller,
|
|
1295
|
+
async armed() {
|
|
1296
|
+
expect(armed, `${subject}: readiness was never requested before timeout signal delivery`).toBeDefined();
|
|
1297
|
+
// The executor tolerates killer errors; re-await here so a failed readiness assertion cannot be swallowed.
|
|
1298
|
+
await armed;
|
|
1299
|
+
},
|
|
1300
|
+
};
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
/** A complete readiness line identifies a live member of the executor's own process group. */
|
|
1304
|
+
async function armedPid(path: string, pgid: number, subject: string): Promise<number> {
|
|
1305
|
+
const text = await readToEnd(path);
|
|
1306
|
+
const match = /^ready pid=(\d+)\n$/.exec(text);
|
|
1307
|
+
const pid = Number(match?.[1]);
|
|
1308
|
+
const detail = `${subject}: ready=${JSON.stringify(text)} fifo=${path} pid=${pid} expected pgid=${pgid}`;
|
|
1309
|
+
expect(Number.isSafeInteger(pid) && pid > 0, `${detail}; state=unobserved (invalid readiness PID)`).toBe(true);
|
|
1310
|
+
// Bare `ps` through PATH: NixOS has no /bin/ps, and a failed spawn leaves stdout null.
|
|
1311
|
+
const observed = spawnSync('ps', ['-o', 'pid=,ppid=,pgid=,stat=', '-p', String(pid)], { encoding: 'utf8' });
|
|
1312
|
+
const observedStdout = observed.stdout ?? '';
|
|
1313
|
+
const state = `${detail}; state=${observedStdout.trim()} status=${observed.status} stderr=${observed.stderr} error=${observed.error}`;
|
|
1314
|
+
expect(observed.status, state).toBe(0);
|
|
1315
|
+
const [seenPid, , seenGroup, stat] = observedStdout.trim().split(/\s+/);
|
|
1316
|
+
expect(Number(seenPid), state).toBe(pid);
|
|
1317
|
+
expect(Number(seenGroup), state).toBe(pgid);
|
|
1318
|
+
expect(stat !== undefined && !stat.startsWith('Z'), state).toBe(true);
|
|
1319
|
+
return pid;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
/**
|
|
1323
|
+
* A command that ignores SIGTERM, a disposition its `sleep` inherits, and `armed`, which resolves once it does. A
|
|
1324
|
+
* killer that awaits `armed` before the graceful signal sends it to a command that cannot die of it: a bound that fires
|
|
1325
|
+
* before the command's shell has even run `trap` (a 250 ms timer lost that race in a loaded gate) ends it on the
|
|
1326
|
+
* default disposition, and the escalation under test never runs. The command's FIFO write waits for the reader, so
|
|
1327
|
+
* the announcement can neither precede the trap nor be missed.
|
|
1328
|
+
*/
|
|
1329
|
+
function ignoresTerm(root: string): { command: string; armed: () => Promise<void> } {
|
|
1330
|
+
const fifo = join(root, 'armed.fifo');
|
|
1331
|
+
expect(spawnSync('mkfifo', [fifo]).status).toBe(0);
|
|
1332
|
+
return {
|
|
1333
|
+
command: `sh -c 'trap "" TERM; echo armed > "$0"; exec sleep 5' '${fifo}'`,
|
|
1334
|
+
armed: async () => {
|
|
1335
|
+
expect(await readToEnd(fifo)).toBe('armed\n');
|
|
1336
|
+
},
|
|
1337
|
+
};
|
|
1338
|
+
}
|
|
1339
|
+
|
|
364
1340
|
async function createWorkspace(): Promise<WorkspaceFixture> {
|
|
365
1341
|
const root = await mkdtemp(join(tmpdir(), 'smoothbricks-bounded-exec-'));
|
|
366
1342
|
workspaces.push(root);
|
|
@@ -375,6 +1351,35 @@ async function createWorkspace(): Promise<WorkspaceFixture> {
|
|
|
375
1351
|
};
|
|
376
1352
|
}
|
|
377
1353
|
|
|
1354
|
+
function leaseAt(directory: string): RamTempLease {
|
|
1355
|
+
return { directory, mountpoint: dirname(directory), capacityBytes: 1024 * 1024 * 1024 };
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/** A temp volume whose answers are fixed, recording which leases were reaped and released, in order. */
|
|
1359
|
+
function scriptedVolume(
|
|
1360
|
+
acquisition: Result<RamTempAcquisition, RamTempError>,
|
|
1361
|
+
full: RamTempError | null = null,
|
|
1362
|
+
leftovers: string[] = [],
|
|
1363
|
+
): TempVolume & { released: string[]; order: string[] } {
|
|
1364
|
+
const released: string[] = [];
|
|
1365
|
+
const order: string[] = [];
|
|
1366
|
+
return {
|
|
1367
|
+
released,
|
|
1368
|
+
order,
|
|
1369
|
+
acquire: () => Promise.resolve(acquisition),
|
|
1370
|
+
reap(lease) {
|
|
1371
|
+
order.push(`reap ${lease.directory}`);
|
|
1372
|
+
return Promise.resolve({ ok: true, value: leftovers });
|
|
1373
|
+
},
|
|
1374
|
+
release(lease) {
|
|
1375
|
+
released.push(lease.directory);
|
|
1376
|
+
order.push(`release ${lease.directory}`);
|
|
1377
|
+
return Promise.resolve({ ok: true, value: undefined });
|
|
1378
|
+
},
|
|
1379
|
+
fullness: () => Promise.resolve(full),
|
|
1380
|
+
};
|
|
1381
|
+
}
|
|
1382
|
+
|
|
378
1383
|
function captureWrite(chunks: string[]): typeof process.stdout.write {
|
|
379
1384
|
return ((
|
|
380
1385
|
chunk: string | Uint8Array,
|
|
@@ -397,6 +1402,54 @@ async function exists(path: string): Promise<boolean> {
|
|
|
397
1402
|
}
|
|
398
1403
|
}
|
|
399
1404
|
|
|
400
|
-
|
|
401
|
-
|
|
1405
|
+
/** A granted host grant whose `closed` resolves on release, as the gateway's does when the grant's socket closes. */
|
|
1406
|
+
function heldGrant(tokens: number, released: number[]): TokenGrant {
|
|
1407
|
+
let closeGrant!: () => void;
|
|
1408
|
+
const closed = new Promise<void>((resolve) => {
|
|
1409
|
+
closeGrant = resolve;
|
|
1410
|
+
});
|
|
1411
|
+
return {
|
|
1412
|
+
granted: true,
|
|
1413
|
+
tokens,
|
|
1414
|
+
release: () => {
|
|
1415
|
+
released.push(tokens);
|
|
1416
|
+
closeGrant();
|
|
1417
|
+
},
|
|
1418
|
+
closed,
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
/** A parent's CPU pool of `tokens`, open while `body` runs. Its upstream never closes on its own. */
|
|
1423
|
+
async function withPool<T>(tokens: number, body: (pool: CpuPool) => Promise<T>): Promise<T> {
|
|
1424
|
+
const created = await createCpuPool(tokens, new Promise<void>(() => {}));
|
|
1425
|
+
if (!created.ok) {
|
|
1426
|
+
throw new Error(`cpu pool: ${created.error}`);
|
|
1427
|
+
}
|
|
1428
|
+
let result: { ok: true; value: T } | { ok: false; error: unknown };
|
|
1429
|
+
try {
|
|
1430
|
+
result = { ok: true, value: await body(created.pool) };
|
|
1431
|
+
} catch (error) {
|
|
1432
|
+
result = { ok: false, error };
|
|
1433
|
+
}
|
|
1434
|
+
const outcome = await created.pool.close();
|
|
1435
|
+
if (!outcome.ok) {
|
|
1436
|
+
const failure = new Error(`cpu pool did not close: ${outcome.error}`);
|
|
1437
|
+
throw result.ok ? failure : new AggregateError([result.error, failure], 'CPU pool body and cleanup failed');
|
|
1438
|
+
}
|
|
1439
|
+
if (!result.ok) throw result.error;
|
|
1440
|
+
return result.value;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
/** The descriptor a run under `pool` inherits: the pool's own environment, read back the way a child reads it. */
|
|
1444
|
+
function inheritedFrom(pool: CpuPool): InheritedCpuPool {
|
|
1445
|
+
const parent = readInheritedCpuPool(pool.environment);
|
|
1446
|
+
if (parent === null) {
|
|
1447
|
+
throw new Error('the pool environment names no endpoint');
|
|
1448
|
+
}
|
|
1449
|
+
return parent;
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
/** The count a pool of `tokens` lends a run that wants `want`: min(want, tokens, ceil(cores / 2)). */
|
|
1453
|
+
function lentCount(want: number, tokens: number): number {
|
|
1454
|
+
return Math.min(want, tokens, Math.ceil(availableParallelism() / 2));
|
|
402
1455
|
}
|