@smoothbricks/nx-plugin 0.4.26 → 0.4.28
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 +994 -117
- 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 +15 -73
- 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 +3 -2
- package/dist/cargo-closure-input.d.ts.map +1 -1
- package/dist/cargo-closure-input.js +16 -15
- 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 +43 -6
- package/dist/cargo-source-hash.d.ts.map +1 -1
- package/dist/cargo-source-hash.js +309 -73
- 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 +101 -5
- package/dist/cargo-workspace.d.ts.map +1 -1
- package/dist/cargo-workspace.js +291 -10
- 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 +207 -12
- package/dist/ensure-built.d.ts.map +1 -1
- package/dist/ensure-built.js +576 -202
- 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 +370 -138
- 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.d.ts.map +1 -1
- package/dist/manifest-hash.js +2 -5
- 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 -0
- package/dist/workspace-manifest.d.ts.map +1 -1
- package/executors.json +5 -0
- package/loader.js +13 -11
- package/managed/raw/envrc +33 -67
- 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 +569 -11
- package/managed/raw/tooling/direnv/devenv.smoo.nix +314 -93
- 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 +1146 -159
- 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 -41
- package/package.json +74 -12
- package/runtime-input.sh +81 -0
- package/src/__tests__/counted-cargo.ts +33 -14
- package/src/__tests__/fixture-nx-env.ts +111 -46
- 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 +16 -74
- 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 -21
- 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 +138 -66
- package/src/cargo-closure-input.ts +17 -15
- package/src/cargo-dev-profile.ts +137 -0
- package/src/cargo-resolve-cache.test.ts +70 -1
- package/src/cargo-source-hash.test.ts +193 -97
- package/src/cargo-source-hash.ts +345 -68
- package/src/cargo-toolchain-policy.ts +6 -0
- package/src/cargo-workspace.test.ts +320 -0
- package/src/cargo-workspace.ts +336 -10
- 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 +1036 -578
- package/src/ensure-built.ts +820 -224
- 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 +909 -295
- package/src/index.ts +441 -143
- package/src/linked-snapshots.nx.test.ts +172 -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 +2 -6
- 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 +427 -82
- package/src/nx-cache-bound.test.ts +5 -10
- 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 +7 -16
- 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 +26 -25
- 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 +87 -89
- 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 -0
- package/test-timings.json +85 -30
- package/managed/raw/tooling/direnv/inherited-devenv.ts +0 -665
- package/managed/raw/tooling/direnv/shared-caches.sh +0 -53
|
@@ -0,0 +1,1528 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { randomUUID } from 'node:crypto';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { lstat, mkdir, mkdtemp, open, readdir, readFile, rename, rm, rmdir, stat, statfs, writeFile, } from 'node:fs/promises';
|
|
5
|
+
import { Socket } from 'node:net';
|
|
6
|
+
import { basename, join } from 'node:path';
|
|
7
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
8
|
+
import { awaitExit, pidsWorkingIn, processTable, terminate, withDescendants, } from '../../testing.js';
|
|
9
|
+
import { isRecord } from '../../type-guards.js';
|
|
10
|
+
import { diskClassOf, recordedShare, takeDiskLease } from './disk-lease.js';
|
|
11
|
+
import { GATEWAY_SOCKET } from './gateway-lease.js';
|
|
12
|
+
import { captureHeldVerdict, parseHeldVerdict, validateHeldVerdict, } from './ram-temp-held.js';
|
|
13
|
+
/**
|
|
14
|
+
* One RAM-backed APFS volume per host user, shared by every test task that runs
|
|
15
|
+
* on the host, so test temp files never reach the SSD.
|
|
16
|
+
*
|
|
17
|
+
* Measured on macOS 26.6:
|
|
18
|
+
* - `hdiutil attach ram://<sectors>` allocates lazily: an attached 8 GiB volume
|
|
19
|
+
* costs ~13 MB until written, so the capacity is a ceiling, not a reservation.
|
|
20
|
+
* - Deleting files does not hand pages back (no TRIM through the RAM device):
|
|
21
|
+
* the volume's cost is its high-water mark until it is detached. That is why
|
|
22
|
+
* the volume is detached as soon as no task holds a lease, rather than kept.
|
|
23
|
+
* - The memory is pageable (it belongs to diskimages-helper), so it reaches the
|
|
24
|
+
* SSD only through swap under memory pressure.
|
|
25
|
+
* - A RAM disk belongs to the legacy DiskImages framework: attaching one starts
|
|
26
|
+
* a `diskimages-helper` that exits on detach, with no AppleDiskImageDevice and
|
|
27
|
+
* no `diskimagesiod`, so it spends none of the AppleDiskImages2 attach budget
|
|
28
|
+
* that cowshed's images and tests draw on (specs/cowshed/01a_apfs.md, "How
|
|
29
|
+
* the APFS host degrades"; measured 2026-10-05).
|
|
30
|
+
* - Attach + format + mount costs ~2 s and is DiskArbitration traffic, so the
|
|
31
|
+
* volume is created once, lazily, and every task takes a subdirectory.
|
|
32
|
+
* - Only DiskArbitration's own mountpoint (`/Volumes/<volume name>`) is safe to
|
|
33
|
+
* ask for: `diskutil mount -mountPoint <dir>` was refused as not privileged
|
|
34
|
+
* (0xf8da0009), and storagekitd escalated it to an administrator dialog whose
|
|
35
|
+
* wait blocked every diskutil on the host. A default mount raised no authd
|
|
36
|
+
* request (log show, 2026-10-04).
|
|
37
|
+
* - Without authorization the volume mounts only `noowners`: launchd refuses a
|
|
38
|
+
* plist from it ("Caller specified a plist with bad ownership/permissions"),
|
|
39
|
+
* so a test that bootstraps a launchd job keeps the plist outside TMPDIR.
|
|
40
|
+
*
|
|
41
|
+
* One kernel lock (`O_EXLOCK` on a file in /private/tmp), dropped by the kernel
|
|
42
|
+
* when its holder dies, admits changes to a small ledger beside it and to the
|
|
43
|
+
* set of lease directories. It is held only to read and publish those: the
|
|
44
|
+
* host-wide probes (`ioreg`, `mount`, the process census), directory cleanup,
|
|
45
|
+
* provisioning and detach run outside it, under claims the ledger publishes.
|
|
46
|
+
* One caller creates or detaches the volume at a time, and a caller that finds
|
|
47
|
+
* either in flight yields and looks again; each dead lease is settled by the
|
|
48
|
+
* one caller that claimed it, and every other caller passes it by. A claim
|
|
49
|
+
* names its owner's pid, so a claim whose owner died is void. Before it
|
|
50
|
+
* removes a lease or detaches the volume, a caller takes the lock again and
|
|
51
|
+
* checks that the volume, the lease directory and its own claim are still as
|
|
52
|
+
* it claimed them. A lease is a directory named after the task process's pid;
|
|
53
|
+
* a lease whose pid is gone is reclaimed by the next task, and the volume is
|
|
54
|
+
* detached when the last lease ends.
|
|
55
|
+
*
|
|
56
|
+
* The volume is created under its final name and never renamed. It is mounted by `mount_apfs` at a
|
|
57
|
+
* private staging directory and marked there; the kernel then unmounts it (`umount`) and `diskutil mount`, the
|
|
58
|
+
* only call that places it at its mountpoint, so it reaches `/Volumes` already marked. A Disk
|
|
59
|
+
* Arbitration rename names whatever volume holds the path it recorded, and a force-unmounted volume
|
|
60
|
+
* leaves a bare directory there on the host's Data volume, so no rename is made. `noowners` lets the
|
|
61
|
+
* marker be written, since a fresh volume's root is owned by root (measured 2026-10-15). A creator that
|
|
62
|
+
* dies part way leaves debris at the staging directory or as an unmounted volume, never an unmarked
|
|
63
|
+
* volume at the mountpoint, which DiskArbitration would make every later volume dodge as "<name> 1".
|
|
64
|
+
* The host's own image list is the only record of what was
|
|
65
|
+
* attached: before creating, a task detaches every RAM disk of this user and
|
|
66
|
+
* this volume's exact size that is mounted nowhere but this volume's names,
|
|
67
|
+
* because a creator killed or deadlined mid-step leaves one behind — measured
|
|
68
|
+
* 2026-10-06: a deadlined `hdiutil attach` whose diskimages-helper finished the
|
|
69
|
+
* attach after `hdiutil` was killed, and a volume mounted with no marker. A
|
|
70
|
+
* disk with a lease directory on it is never detached; its error names it.
|
|
71
|
+
*
|
|
72
|
+
* An image attached from a file below a lease belongs to whoever attached it:
|
|
73
|
+
* cowshed's real-APFS fixtures release theirs under the production per-image
|
|
74
|
+
* lease, from scratch roots outside TMPDIR (specs/cowshed/08_testing.md). This
|
|
75
|
+
* module never detaches one, and never deletes through a mount below a lease.
|
|
76
|
+
* A dead lease that still backs an image or holds a mount is kept, with the
|
|
77
|
+
* volume under it, and every task that takes a lease names it. Where every
|
|
78
|
+
* holder has an identity a cheap `lstat` re-proves (a mount below the lease),
|
|
79
|
+
* the ledger keeps that proof (`ram-temp-held.ts`) and later tasks check it
|
|
80
|
+
* instead of probing the host; any change sends the lease back to a probe.
|
|
81
|
+
* Every disk child runs under a deadline, in its own process group so the
|
|
82
|
+
* deadline also reaches the helper it spawned, so a wedged DiskArbitration
|
|
83
|
+
* fails the task instead of every task waiting on the volume. A detach is the
|
|
84
|
+
* exception: never killed at its deadline, because an interrupted detach can
|
|
85
|
+
* wedge DiskArbitration itself; its wait is bounded and reported by name, and
|
|
86
|
+
* a stray's detach gets the short stray bound, so one wedged stray cannot
|
|
87
|
+
* hold the create claim while a chain of tasks queues behind it.
|
|
88
|
+
*
|
|
89
|
+
* A cowshed sandbox denies writes to /private/tmp (EPERM on the lock), and
|
|
90
|
+
* cannot attach disk images at all; there the task keeps the workspace's own
|
|
91
|
+
* TMPDIR and says so once on stderr.
|
|
92
|
+
*/
|
|
93
|
+
/** macOS `open(2)` flag: take an exclusive flock atomically with the open. Absent from `fs.constants`. */
|
|
94
|
+
const O_EXLOCK = 0x20;
|
|
95
|
+
/** Contended opens yield without occupying a filesystem worker needed by the lock holder. */
|
|
96
|
+
const LOCK_RETRY_MS = 50;
|
|
97
|
+
/**
|
|
98
|
+
* A caller waiting this long on another's creation or detach says so on stderr, and again each
|
|
99
|
+
* time as long again passes: a claim whose owner pid was reused after a crash never voids itself.
|
|
100
|
+
*/
|
|
101
|
+
const CLAIM_WAIT_NOTICE_MS = 15_000;
|
|
102
|
+
const SECTOR_BYTES = 512;
|
|
103
|
+
const MIB = 1024 * 1024;
|
|
104
|
+
const GIB = 1024 * MIB;
|
|
105
|
+
/**
|
|
106
|
+
* The revision of the volume's protocol. Checkouts at different revisions run on one host at
|
|
107
|
+
* once, and each is safe only beside its own: revision 1 leases under the kernel lock alone,
|
|
108
|
+
* revision 2 leases and detaches outside it, behind claims in a ledger revision 1 never reads.
|
|
109
|
+
* So no revision shares a volume with another. It names the volume, its lock and its ledger, and
|
|
110
|
+
* adds as many sectors to the RAM disk, whose size and user are what a creator's stray reclaim
|
|
111
|
+
* matches: neither revision detaches the other's disk, mounted or still attaching.
|
|
112
|
+
*/
|
|
113
|
+
const RAM_TEMP_REVISION = 2;
|
|
114
|
+
/**
|
|
115
|
+
* The volume's ceiling, plus the revision's sectors. Pages are allocated as they are written and
|
|
116
|
+
* returned when the volume detaches, so this bounds what a runaway suite can take, not what a gate
|
|
117
|
+
* costs. Measured 2026-10-04: a full parallel smoothbricks `lint test build host-controller-test`
|
|
118
|
+
* gate peaked at 1.61 GiB used with 18 concurrent leases; 8 GiB is five times that. That
|
|
119
|
+
* gate also staged cowshed's real-APFS fixture images on the volume, which now stay under
|
|
120
|
+
* /private/tmp, so the peak is an upper bound.
|
|
121
|
+
*/
|
|
122
|
+
export const RAM_TEMP_CAPACITY_BYTES = 8 * GIB + RAM_TEMP_REVISION * SECTOR_BYTES;
|
|
123
|
+
/** A failed task with less than this free on the volume is reported as having run out of space. */
|
|
124
|
+
const FULL_FREE_BYTES = 64 * MIB;
|
|
125
|
+
/** `<pid>-<mkdtemp suffix>`: the pid says whether the lease's task is alive. */
|
|
126
|
+
const LEASE_NAME = /^(\d+)-[A-Za-z0-9]{6}$/;
|
|
127
|
+
const MARKER_FILE = '.smoo-ram';
|
|
128
|
+
/**
|
|
129
|
+
* How long a process a task left in its lease gets to exit on SIGTERM before it gets SIGKILL: the
|
|
130
|
+
* time a daemon takes to stop its watcher and its plugin workers, and well under the grace a
|
|
131
|
+
* bounded leg gives its own command.
|
|
132
|
+
*/
|
|
133
|
+
const LEFTOVER_GRACE_MS = 5_000;
|
|
134
|
+
/** A `ps` state in which a process starts nothing: stopped (`T`, `t` when traced) or exited (`Z`). */
|
|
135
|
+
const STARTS_NOTHING = /^[TtZ]/;
|
|
136
|
+
/**
|
|
137
|
+
* The processes `host` reads. A lease belongs to one task and its command is gone by the time the
|
|
138
|
+
* lease ends, so what still works in it is what the command left behind. A fixture's Nx daemon is
|
|
139
|
+
* the usual one: Nx detaches it into a session of its own, so the process-group kill that ends a
|
|
140
|
+
* timed-out command never reaches it, and a test process that is killed runs no teardown of its
|
|
141
|
+
* own. Deleting the lease asks such a daemon to leave only by deleting its record, which it
|
|
142
|
+
* notices within a second when it is idle and not at all while it is wedged; a signal does not
|
|
143
|
+
* depend on the daemon noticing, and a process left on the volume is what keeps it from
|
|
144
|
+
* detaching.
|
|
145
|
+
*
|
|
146
|
+
* A leftover keeps running while it is looked for, and a child it starts after the process table
|
|
147
|
+
* was read is in no table that was. So each one found is held with SIGSTOP and the table is read
|
|
148
|
+
* again, until a read names nothing that the read before did not already show stopped: a stopped
|
|
149
|
+
* process starts nothing, and whatever it started before it stopped is in every table read after.
|
|
150
|
+
* Discovery waits for stopped-census readiness; the exit grace starts only after SIGTERM.
|
|
151
|
+
* Only then does each get SIGTERM, and SIGCONT to act on it. A child whose parent exited before it
|
|
152
|
+
* could be held has lost its ancestry, so once the stopped-table barrier holds, the lease is asked
|
|
153
|
+
* again what works in it, including when that table names nothing, to recover reparented children.
|
|
154
|
+
*/
|
|
155
|
+
export function leaseProcessesOn(host) {
|
|
156
|
+
return {
|
|
157
|
+
async stopWorkingIn(directory) {
|
|
158
|
+
let working = (await host.workingIn(directory)).filter((pid) => pid !== process.pid);
|
|
159
|
+
if (working.length === 0) {
|
|
160
|
+
return [];
|
|
161
|
+
}
|
|
162
|
+
const known = new Map();
|
|
163
|
+
let held = new Set();
|
|
164
|
+
let failures;
|
|
165
|
+
try {
|
|
166
|
+
for (;;) {
|
|
167
|
+
const roots = [...working, ...known.keys()];
|
|
168
|
+
const current = withDescendants(await host.table(), roots).filter((entry) => entry.pid !== process.pid);
|
|
169
|
+
for (const entry of current) {
|
|
170
|
+
known.set(entry.pid, entry);
|
|
171
|
+
}
|
|
172
|
+
if (current.every((entry) => held.has(entry.pid) && STARTS_NOTHING.test(entry.stat))) {
|
|
173
|
+
working = (await host.workingIn(directory)).filter((pid) => pid !== process.pid);
|
|
174
|
+
if (working.every((pid) => held.has(pid))) {
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const entry of current) {
|
|
179
|
+
host.signal(entry.pid, 'SIGSTOP');
|
|
180
|
+
}
|
|
181
|
+
held = new Set(current.filter((entry) => STARTS_NOTHING.test(entry.stat)).map((entry) => entry.pid));
|
|
182
|
+
}
|
|
183
|
+
for (const entry of known.values()) {
|
|
184
|
+
host.signal(entry.pid, 'SIGTERM');
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
catch (error) {
|
|
188
|
+
failures = [error];
|
|
189
|
+
}
|
|
190
|
+
finally {
|
|
191
|
+
// SIGTERM is pending while stopped. One failed resume must not strand the other holds.
|
|
192
|
+
for (const entry of known.values()) {
|
|
193
|
+
try {
|
|
194
|
+
host.signal(entry.pid, 'SIGCONT');
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
failures ??= [];
|
|
198
|
+
failures.push(new Error(`${entry.pid} (${entry.command}): SIGCONT failed: ${error instanceof Error ? error.message : String(error)}`, { cause: error }));
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (failures !== undefined) {
|
|
203
|
+
if (failures.length === 1) {
|
|
204
|
+
throw failures[0];
|
|
205
|
+
}
|
|
206
|
+
throw new AggregateError(failures, `processes working in ${directory}: ${failures.map((error) => (error instanceof Error ? error.message : String(error))).join('; ')}`);
|
|
207
|
+
}
|
|
208
|
+
const leftovers = [...known.values()];
|
|
209
|
+
try {
|
|
210
|
+
await awaitExit(leftovers, `processes working in ${directory}`, LEFTOVER_GRACE_MS);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
for (const entry of leftovers) {
|
|
214
|
+
host.signal(entry.pid, 'SIGKILL');
|
|
215
|
+
}
|
|
216
|
+
await awaitExit(leftovers, `processes working in ${directory} after SIGKILL`, LEFTOVER_GRACE_MS);
|
|
217
|
+
}
|
|
218
|
+
return leftovers.map((entry) => `${entry.pid} (${entry.command.slice(0, 160)})`);
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
export const hostLeaseProcesses = leaseProcessesOn({
|
|
223
|
+
workingIn: pidsWorkingIn,
|
|
224
|
+
table: processTable,
|
|
225
|
+
signal: terminate,
|
|
226
|
+
});
|
|
227
|
+
export function ramTempPaths(user) {
|
|
228
|
+
const volumeName = `smoo-ram${RAM_TEMP_REVISION}-${user.uid}`;
|
|
229
|
+
return {
|
|
230
|
+
mountpoint: join('/Volumes', volumeName),
|
|
231
|
+
lockFile: `/private/tmp/${volumeName}.lock`,
|
|
232
|
+
volumeName,
|
|
233
|
+
staging: `/private/tmp/${volumeName}-new`,
|
|
234
|
+
user: user.username,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
export function describeRamTempError(error) {
|
|
238
|
+
switch (error.kind) {
|
|
239
|
+
case 'provision-failed':
|
|
240
|
+
return `RAM temp volume: ${error.step} failed: ${error.detail}`;
|
|
241
|
+
case 'volume-full':
|
|
242
|
+
return `RAM temp volume ${error.mountpoint} is full: ${Math.round(error.freeBytes / MIB)} MiB free of ${Math.round(error.capacityBytes / MIB)} MiB; raise RAM_TEMP_CAPACITY_BYTES in @smoothbricks/nx-plugin`;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const EMPTY_LEDGER = { lifecycle: null, claims: [], held: [] };
|
|
246
|
+
export class RamTempVolume {
|
|
247
|
+
paths;
|
|
248
|
+
capacityBytes;
|
|
249
|
+
commands;
|
|
250
|
+
pid;
|
|
251
|
+
isAlive;
|
|
252
|
+
processes;
|
|
253
|
+
/** The RAM disk's size, which with the attaching user identifies this module's disks on the host. */
|
|
254
|
+
sectors;
|
|
255
|
+
/** The claims and held verdicts the lock admits changes to, beside the lock file. */
|
|
256
|
+
ledgerPath;
|
|
257
|
+
constructor(paths, capacityBytes, commands, pid = process.pid, isAlive = processIsAlive, processes = hostLeaseProcesses) {
|
|
258
|
+
this.paths = paths;
|
|
259
|
+
this.capacityBytes = capacityBytes;
|
|
260
|
+
this.commands = commands;
|
|
261
|
+
this.pid = pid;
|
|
262
|
+
this.isAlive = isAlive;
|
|
263
|
+
this.processes = processes;
|
|
264
|
+
this.sectors = Math.ceil(capacityBytes / SECTOR_BYTES);
|
|
265
|
+
this.ledgerPath = `${paths.lockFile}.ledger`;
|
|
266
|
+
}
|
|
267
|
+
/** Take a lease: create the volume if no task holds one, and a private directory on it. */
|
|
268
|
+
async acquire() {
|
|
269
|
+
const op = { owner: { pid: this.pid, token: randomUUID() }, claimed: false, made: null };
|
|
270
|
+
let result;
|
|
271
|
+
try {
|
|
272
|
+
result = await this.lease(op);
|
|
273
|
+
}
|
|
274
|
+
catch (error) {
|
|
275
|
+
result = failed('lease', errorText(error));
|
|
276
|
+
}
|
|
277
|
+
const ended = await this.end(op);
|
|
278
|
+
if (result.ok) {
|
|
279
|
+
if (ended.ok) {
|
|
280
|
+
return result;
|
|
281
|
+
}
|
|
282
|
+
// Its claims could not be dropped, so the lease is not handed out; the one made goes back.
|
|
283
|
+
const undone = await this.undo(op);
|
|
284
|
+
return { ok: false, error: alsoFailed(ended.error, undone) };
|
|
285
|
+
}
|
|
286
|
+
// The caller gets no lease, so the one made for it goes; a returned lease is never touched.
|
|
287
|
+
const cleanup = [...(ended.ok ? [] : [ended.error]), ...(await this.undo(op))];
|
|
288
|
+
if (result.error.kind !== 'lock-denied') {
|
|
289
|
+
return { ok: false, error: alsoFailed(result.error, cleanup) };
|
|
290
|
+
}
|
|
291
|
+
if (cleanup.length === 0) {
|
|
292
|
+
return { ok: true, value: { kind: 'sandboxed', detail: result.error.detail } };
|
|
293
|
+
}
|
|
294
|
+
const denied = { kind: 'provision-failed', step: 'lock', detail: result.error.detail };
|
|
295
|
+
return { ok: false, error: alsoFailed(denied, cleanup) };
|
|
296
|
+
}
|
|
297
|
+
/** Make a lease directory for `op`'s caller, recorded so a failed acquire takes it back. */
|
|
298
|
+
async make(op) {
|
|
299
|
+
const directory = await mkdtemp(join(this.paths.mountpoint, `${this.pid}-`));
|
|
300
|
+
const identity = await leaseIdentity(directory);
|
|
301
|
+
op.made = identity === null ? null : { directory, identity };
|
|
302
|
+
return directory;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Undo a failed acquire: take back the lease made for it, which may have been the volume's last
|
|
306
|
+
* with every release already past it, so then detach the volume as the last release would have.
|
|
307
|
+
* Answers what failed on the way.
|
|
308
|
+
*/
|
|
309
|
+
async undo(op) {
|
|
310
|
+
const made = op.made !== null;
|
|
311
|
+
const unmade = await this.unmake(op);
|
|
312
|
+
if (!unmade.ok) {
|
|
313
|
+
return [unmade.error];
|
|
314
|
+
}
|
|
315
|
+
if (!made) {
|
|
316
|
+
return [];
|
|
317
|
+
}
|
|
318
|
+
const idle = await this.locked(op, 'detach', async (ledger, volume) => volume !== null && volume.leases.length === 0 && ledger.lifecycle === null && ledger.claims.length === 0
|
|
319
|
+
? { ledger: { ...ledger, lifecycle: { kind: 'detach', owner: op.owner } }, value: volume }
|
|
320
|
+
: { ledger, value: null });
|
|
321
|
+
const detached = !idle.ok || idle.value === null ? idle : await this.detach(op, idle.value);
|
|
322
|
+
const failures = [];
|
|
323
|
+
if (!detached.ok) {
|
|
324
|
+
failures.push(detached.error.kind === 'lock-denied'
|
|
325
|
+
? { kind: 'provision-failed', step: 'lock', detail: detached.error.detail }
|
|
326
|
+
: detached.error);
|
|
327
|
+
}
|
|
328
|
+
const ended = await this.end(op);
|
|
329
|
+
if (!ended.ok) {
|
|
330
|
+
failures.push(ended.error);
|
|
331
|
+
}
|
|
332
|
+
return failures;
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Take back the lease made for a caller that never got it. No task has used it, so it is empty:
|
|
336
|
+
* `rmdir` removes only that, follows no link, and refuses a directory with a mount on it. A
|
|
337
|
+
* directory that is no longer the one made, by identity, is left where it is and named.
|
|
338
|
+
*/
|
|
339
|
+
async unmake(op) {
|
|
340
|
+
const made = op.made;
|
|
341
|
+
if (made === null) {
|
|
342
|
+
return { ok: true, value: undefined };
|
|
343
|
+
}
|
|
344
|
+
try {
|
|
345
|
+
const identity = await leaseIdentity(made.directory);
|
|
346
|
+
if (identity !== null && identity !== made.identity) {
|
|
347
|
+
return failed('lease rollback', `${made.directory} is no longer the lease made for this task; left in place`);
|
|
348
|
+
}
|
|
349
|
+
if (identity !== null) {
|
|
350
|
+
await rmdir(made.directory);
|
|
351
|
+
}
|
|
352
|
+
op.made = null;
|
|
353
|
+
return { ok: true, value: undefined };
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
return failed('lease rollback', `${made.directory}: ${errorText(error)}`);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
async lease(op) {
|
|
360
|
+
let waited = null;
|
|
361
|
+
for (;;) {
|
|
362
|
+
const taken = await this.locked(op, 'lease', async (ledger, volume) => {
|
|
363
|
+
if (ledger.lifecycle !== null) {
|
|
364
|
+
return { ledger, value: { kind: 'wait', claim: ledger.lifecycle } };
|
|
365
|
+
}
|
|
366
|
+
if (volume === null) {
|
|
367
|
+
return { ledger: { ...ledger, lifecycle: { kind: 'create', owner: op.owner } }, value: { kind: 'create' } };
|
|
368
|
+
}
|
|
369
|
+
// The lease exists before the lock drops, so no release finds the volume empty under it.
|
|
370
|
+
const directory = await this.make(op);
|
|
371
|
+
const claimed = await this.claim(ledger, volume, op.owner, null);
|
|
372
|
+
return {
|
|
373
|
+
ledger: claimed.ledger,
|
|
374
|
+
value: { kind: 'leased', volume, directory, held: claimed.held, candidates: claimed.candidates },
|
|
375
|
+
};
|
|
376
|
+
});
|
|
377
|
+
if (!taken.ok) {
|
|
378
|
+
return taken;
|
|
379
|
+
}
|
|
380
|
+
const value = taken.value;
|
|
381
|
+
switch (value.kind) {
|
|
382
|
+
case 'wait':
|
|
383
|
+
// Another caller is creating or detaching the volume: look again once it has.
|
|
384
|
+
waited = await this.waitOut(value.claim, waited);
|
|
385
|
+
continue;
|
|
386
|
+
case 'create':
|
|
387
|
+
return await this.create(op);
|
|
388
|
+
case 'leased': {
|
|
389
|
+
const settled = await this.settle(op, value.volume, value.candidates);
|
|
390
|
+
if (!settled.ok) {
|
|
391
|
+
return settled;
|
|
392
|
+
}
|
|
393
|
+
if (value.candidates.length > 0) {
|
|
394
|
+
const published = await this.locked(op, 'lease', async (ledger, current) => ({
|
|
395
|
+
ledger: settledLedger(ledger, current, op.owner, settled.value.verdicts),
|
|
396
|
+
value: undefined,
|
|
397
|
+
}));
|
|
398
|
+
if (!published.ok) {
|
|
399
|
+
return published;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
return {
|
|
403
|
+
ok: true,
|
|
404
|
+
value: {
|
|
405
|
+
kind: 'leased',
|
|
406
|
+
lease: {
|
|
407
|
+
directory: value.directory,
|
|
408
|
+
mountpoint: this.paths.mountpoint,
|
|
409
|
+
capacityBytes: value.volume.capacity,
|
|
410
|
+
},
|
|
411
|
+
held: [...value.held, ...settled.value.held].map((lease) => lease.text),
|
|
412
|
+
reaped: [...settled.value.reaped],
|
|
413
|
+
},
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
/** With this caller's creation claim published: provision the volume, then take the first lease on it. */
|
|
420
|
+
async create(op) {
|
|
421
|
+
const provisioned = await this.provisionVolume();
|
|
422
|
+
if (!provisioned.ok) {
|
|
423
|
+
return provisioned;
|
|
424
|
+
}
|
|
425
|
+
const placed = await this.locked(op, 'lease', async (ledger, volume) => {
|
|
426
|
+
const next = withoutClaimsOf(ledger, op.owner);
|
|
427
|
+
if (ledger.lifecycle?.owner.token !== op.owner.token) {
|
|
428
|
+
return {
|
|
429
|
+
ledger: next,
|
|
430
|
+
value: failed('create', `the creation claim in ${this.ledgerPath} was lost while the volume was provisioned`),
|
|
431
|
+
};
|
|
432
|
+
}
|
|
433
|
+
if (volume === null) {
|
|
434
|
+
return {
|
|
435
|
+
ledger: next,
|
|
436
|
+
value: failed('create', `${this.paths.mountpoint} is not the marked volume once provisioned`),
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
return { ledger: next, value: { ok: true, value: await this.make(op) } };
|
|
440
|
+
});
|
|
441
|
+
if (!placed.ok) {
|
|
442
|
+
return placed;
|
|
443
|
+
}
|
|
444
|
+
if (!placed.value.ok) {
|
|
445
|
+
return placed.value;
|
|
446
|
+
}
|
|
447
|
+
return {
|
|
448
|
+
ok: true,
|
|
449
|
+
value: {
|
|
450
|
+
kind: 'leased',
|
|
451
|
+
lease: { directory: placed.value.value, mountpoint: this.paths.mountpoint, capacityBytes: this.capacityBytes },
|
|
452
|
+
held: [],
|
|
453
|
+
reaped: [],
|
|
454
|
+
},
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Stop what the task left working in its lease, before the lease ends: the processes whose
|
|
459
|
+
* working directory is in it and everything they started, answered as `<pid> (<command>)`.
|
|
460
|
+
* Runs outside the volume's lock, which one lease's leftovers must not hold up the others
|
|
461
|
+
* behind, and costs a scan of every process only when the lease still holds anything.
|
|
462
|
+
*/
|
|
463
|
+
async reap(lease) {
|
|
464
|
+
try {
|
|
465
|
+
const entries = await readdir(lease.directory).catch((error) => {
|
|
466
|
+
if (errorCode(error) === 'ENOENT')
|
|
467
|
+
return [];
|
|
468
|
+
throw error;
|
|
469
|
+
});
|
|
470
|
+
if (entries.length === 0) {
|
|
471
|
+
return { ok: true, value: [] };
|
|
472
|
+
}
|
|
473
|
+
return { ok: true, value: await this.processes.stopWorkingIn(lease.directory) };
|
|
474
|
+
}
|
|
475
|
+
catch (error) {
|
|
476
|
+
return failed('reap', errorText(error));
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
/** End a lease; the last lease detaches the volume, returning its RAM. */
|
|
480
|
+
async release(lease) {
|
|
481
|
+
const op = { owner: { pid: this.pid, token: randomUUID() }, claimed: false, made: null };
|
|
482
|
+
let result;
|
|
483
|
+
try {
|
|
484
|
+
result = await this.releaseLease(op, basename(lease.directory));
|
|
485
|
+
}
|
|
486
|
+
catch (error) {
|
|
487
|
+
result = failed('release', errorText(error));
|
|
488
|
+
}
|
|
489
|
+
const ended = await this.end(op);
|
|
490
|
+
if (result.ok) {
|
|
491
|
+
return ended;
|
|
492
|
+
}
|
|
493
|
+
const primary = result.error.kind === 'lock-denied'
|
|
494
|
+
? { kind: 'provision-failed', step: 'lock', detail: result.error.detail }
|
|
495
|
+
: result.error;
|
|
496
|
+
return { ok: false, error: alsoFailed(primary, ended.ok ? [] : [ended.error]) };
|
|
497
|
+
}
|
|
498
|
+
async releaseLease(op, own) {
|
|
499
|
+
let waited = null;
|
|
500
|
+
for (;;) {
|
|
501
|
+
const taken = await this.locked(op, 'release', async (ledger, volume) => {
|
|
502
|
+
if (volume === null) {
|
|
503
|
+
return { ledger, value: { kind: 'unmounted' } };
|
|
504
|
+
}
|
|
505
|
+
if (ledger.lifecycle !== null) {
|
|
506
|
+
return { ledger, value: { kind: 'wait', claim: ledger.lifecycle } };
|
|
507
|
+
}
|
|
508
|
+
const claimed = await this.claim(ledger, volume, op.owner, own);
|
|
509
|
+
return {
|
|
510
|
+
ledger: claimed.ledger,
|
|
511
|
+
value: { kind: 'claimed', volume, held: claimed.held, candidates: claimed.candidates },
|
|
512
|
+
};
|
|
513
|
+
});
|
|
514
|
+
if (!taken.ok) {
|
|
515
|
+
return taken;
|
|
516
|
+
}
|
|
517
|
+
const value = taken.value;
|
|
518
|
+
switch (value.kind) {
|
|
519
|
+
case 'unmounted':
|
|
520
|
+
return { ok: true, value: undefined };
|
|
521
|
+
case 'wait':
|
|
522
|
+
waited = await this.waitOut(value.claim, waited);
|
|
523
|
+
continue;
|
|
524
|
+
case 'claimed': {
|
|
525
|
+
const settled = await this.settle(op, value.volume, value.candidates);
|
|
526
|
+
if (!settled.ok) {
|
|
527
|
+
return settled;
|
|
528
|
+
}
|
|
529
|
+
const idle = await this.locked(op, 'release', async (ledger, current) => {
|
|
530
|
+
const next = settledLedger(ledger, current, op.owner, settled.value.verdicts);
|
|
531
|
+
// The last lease is gone, and nobody else settles one or changes the volume: detach it.
|
|
532
|
+
return current?.key === value.volume.key &&
|
|
533
|
+
current.leases.length === 0 &&
|
|
534
|
+
next.lifecycle === null &&
|
|
535
|
+
next.claims.length === 0
|
|
536
|
+
? { ledger: { ...next, lifecycle: { kind: 'detach', owner: op.owner } }, value: true }
|
|
537
|
+
: { ledger: next, value: false };
|
|
538
|
+
});
|
|
539
|
+
if (!idle.ok) {
|
|
540
|
+
return idle;
|
|
541
|
+
}
|
|
542
|
+
const kept = [...value.held, ...settled.value.held].find((lease) => lease.name === own);
|
|
543
|
+
if (kept !== undefined) {
|
|
544
|
+
return failed('release', kept.text);
|
|
545
|
+
}
|
|
546
|
+
return idle.value ? await this.detach(op, value.volume) : { ok: true, value: undefined };
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
/**
|
|
552
|
+
* Wait out another caller's creation or detach. Liveness of its owner is its pid's, so a crashed
|
|
553
|
+
* owner whose pid was reused holds every caller here; past each `CLAIM_WAIT_NOTICE_MS` on the same
|
|
554
|
+
* claim, stderr names it with the wait so far.
|
|
555
|
+
*/
|
|
556
|
+
async waitOut(claim, waited) {
|
|
557
|
+
const now = performance.now();
|
|
558
|
+
const wait = waited?.token === claim.owner.token ? waited : { token: claim.owner.token, since: now, notices: 0 };
|
|
559
|
+
const elapsed = now - wait.since;
|
|
560
|
+
const due = Math.floor(elapsed / CLAIM_WAIT_NOTICE_MS);
|
|
561
|
+
if (due > wait.notices) {
|
|
562
|
+
process.stderr.write(`RAM temp volume: waited ${(elapsed / 1000).toFixed(1)} s on the ${claim.kind} claim of pid ${claim.owner.pid} (token ${claim.owner.token}) in ${this.ledgerPath}; if that pid is not ${claim.kind === 'create' ? 'creating' : 'detaching'} ${this.paths.mountpoint}, remove the ledger once no task uses the volume\n`);
|
|
563
|
+
}
|
|
564
|
+
await sleep(LOCK_RETRY_MS);
|
|
565
|
+
return { ...wait, notices: Math.max(wait.notices, due) };
|
|
566
|
+
}
|
|
567
|
+
/** After a failed task: whether the volume ran out of space, which is then the error to report. */
|
|
568
|
+
async fullness(lease) {
|
|
569
|
+
try {
|
|
570
|
+
const fs = await statfs(lease.mountpoint);
|
|
571
|
+
const freeBytes = fs.bavail * fs.bsize;
|
|
572
|
+
return freeBytes < FULL_FREE_BYTES
|
|
573
|
+
? { kind: 'volume-full', mountpoint: lease.mountpoint, capacityBytes: lease.capacityBytes, freeBytes }
|
|
574
|
+
: null;
|
|
575
|
+
}
|
|
576
|
+
catch {
|
|
577
|
+
return null;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
async lock() {
|
|
581
|
+
for (;;) {
|
|
582
|
+
try {
|
|
583
|
+
const handle = await open(this.paths.lockFile, constants.O_RDWR | constants.O_CREAT | constants.O_NOFOLLOW | constants.O_NONBLOCK | O_EXLOCK, 0o600);
|
|
584
|
+
return { ok: true, value: handle };
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
const code = errorCode(error);
|
|
588
|
+
if (code !== 'EAGAIN' && code !== 'EWOULDBLOCK') {
|
|
589
|
+
return { ok: false, error: { code, message: errorText(error) } };
|
|
590
|
+
}
|
|
591
|
+
// Blocking O_EXLOCK opens consume libuv workers. Four waiters can occupy the entire
|
|
592
|
+
// pool while the holder's read/close waits for a worker, deadlocking every host task.
|
|
593
|
+
await sleep(LOCK_RETRY_MS);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
/** Drop whatever claims `op` still holds: an operation that failed part way leaves none behind it. */
|
|
598
|
+
async end(op) {
|
|
599
|
+
if (!op.claimed) {
|
|
600
|
+
return { ok: true, value: undefined };
|
|
601
|
+
}
|
|
602
|
+
const dropped = await this.locked(op, 'claim release', async (ledger) => ({
|
|
603
|
+
ledger: withoutClaimsOf(ledger, op.owner),
|
|
604
|
+
value: undefined,
|
|
605
|
+
}));
|
|
606
|
+
if (dropped.ok) {
|
|
607
|
+
return dropped;
|
|
608
|
+
}
|
|
609
|
+
return dropped.error.kind === 'lock-denied'
|
|
610
|
+
? failed('lock', dropped.error.detail)
|
|
611
|
+
: { ok: false, error: dropped.error };
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* One step under the lock: read the ledger, void the claims of dead owners and the verdicts of
|
|
615
|
+
* another volume or a lease that is gone, observe the volume, and publish what `change` returns.
|
|
616
|
+
* `change` reads metadata and takes or names directories; it runs no host command.
|
|
617
|
+
*/
|
|
618
|
+
async locked(op, step, change) {
|
|
619
|
+
const lock = await this.lock();
|
|
620
|
+
if (!lock.ok) {
|
|
621
|
+
return lock.error.code === 'EPERM'
|
|
622
|
+
? { ok: false, error: { kind: 'lock-denied', detail: `${this.paths.lockFile}: ${lock.error.message}` } }
|
|
623
|
+
: failed('lock', lock.error.message);
|
|
624
|
+
}
|
|
625
|
+
try {
|
|
626
|
+
const read = await this.readLedger();
|
|
627
|
+
if (!read.ok) {
|
|
628
|
+
return read;
|
|
629
|
+
}
|
|
630
|
+
const volume = await this.observe();
|
|
631
|
+
const next = await change(prune(read.value.ledger, volume, this.isAlive), volume);
|
|
632
|
+
const text = ledgerText(next.ledger);
|
|
633
|
+
if (text !== (read.value.text ?? ledgerText(EMPTY_LEDGER))) {
|
|
634
|
+
await publishLedger(this.ledgerPath, text);
|
|
635
|
+
}
|
|
636
|
+
op.claimed =
|
|
637
|
+
next.ledger.lifecycle?.owner.token === op.owner.token ||
|
|
638
|
+
next.ledger.claims.some((claim) => claim.owner.token === op.owner.token);
|
|
639
|
+
return { ok: true, value: next.value };
|
|
640
|
+
}
|
|
641
|
+
catch (error) {
|
|
642
|
+
return failed(step, errorText(error));
|
|
643
|
+
}
|
|
644
|
+
finally {
|
|
645
|
+
await lock.value.close();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
/** The ledger as published, and its text; none yet is an empty one. Only this module writes it, whole. */
|
|
649
|
+
async readLedger() {
|
|
650
|
+
let text;
|
|
651
|
+
try {
|
|
652
|
+
const handle = await open(this.ledgerPath, constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
653
|
+
try {
|
|
654
|
+
text = await handle.readFile('utf8');
|
|
655
|
+
}
|
|
656
|
+
finally {
|
|
657
|
+
await handle.close();
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
catch (error) {
|
|
661
|
+
if (errorCode(error) === 'ENOENT') {
|
|
662
|
+
return { ok: true, value: { ledger: EMPTY_LEDGER, text: null } };
|
|
663
|
+
}
|
|
664
|
+
return failed('ledger', `${this.ledgerPath}: ${errorText(error)}`);
|
|
665
|
+
}
|
|
666
|
+
// Claims that cannot be read cannot be honoured, so no lease or volume change is safe until it is
|
|
667
|
+
// removed; only this module writes it, in one rename, so a malformed one is not its own.
|
|
668
|
+
const refused = (detail) => failed('ledger', `${this.ledgerPath} ${detail}; remove it once no task uses the volume`);
|
|
669
|
+
let wire;
|
|
670
|
+
try {
|
|
671
|
+
wire = JSON.parse(text);
|
|
672
|
+
}
|
|
673
|
+
catch (error) {
|
|
674
|
+
return refused(`is not JSON: ${errorText(error)}`);
|
|
675
|
+
}
|
|
676
|
+
const ledger = parseLedger(wire);
|
|
677
|
+
return ledger === undefined ? refused("is not this module's ledger") : { ok: true, value: { ledger, text } };
|
|
678
|
+
}
|
|
679
|
+
/**
|
|
680
|
+
* The volume when it is mounted at the mountpoint, identified by its marker; else null. Its key
|
|
681
|
+
* names this attach: the root's and the marker's identity and birth, and the marker's change
|
|
682
|
+
* time, which nothing changes once it is written. The root's own times change with every lease.
|
|
683
|
+
*/
|
|
684
|
+
async observe() {
|
|
685
|
+
const markerPath = join(this.paths.mountpoint, MARKER_FILE);
|
|
686
|
+
const marker = await readFile(markerPath, 'utf8').catch(() => null);
|
|
687
|
+
if (marker?.trim() !== this.paths.volumeName) {
|
|
688
|
+
return null;
|
|
689
|
+
}
|
|
690
|
+
const [root, parent, mark] = await Promise.all([
|
|
691
|
+
stat(this.paths.mountpoint, { bigint: true }),
|
|
692
|
+
stat(join(this.paths.mountpoint, '..'), { bigint: true }),
|
|
693
|
+
lstat(markerPath, { bigint: true }),
|
|
694
|
+
]);
|
|
695
|
+
if (root.dev === parent.dev) {
|
|
696
|
+
return null;
|
|
697
|
+
}
|
|
698
|
+
const [fs, names] = await Promise.all([statfs(this.paths.mountpoint), readdir(this.paths.mountpoint)]);
|
|
699
|
+
return {
|
|
700
|
+
capacity: fs.blocks * fs.bsize,
|
|
701
|
+
key: [
|
|
702
|
+
`root ${root.dev}:${root.ino}:${root.birthtimeNs}`,
|
|
703
|
+
`marker ${mark.dev}:${mark.ino}:${mark.birthtimeNs}:${mark.ctimeNs}`,
|
|
704
|
+
].join(' '),
|
|
705
|
+
device: Number(root.dev),
|
|
706
|
+
leases: names.filter((name) => LEASE_NAME.test(name)),
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Under the lock: claim `own`, the caller's ending lease if any, and every dead lease no other
|
|
711
|
+
* caller is settling. A dead lease whose cached verdict still validates is named held and left
|
|
712
|
+
* unclaimed: that proof costs `lstat` calls, not a host-wide probe, so no caller waits on it.
|
|
713
|
+
*/
|
|
714
|
+
async claim(ledger, volume, owner, own) {
|
|
715
|
+
const held = [];
|
|
716
|
+
const candidates = [];
|
|
717
|
+
const claims = [...ledger.claims];
|
|
718
|
+
let verdicts = ledger.held;
|
|
719
|
+
for (const name of volume.leases) {
|
|
720
|
+
if ((name !== own && this.isAlive(leasePid(name))) || ledger.claims.some((claim) => claim.name === name)) {
|
|
721
|
+
continue;
|
|
722
|
+
}
|
|
723
|
+
const directory = join(this.paths.mountpoint, name);
|
|
724
|
+
const identity = await leaseIdentity(directory);
|
|
725
|
+
if (identity === null) {
|
|
726
|
+
continue;
|
|
727
|
+
}
|
|
728
|
+
const cached = verdicts.find((entry) => entry.lease === name);
|
|
729
|
+
if (cached !== undefined) {
|
|
730
|
+
const valid = await validateHeldVerdict(cached.verdict, heldContext(volume, name, identity));
|
|
731
|
+
if (valid.ok && valid.value) {
|
|
732
|
+
held.push({
|
|
733
|
+
name,
|
|
734
|
+
text: holdingText(directory, cached.verdict.holders.map((proof) => proof.description)),
|
|
735
|
+
});
|
|
736
|
+
continue;
|
|
737
|
+
}
|
|
738
|
+
// Changed or unreadable: the verdict goes, and the lease is probed afresh.
|
|
739
|
+
verdicts = verdicts.filter((entry) => entry !== cached);
|
|
740
|
+
}
|
|
741
|
+
claims.push({ name, identity, owner });
|
|
742
|
+
candidates.push({ name, directory, identity, own: name === own });
|
|
743
|
+
}
|
|
744
|
+
return { ledger: { ...ledger, claims, held: verdicts }, held, candidates };
|
|
745
|
+
}
|
|
746
|
+
/**
|
|
747
|
+
* Outside the lock: settle the leases this caller claimed. The processes every dead one still
|
|
748
|
+
* runs are stopped first, and only then is the pass's one inventory read, so no inventory older
|
|
749
|
+
* than a lease's settled census approves its removal. Anything attached from below a lease keeps
|
|
750
|
+
* it: an image backed by a file there would be left pinned to a deleted file, and detaching it
|
|
751
|
+
* here would race the owner that releases it under its own lease; a mount there is backed
|
|
752
|
+
* elsewhere — a cowshed workspace adopted from a fixture checkout, say — and `rm -r` would
|
|
753
|
+
* delete its contents through the mount. A kept lease gets a verdict to publish, when its
|
|
754
|
+
* holders have cheap lifetime keys. A lease nothing holds is removed once the lock confirms it
|
|
755
|
+
* is as claimed.
|
|
756
|
+
*/
|
|
757
|
+
async settle(op, volume, candidates) {
|
|
758
|
+
if (candidates.length === 0) {
|
|
759
|
+
return { ok: true, value: { held: [], reaped: [], verdicts: [] } };
|
|
760
|
+
}
|
|
761
|
+
const reaped = [];
|
|
762
|
+
for (const candidate of candidates) {
|
|
763
|
+
// Its task is gone, so what works in it is what that task left: stopped before the lease
|
|
764
|
+
// is removed, even where the removal is refused below. A task reaps its own (`reap`).
|
|
765
|
+
if (!candidate.own && (await readdir(candidate.directory)).length > 0) {
|
|
766
|
+
reaped.push(...(await this.processes.stopWorkingIn(candidate.directory)));
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
const inventory = await this.readInventory();
|
|
770
|
+
if (!inventory.ok) {
|
|
771
|
+
return inventory;
|
|
772
|
+
}
|
|
773
|
+
const held = [];
|
|
774
|
+
const verdicts = [];
|
|
775
|
+
const removable = [];
|
|
776
|
+
for (const candidate of candidates) {
|
|
777
|
+
const below = candidatesBelow(inventory.value, `${candidate.directory}/`);
|
|
778
|
+
if (below.length === 0) {
|
|
779
|
+
removable.push(candidate);
|
|
780
|
+
continue;
|
|
781
|
+
}
|
|
782
|
+
const by = below.map((holder) => holder.description);
|
|
783
|
+
held.push({ name: candidate.name, text: holdingText(candidate.directory, by) });
|
|
784
|
+
// No verdict for a holder without a cheap lifetime key: the next pass probes it again.
|
|
785
|
+
const captured = await captureHeldVerdict(heldContext(volume, candidate.name, candidate.identity), below);
|
|
786
|
+
if (captured.ok && captured.value !== undefined) {
|
|
787
|
+
verdicts.push({ lease: candidate.name, verdict: captured.value });
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
if (removable.length > 0) {
|
|
791
|
+
const confirmed = await this.locked(op, 'reclaim', async (ledger, current) => ({
|
|
792
|
+
ledger,
|
|
793
|
+
value: current?.key === volume.key ? await this.unchanged(ledger, op.owner, removable) : [],
|
|
794
|
+
}));
|
|
795
|
+
if (!confirmed.ok) {
|
|
796
|
+
return confirmed;
|
|
797
|
+
}
|
|
798
|
+
for (const candidate of confirmed.value) {
|
|
799
|
+
// Nothing holds the lease, so its directories may be made removable first: `rm -r` cannot
|
|
800
|
+
// empty a readonly one. Only this volume's device is changed (see prepareForRemoval).
|
|
801
|
+
await prepareForRemoval(candidate.directory, volume.device);
|
|
802
|
+
await rm(candidate.directory, { recursive: true, force: true });
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return { ok: true, value: { held, reaped, verdicts } };
|
|
806
|
+
}
|
|
807
|
+
/**
|
|
808
|
+
* Under the lock: the claimed leases still as claimed — this caller's claim on the same
|
|
809
|
+
* directory, and a task still gone. Any other is left for a fresh look, never removed.
|
|
810
|
+
*/
|
|
811
|
+
async unchanged(ledger, owner, candidates) {
|
|
812
|
+
const confirmed = [];
|
|
813
|
+
for (const candidate of candidates) {
|
|
814
|
+
const claimed = ledger.claims.some((claim) => claim.name === candidate.name && claim.identity === candidate.identity && claim.owner.token === owner.token);
|
|
815
|
+
if (claimed &&
|
|
816
|
+
(candidate.own || !this.isAlive(leasePid(candidate.name))) &&
|
|
817
|
+
(await leaseIdentity(candidate.directory)) === candidate.identity) {
|
|
818
|
+
confirmed.push(candidate);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
return confirmed;
|
|
822
|
+
}
|
|
823
|
+
/** Detach what earlier creators left, then attach and provision the volume. */
|
|
824
|
+
async provisionVolume() {
|
|
825
|
+
const strays = await this.reclaimStrays(this.holdersPass());
|
|
826
|
+
if (!strays.ok) {
|
|
827
|
+
return strays;
|
|
828
|
+
}
|
|
829
|
+
const attached = await this.commands.run('/usr/bin/hdiutil', ['attach', '-nomount', `ram://${this.sectors}`]);
|
|
830
|
+
if (attached.status !== 0) {
|
|
831
|
+
// A deadlined attach took its helper's process group down with it; one that attached
|
|
832
|
+
// regardless is a stray the next creator detaches.
|
|
833
|
+
return failed('hdiutil attach', commandText(attached));
|
|
834
|
+
}
|
|
835
|
+
const physical = /^(\/dev\/disk\d+)\b/.exec(attached.stdout)?.[1];
|
|
836
|
+
if (!physical) {
|
|
837
|
+
return failed('hdiutil attach', `no device in: ${attached.stdout.trim()}`);
|
|
838
|
+
}
|
|
839
|
+
const provisioned = await this.provision(physical);
|
|
840
|
+
if (provisioned.ok) {
|
|
841
|
+
return provisioned;
|
|
842
|
+
}
|
|
843
|
+
const detached = await this.commands.run('/usr/bin/hdiutil', ['detach', '-force', physical]);
|
|
844
|
+
// The detach takes every volume of the disk with it. What it leaves at the staging path is a bare
|
|
845
|
+
// directory on the host's Data volume: removed here, and already absent is done.
|
|
846
|
+
const detachFailure = detached.status === 0
|
|
847
|
+
? null
|
|
848
|
+
: `hdiutil detach -force ${physical} then failed (${commandText(detached)}), so it stays attached until the next creator reclaims it`;
|
|
849
|
+
const stagingFailure = await rmdir(this.paths.staging).then(() => null, (error) => errorCode(error) === 'ENOENT' ? null : `rmdir ${this.paths.staging} then failed (${errorText(error)})`);
|
|
850
|
+
const failures = [detachFailure, stagingFailure].filter((failure) => failure !== null);
|
|
851
|
+
return failures.length === 0
|
|
852
|
+
? provisioned
|
|
853
|
+
: failed(provisioned.error.step, [provisioned.error.detail, ...failures].join('; '));
|
|
854
|
+
}
|
|
855
|
+
/**
|
|
856
|
+
* Create the volume under its final name, mount it with `mount_apfs` at the private staging
|
|
857
|
+
* directory, and mark it there. Then `umount` that private mount and let `diskutil mount` place
|
|
858
|
+
* the volume at the mountpoint: the only call that does, and it runs after the marker, so the
|
|
859
|
+
* volume never reaches `/Volumes` unmarked. Nothing is renamed (see the module comment). The
|
|
860
|
+
* staging mount is the kernel's, so the kernel releases it: a Disk Arbitration unmount waits in
|
|
861
|
+
* its one queue behind its own probe of the fresh RAM disk (5.1 s for the `zfs` probe alone in
|
|
862
|
+
* `log stream`; 6.5–6.7 s per unmount against 64–69 ms for `umount`, measured 2026-10-15). A
|
|
863
|
+
* crash before the final mount leaves the volume attached at the staging directory or
|
|
864
|
+
* unmounted; the next creator's reclaim detaches either.
|
|
865
|
+
*/
|
|
866
|
+
async provision(physical) {
|
|
867
|
+
const container = await this.commands.run('/usr/sbin/diskutil', ['apfs', 'createContainer', physical]);
|
|
868
|
+
const containerDisk = operationDisk(container.stdout);
|
|
869
|
+
if (container.status !== 0 || !containerDisk) {
|
|
870
|
+
return failed('diskutil apfs createContainer', commandText(container));
|
|
871
|
+
}
|
|
872
|
+
const added = await this.commands.run('/usr/sbin/diskutil', [
|
|
873
|
+
'apfs',
|
|
874
|
+
'addVolume',
|
|
875
|
+
containerDisk,
|
|
876
|
+
'APFS',
|
|
877
|
+
this.paths.volumeName,
|
|
878
|
+
'-nomount',
|
|
879
|
+
]);
|
|
880
|
+
const volume = operationDisk(added.stdout);
|
|
881
|
+
if (added.status !== 0 || !volume) {
|
|
882
|
+
return failed('diskutil apfs addVolume', commandText(added));
|
|
883
|
+
}
|
|
884
|
+
const staging = this.paths.staging;
|
|
885
|
+
try {
|
|
886
|
+
await mkdir(staging, { recursive: true });
|
|
887
|
+
}
|
|
888
|
+
catch (error) {
|
|
889
|
+
return failed('staging directory', errorText(error));
|
|
890
|
+
}
|
|
891
|
+
// `noowners`: a fresh volume's root is owned by root, and the marker write is denied without it.
|
|
892
|
+
const mounted = await this.commands.run('/sbin/mount_apfs', ['-o', 'nobrowse,noowners', `/dev/${volume}`, staging]);
|
|
893
|
+
if (mounted.status !== 0) {
|
|
894
|
+
return failed('mount_apfs (staging)', commandText(mounted));
|
|
895
|
+
}
|
|
896
|
+
const atStaging = await this.expectMounted(volume, staging, 'mount_apfs (staging)');
|
|
897
|
+
if (!atStaging.ok) {
|
|
898
|
+
return atStaging;
|
|
899
|
+
}
|
|
900
|
+
try {
|
|
901
|
+
await writeFile(join(staging, MARKER_FILE), `${this.paths.volumeName}\n`);
|
|
902
|
+
}
|
|
903
|
+
catch (error) {
|
|
904
|
+
return failed('marker', errorText(error));
|
|
905
|
+
}
|
|
906
|
+
const unmounted = await this.commands.run('/sbin/umount', [staging]);
|
|
907
|
+
if (unmounted.status !== 0) {
|
|
908
|
+
return failed('umount (staging)', commandText(unmounted));
|
|
909
|
+
}
|
|
910
|
+
// No -mountPoint: DiskArbitration names the mountpoint after the volume, and appends " 1" when a
|
|
911
|
+
// /Volumes entry already holds the name.
|
|
912
|
+
const placing = await this.commands.run('/usr/sbin/diskutil', ['mount', '-mountOptions', 'nobrowse', volume]);
|
|
913
|
+
if (placing.status !== 0) {
|
|
914
|
+
return failed('diskutil mount', commandText(placing));
|
|
915
|
+
}
|
|
916
|
+
const placed = await this.expectMounted(volume, this.paths.mountpoint, 'diskutil mount');
|
|
917
|
+
if (!placed.ok) {
|
|
918
|
+
return placed;
|
|
919
|
+
}
|
|
920
|
+
try {
|
|
921
|
+
await rmdir(staging);
|
|
922
|
+
}
|
|
923
|
+
catch (error) {
|
|
924
|
+
return failed('staging directory', errorText(error));
|
|
925
|
+
}
|
|
926
|
+
return placed;
|
|
927
|
+
}
|
|
928
|
+
async expectMounted(volume, expected, step) {
|
|
929
|
+
const table = await this.commands.run('/sbin/mount', []);
|
|
930
|
+
if (table.status !== 0) {
|
|
931
|
+
return failed('mount (mount table)', commandText(table));
|
|
932
|
+
}
|
|
933
|
+
const point = new RegExp(`^/dev/${volume} on (.+) \\(`, 'm').exec(table.stdout)?.[1];
|
|
934
|
+
return point === expected
|
|
935
|
+
? { ok: true, value: undefined }
|
|
936
|
+
: failed(step, `${volume} mounted at ${point ?? 'nowhere'}, expected ${expected}`);
|
|
937
|
+
}
|
|
938
|
+
/** This module's RAM disks attached on the host: this user's, of this volume's exact size, mounted nowhere else. */
|
|
939
|
+
async attached() {
|
|
940
|
+
const info = await this.commands.run('/usr/bin/hdiutil', ['info']);
|
|
941
|
+
if (info.status !== 0) {
|
|
942
|
+
return failed('hdiutil info', commandText(info));
|
|
943
|
+
}
|
|
944
|
+
const ours = (point) => {
|
|
945
|
+
// DiskArbitration's " 1" suffix: a volume that found its name taken.
|
|
946
|
+
const name = point.replace(/ \d+$/, '');
|
|
947
|
+
// hdiutil lists a mount by the kernel's path; `staging` is written as that path, so it compares exactly.
|
|
948
|
+
return name === this.paths.mountpoint || name === this.paths.staging;
|
|
949
|
+
};
|
|
950
|
+
return {
|
|
951
|
+
ok: true,
|
|
952
|
+
value: ramDisksIn(info.stdout).filter((disk) => disk.user === this.paths.user && disk.sectors === this.sectors && disk.mountpoints.every(ours)),
|
|
953
|
+
};
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* Detach what earlier creators left attached: no marked volume is mounted, so every RAM disk of
|
|
957
|
+
* this volume is debris — unformatted, staged, or mounted without its marker. One carrying a
|
|
958
|
+
* lease, or with anything attached below it, is still in use and stops the creation instead.
|
|
959
|
+
*/
|
|
960
|
+
async reclaimStrays(holders) {
|
|
961
|
+
const strays = await this.attached();
|
|
962
|
+
if (!strays.ok) {
|
|
963
|
+
return strays;
|
|
964
|
+
}
|
|
965
|
+
for (const disk of strays.value) {
|
|
966
|
+
for (const point of disk.mountpoints) {
|
|
967
|
+
const leases = await readdir(point).then((names) => ({ ok: true, names: names.filter((name) => LEASE_NAME.test(name)) }), (error) => ({ ok: false, detail: errorText(error) }));
|
|
968
|
+
if (!leases.ok) {
|
|
969
|
+
return failed('reclaim', `${disk.physical} at ${point}: ${leases.detail}`);
|
|
970
|
+
}
|
|
971
|
+
if (leases.names.length > 0) {
|
|
972
|
+
return failed('reclaim', `${disk.physical} at ${point} has no marker but carries leases ${leases.names.join(', ')}; not detached`);
|
|
973
|
+
}
|
|
974
|
+
const held = await holders(`${point}/`);
|
|
975
|
+
if (!held.ok) {
|
|
976
|
+
return held;
|
|
977
|
+
}
|
|
978
|
+
if (held.value.length > 0) {
|
|
979
|
+
return failed('reclaim', holdingText(point, held.value));
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
const detached = await this.commands.run('/usr/bin/hdiutil', ['detach', '-force', disk.physical], STRAY_DETACH_DEADLINE_MS);
|
|
983
|
+
if (detached.status !== 0) {
|
|
984
|
+
return failed(`hdiutil detach (stray ${disk.physical})`, commandText(detached));
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
return { ok: true, value: undefined };
|
|
988
|
+
}
|
|
989
|
+
/**
|
|
990
|
+
* With this caller's detach claim published, which no caller takes a lease past. Every lease is
|
|
991
|
+
* gone, so nothing below the volume should be attached; whatever is would lose its backing file
|
|
992
|
+
* to the detach. The lock confirms the volume, still empty, and the claim before it goes.
|
|
993
|
+
*/
|
|
994
|
+
async detach(op, volume) {
|
|
995
|
+
const inventory = await this.readInventory();
|
|
996
|
+
if (!inventory.ok) {
|
|
997
|
+
return inventory;
|
|
998
|
+
}
|
|
999
|
+
const held = candidatesBelow(inventory.value, `${this.paths.mountpoint}/`).map((holder) => holder.description);
|
|
1000
|
+
if (held.length > 0) {
|
|
1001
|
+
return failed('detach', holdingText(this.paths.mountpoint, held));
|
|
1002
|
+
}
|
|
1003
|
+
const disks = await this.attached();
|
|
1004
|
+
if (!disks.ok) {
|
|
1005
|
+
return disks;
|
|
1006
|
+
}
|
|
1007
|
+
const disk = disks.value.find((candidate) => candidate.mountpoints.includes(this.paths.mountpoint));
|
|
1008
|
+
if (!disk) {
|
|
1009
|
+
return failed('detach', `hdiutil info lists no RAM disk of ${this.paths.user} with ${this.sectors} sectors mounted at ${this.paths.mountpoint}`);
|
|
1010
|
+
}
|
|
1011
|
+
const unchanged = await this.locked(op, 'detach', async (ledger, current) => ({
|
|
1012
|
+
ledger,
|
|
1013
|
+
value: ledger.lifecycle?.owner.token === op.owner.token &&
|
|
1014
|
+
current?.key === volume.key &&
|
|
1015
|
+
current.leases.length === 0,
|
|
1016
|
+
}));
|
|
1017
|
+
if (!unchanged.ok) {
|
|
1018
|
+
return unchanged;
|
|
1019
|
+
}
|
|
1020
|
+
if (!unchanged.value) {
|
|
1021
|
+
// A lease or another attach arrived despite the claim: the volume stays for it.
|
|
1022
|
+
return { ok: true, value: undefined };
|
|
1023
|
+
}
|
|
1024
|
+
let detached = await this.commands.run('/usr/bin/hdiutil', ['detach', disk.physical]);
|
|
1025
|
+
if (detached.status !== 0) {
|
|
1026
|
+
detached = await this.commands.run('/usr/bin/hdiutil', ['detach', '-force', disk.physical]);
|
|
1027
|
+
}
|
|
1028
|
+
if (detached.status !== 0) {
|
|
1029
|
+
return failed('hdiutil detach', commandText(detached));
|
|
1030
|
+
}
|
|
1031
|
+
// DiskArbitration removes the /Volumes directory it created when the volume goes.
|
|
1032
|
+
return { ok: true, value: undefined };
|
|
1033
|
+
}
|
|
1034
|
+
/** The host's attachments as one pass reads them: one `ioreg` run and one `mount` run. */
|
|
1035
|
+
async readInventory() {
|
|
1036
|
+
const registry = await this.commands.run('/usr/sbin/ioreg', ['-r', '-c', 'AppleDiskImageDevice', '-l', '-w0']);
|
|
1037
|
+
if (registry.status !== 0) {
|
|
1038
|
+
return failed('ioreg (attached disk images)', commandText(registry));
|
|
1039
|
+
}
|
|
1040
|
+
const mounts = await this.commands.run('/sbin/mount', []);
|
|
1041
|
+
if (mounts.status !== 0) {
|
|
1042
|
+
return failed('mount (mount table)', commandText(mounts));
|
|
1043
|
+
}
|
|
1044
|
+
return { ok: true, value: { images: diskImages(registry.stdout), mounts: mountPoints(mounts.stdout) } };
|
|
1045
|
+
}
|
|
1046
|
+
/**
|
|
1047
|
+
* The holder check of one creation pass. The first check reads the attachments and every later
|
|
1048
|
+
* check in the pass reads the same ones, so a pass runs `ioreg` and `mount` once however many
|
|
1049
|
+
* disks it visits. What is attached from below a prefix: images whose backing file is there,
|
|
1050
|
+
* and mounts. `hdiutil info` omits attached images while another image attaches or detaches
|
|
1051
|
+
* (measured by cowshed's scratch-root sweep), and tests attach images concurrently.
|
|
1052
|
+
*/
|
|
1053
|
+
holdersPass() {
|
|
1054
|
+
let inventory;
|
|
1055
|
+
return async (prefix) => {
|
|
1056
|
+
inventory ??= this.readInventory();
|
|
1057
|
+
const read = await inventory;
|
|
1058
|
+
return read.ok
|
|
1059
|
+
? { ok: true, value: candidatesBelow(read.value, prefix).map((holder) => holder.description) }
|
|
1060
|
+
: read;
|
|
1061
|
+
};
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
/** What is attached from below `prefix` in `inventory`: images whose backing file is there, and mounts. */
|
|
1065
|
+
function candidatesBelow(inventory, prefix) {
|
|
1066
|
+
return [
|
|
1067
|
+
...inventory.images
|
|
1068
|
+
.filter((entry) => entry.image.startsWith(prefix))
|
|
1069
|
+
.map((entry) => ({
|
|
1070
|
+
kind: 'image',
|
|
1071
|
+
imagePath: entry.image,
|
|
1072
|
+
diskPath: `/dev/${entry.disk}`,
|
|
1073
|
+
description: `${entry.image} (/dev/${entry.disk})`,
|
|
1074
|
+
})),
|
|
1075
|
+
...inventory.mounts
|
|
1076
|
+
.filter((point) => point.startsWith(prefix))
|
|
1077
|
+
.map((point) => ({ kind: 'mount', mountpoint: point, description: `${point} (mounted)` })),
|
|
1078
|
+
];
|
|
1079
|
+
}
|
|
1080
|
+
/** The pid a lease directory is named after: whether its task is alive. */
|
|
1081
|
+
function leasePid(name) {
|
|
1082
|
+
return Number(LEASE_NAME.exec(name)?.[1]);
|
|
1083
|
+
}
|
|
1084
|
+
/** A lease directory's identity: device, inode and birth, which a directory made again at its path does not share. */
|
|
1085
|
+
async function leaseIdentity(directory) {
|
|
1086
|
+
try {
|
|
1087
|
+
const info = await lstat(directory, { bigint: true });
|
|
1088
|
+
return `${info.dev}:${info.ino}:${info.birthtimeNs}`;
|
|
1089
|
+
}
|
|
1090
|
+
catch (error) {
|
|
1091
|
+
if (errorCode(error) === 'ENOENT') {
|
|
1092
|
+
return null;
|
|
1093
|
+
}
|
|
1094
|
+
throw error;
|
|
1095
|
+
}
|
|
1096
|
+
}
|
|
1097
|
+
/** What a held verdict is bound to: this attach of the volume, and this lease directory on it. */
|
|
1098
|
+
function heldContext(volume, name, identity) {
|
|
1099
|
+
return { volumeKey: volume.key, leaseKey: `${name} ${identity}`, ramDevice: String(volume.device) };
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* The ledger with the claims of dead owners voided, and the verdicts kept only for leases still on
|
|
1103
|
+
* this attach of the volume. A claim's owner is judged as a lease's is, by its pid.
|
|
1104
|
+
*/
|
|
1105
|
+
function prune(ledger, volume, isAlive) {
|
|
1106
|
+
return {
|
|
1107
|
+
lifecycle: ledger.lifecycle !== null && isAlive(ledger.lifecycle.owner.pid) ? ledger.lifecycle : null,
|
|
1108
|
+
claims: ledger.claims.filter((claim) => isAlive(claim.owner.pid)),
|
|
1109
|
+
held: volume === null
|
|
1110
|
+
? []
|
|
1111
|
+
: ledger.held.filter((entry) => entry.verdict.context.volumeKey === volume.key && volume.leases.includes(entry.lease)),
|
|
1112
|
+
};
|
|
1113
|
+
}
|
|
1114
|
+
function withoutClaimsOf(ledger, owner) {
|
|
1115
|
+
return {
|
|
1116
|
+
lifecycle: ledger.lifecycle?.owner.token === owner.token ? null : ledger.lifecycle,
|
|
1117
|
+
claims: ledger.claims.filter((claim) => claim.owner.token !== owner.token),
|
|
1118
|
+
held: ledger.held,
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
/** The ledger once a caller has settled its claims: they are dropped, and its verdicts replace older ones. */
|
|
1122
|
+
function settledLedger(ledger, volume, owner, verdicts) {
|
|
1123
|
+
const current = verdicts.filter((entry) => volume !== null && entry.verdict.context.volumeKey === volume.key && volume.leases.includes(entry.lease));
|
|
1124
|
+
const settled = withoutClaimsOf(ledger, owner);
|
|
1125
|
+
return {
|
|
1126
|
+
...settled,
|
|
1127
|
+
held: [...settled.held.filter((entry) => !current.some((fresh) => fresh.lease === entry.lease)), ...current],
|
|
1128
|
+
};
|
|
1129
|
+
}
|
|
1130
|
+
/** The ledger's wire form, version 1. */
|
|
1131
|
+
function ledgerText(ledger) {
|
|
1132
|
+
return `${JSON.stringify({ version: 1, lifecycle: ledger.lifecycle, claims: ledger.claims, held: ledger.held })}\n`;
|
|
1133
|
+
}
|
|
1134
|
+
/** Publish the ledger in one rename, so no reader, and no crash, ever leaves half of it. */
|
|
1135
|
+
async function publishLedger(path, text) {
|
|
1136
|
+
const staged = `${path}.${randomUUID()}`;
|
|
1137
|
+
try {
|
|
1138
|
+
await writeFile(staged, text, { flag: 'wx', mode: 0o600 });
|
|
1139
|
+
await rename(staged, path);
|
|
1140
|
+
}
|
|
1141
|
+
catch (error) {
|
|
1142
|
+
await rm(staged, { force: true }).catch((cleanup) => {
|
|
1143
|
+
throw new AggregateError([error, cleanup], `${staged}: ${errorText(error)}; removing it failed: ${errorText(cleanup)}`);
|
|
1144
|
+
});
|
|
1145
|
+
throw error;
|
|
1146
|
+
}
|
|
1147
|
+
}
|
|
1148
|
+
/**
|
|
1149
|
+
* The ledger in its wire form; undefined when it is not one. A malformed held verdict is dropped,
|
|
1150
|
+
* so its lease is probed afresh; it never stands for a lease nothing holds.
|
|
1151
|
+
*/
|
|
1152
|
+
function parseLedger(value) {
|
|
1153
|
+
if (!isRecord(value) || value.version !== 1) {
|
|
1154
|
+
return undefined;
|
|
1155
|
+
}
|
|
1156
|
+
const { lifecycle: lifecycleWire, claims: claimsWire, held: heldWire } = value;
|
|
1157
|
+
if (!Array.isArray(claimsWire) || !Array.isArray(heldWire)) {
|
|
1158
|
+
return undefined;
|
|
1159
|
+
}
|
|
1160
|
+
let lifecycle = null;
|
|
1161
|
+
if (lifecycleWire !== null) {
|
|
1162
|
+
if (!isRecord(lifecycleWire)) {
|
|
1163
|
+
return undefined;
|
|
1164
|
+
}
|
|
1165
|
+
const { kind } = lifecycleWire;
|
|
1166
|
+
const owner = parseOwner(lifecycleWire.owner);
|
|
1167
|
+
if ((kind !== 'create' && kind !== 'detach') || owner === undefined) {
|
|
1168
|
+
return undefined;
|
|
1169
|
+
}
|
|
1170
|
+
lifecycle = { kind, owner };
|
|
1171
|
+
}
|
|
1172
|
+
const claims = [];
|
|
1173
|
+
for (const entry of claimsWire) {
|
|
1174
|
+
if (!isRecord(entry)) {
|
|
1175
|
+
return undefined;
|
|
1176
|
+
}
|
|
1177
|
+
const { name, identity } = entry;
|
|
1178
|
+
const owner = parseOwner(entry.owner);
|
|
1179
|
+
if (typeof name !== 'string' || !LEASE_NAME.test(name) || typeof identity !== 'string' || owner === undefined) {
|
|
1180
|
+
return undefined;
|
|
1181
|
+
}
|
|
1182
|
+
claims.push({ name, identity, owner });
|
|
1183
|
+
}
|
|
1184
|
+
const held = [];
|
|
1185
|
+
for (const entry of heldWire) {
|
|
1186
|
+
if (!isRecord(entry)) {
|
|
1187
|
+
continue;
|
|
1188
|
+
}
|
|
1189
|
+
const { lease } = entry;
|
|
1190
|
+
const verdict = parseHeldVerdict(entry.verdict);
|
|
1191
|
+
if (typeof lease === 'string' && verdict !== undefined) {
|
|
1192
|
+
held.push({ lease, verdict });
|
|
1193
|
+
}
|
|
1194
|
+
}
|
|
1195
|
+
return { lifecycle, claims, held };
|
|
1196
|
+
}
|
|
1197
|
+
function parseOwner(value) {
|
|
1198
|
+
if (!isRecord(value)) {
|
|
1199
|
+
return undefined;
|
|
1200
|
+
}
|
|
1201
|
+
const { pid, token } = value;
|
|
1202
|
+
return typeof pid === 'number' && Number.isInteger(pid) && pid > 0 && typeof token === 'string'
|
|
1203
|
+
? { pid, token }
|
|
1204
|
+
: undefined;
|
|
1205
|
+
}
|
|
1206
|
+
/** `directory` stays, with the volume under it: something attached below it still reads its files. */
|
|
1207
|
+
function holdingText(directory, holders) {
|
|
1208
|
+
return `${directory} is kept: ${holders.join(', ')} still attached from below it; detach through its owner`;
|
|
1209
|
+
}
|
|
1210
|
+
/** `primary` stays the error; what its cleanup then failed to undo is named after it, not dropped. */
|
|
1211
|
+
function alsoFailed(primary, cleanup) {
|
|
1212
|
+
if (cleanup.length === 0) {
|
|
1213
|
+
return primary;
|
|
1214
|
+
}
|
|
1215
|
+
const after = `cleanup then failed: ${cleanup.map(describeRamTempError).join('; ')}`;
|
|
1216
|
+
switch (primary.kind) {
|
|
1217
|
+
case 'provision-failed':
|
|
1218
|
+
return { ...primary, detail: `${primary.detail}; ${after}` };
|
|
1219
|
+
case 'volume-full':
|
|
1220
|
+
return { kind: 'provision-failed', step: 'cleanup', detail: `${describeRamTempError(primary)}; ${after}` };
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
/**
|
|
1224
|
+
* Give the owner `rwx` on `directory` and on every directory below it, keeping every other mode
|
|
1225
|
+
* bit, so `rm -r` can empty them. Only directories are changed: a link is never descended
|
|
1226
|
+
* (`withFileTypes` reports it as a link), and each directory is opened `O_NOFOLLOW` and changed
|
|
1227
|
+
* through that handle, never by its path. A directory on another device than `device` is refused
|
|
1228
|
+
* before it is changed. Node has no `openat`, so entries are named by path; the walk checks each
|
|
1229
|
+
* directory's identity again after its `readdir`, and a directory swapped under its path stops it.
|
|
1230
|
+
*/
|
|
1231
|
+
async function prepareForRemoval(directory, device) {
|
|
1232
|
+
let handle;
|
|
1233
|
+
try {
|
|
1234
|
+
handle = await open(directory, constants.O_RDONLY | constants.O_DIRECTORY | constants.O_NOFOLLOW);
|
|
1235
|
+
}
|
|
1236
|
+
catch (error) {
|
|
1237
|
+
// Already gone: `rm -f` ignores that too.
|
|
1238
|
+
if (errorCode(error) === 'ENOENT') {
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
throw error;
|
|
1242
|
+
}
|
|
1243
|
+
try {
|
|
1244
|
+
const info = await handle.stat();
|
|
1245
|
+
if (info.dev !== device) {
|
|
1246
|
+
throw new Error(`${directory} is on device ${info.dev}, not the lease's device ${device}; not changed`);
|
|
1247
|
+
}
|
|
1248
|
+
if ((info.mode & 0o700) !== 0o700) {
|
|
1249
|
+
await handle.chmod((info.mode & 0o7777) | 0o700).catch((error) => {
|
|
1250
|
+
throw new Error(`${directory}: chmod u+rwx failed: ${errorText(error)}`);
|
|
1251
|
+
});
|
|
1252
|
+
}
|
|
1253
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
1254
|
+
const named = await lstat(directory);
|
|
1255
|
+
if (named.dev !== info.dev || named.ino !== info.ino) {
|
|
1256
|
+
throw new Error(`${directory} was replaced while it was walked; not changed further`);
|
|
1257
|
+
}
|
|
1258
|
+
for (const entry of entries) {
|
|
1259
|
+
if (entry.isDirectory()) {
|
|
1260
|
+
await prepareForRemoval(join(directory, entry.name), device);
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
}
|
|
1264
|
+
finally {
|
|
1265
|
+
await handle.close();
|
|
1266
|
+
}
|
|
1267
|
+
}
|
|
1268
|
+
/**
|
|
1269
|
+
* Every disk image in `ioreg -r -c AppleDiskImageDevice -l -w0` output. Each device starts a
|
|
1270
|
+
* `+-o AppleDiskImageDevice` block; the first `BSD Name` below it is the image's whole disk.
|
|
1271
|
+
*/
|
|
1272
|
+
function diskImages(registry) {
|
|
1273
|
+
const images = [];
|
|
1274
|
+
for (const block of registry.split(/^\+-o AppleDiskImageDevice/m)) {
|
|
1275
|
+
const url = /"DiskImageURL" = "file:\/\/([^"]+)"/.exec(block)?.[1];
|
|
1276
|
+
const disk = /"BSD Name" = "(disk\d+)"/.exec(block)?.[1];
|
|
1277
|
+
if (url !== undefined && disk !== undefined) {
|
|
1278
|
+
images.push({ image: decodeURIComponent(url), disk });
|
|
1279
|
+
}
|
|
1280
|
+
}
|
|
1281
|
+
return images;
|
|
1282
|
+
}
|
|
1283
|
+
/** Mount points in `mount` output (`<device> on <path> (<options>)`). */
|
|
1284
|
+
function mountPoints(table) {
|
|
1285
|
+
const points = [];
|
|
1286
|
+
for (const line of table.split('\n')) {
|
|
1287
|
+
const point = /^\S+ on (.+) \([^)]*\)$/.exec(line)?.[1];
|
|
1288
|
+
if (point !== undefined) {
|
|
1289
|
+
points.push(point);
|
|
1290
|
+
}
|
|
1291
|
+
}
|
|
1292
|
+
return points;
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Every image in `ioreg` output whose `DiskImageURL` names a file under `prefix`, as
|
|
1296
|
+
* `<backing file> (/dev/diskN)`.
|
|
1297
|
+
*/
|
|
1298
|
+
export function imagesBackedUnder(registry, prefix) {
|
|
1299
|
+
return diskImages(registry)
|
|
1300
|
+
.filter((entry) => entry.image.startsWith(prefix))
|
|
1301
|
+
.map((entry) => `${entry.image} (/dev/${entry.disk})`);
|
|
1302
|
+
}
|
|
1303
|
+
/** Mount points in `mount` output (`<device> on <path> (<options>)`) that lie under `prefix`. */
|
|
1304
|
+
export function mountsBelow(table, prefix) {
|
|
1305
|
+
return mountPoints(table).filter((point) => point.startsWith(prefix));
|
|
1306
|
+
}
|
|
1307
|
+
/** The disk `diskutil apfs` reports it created: "Disk from APFS operation: disk271". */
|
|
1308
|
+
export function operationDisk(stdout) {
|
|
1309
|
+
return /^Disk from APFS operation: (disk\d+(?:s\d+)?)$/m.exec(stdout)?.[1] ?? null;
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* The RAM disks in `hdiutil info` output. Images are separated by a row of `=`; a RAM disk's
|
|
1313
|
+
* `image-path` is `ram://<sectors>`, and its entity rows are `<dev>\t<content hint>\t<mountpoint>`,
|
|
1314
|
+
* the whole disk first.
|
|
1315
|
+
*/
|
|
1316
|
+
export function ramDisksIn(info) {
|
|
1317
|
+
const disks = [];
|
|
1318
|
+
for (const block of info.split(/^=+$/m)) {
|
|
1319
|
+
const sectors = /^image-path\s*:\s*ram:\/\/(\d+)\s*$/m.exec(block)?.[1];
|
|
1320
|
+
const user = /^mounting user\s*:\s*(\S+)\s*$/m.exec(block)?.[1];
|
|
1321
|
+
const entities = [...block.matchAll(/^(\/dev\/disk\d+(?:s\d+)*)\t[^\t\n]*(?:\t([^\n]*))?$/gm)];
|
|
1322
|
+
const physical = entities[0]?.[1];
|
|
1323
|
+
if (sectors === undefined || user === undefined || physical === undefined) {
|
|
1324
|
+
continue;
|
|
1325
|
+
}
|
|
1326
|
+
const mountpoints = entities.flatMap((entity) => {
|
|
1327
|
+
const point = entity[2]?.trimEnd();
|
|
1328
|
+
return point ? [point] : [];
|
|
1329
|
+
});
|
|
1330
|
+
disks.push({ physical, sectors: Number(sectors), user, mountpoints });
|
|
1331
|
+
}
|
|
1332
|
+
return disks;
|
|
1333
|
+
}
|
|
1334
|
+
/**
|
|
1335
|
+
* A disk child that DiskArbitration never answers would wedge the task, and every task queued
|
|
1336
|
+
* behind the lock, forever; measured: `diskutil mount` hung past 60 s while the host's
|
|
1337
|
+
* DiskArbitration was saturated. Cowshed bounds its disk children the same way.
|
|
1338
|
+
*/
|
|
1339
|
+
const HOST_COMMAND_DEADLINE_MS = 120_000;
|
|
1340
|
+
/**
|
|
1341
|
+
* How long one creator waits on a stray's detach before it reports the stray by name and
|
|
1342
|
+
* gives the create claim back. A stray detach that makes no progress is DiskArbitration's
|
|
1343
|
+
* wedge, not this volume's: at 120 s a chain of creators each waited the full bound on the
|
|
1344
|
+
* same stray and the claim never moved. The detach itself is never killed (below); the
|
|
1345
|
+
* bound is on the wait, and the next creator finds the stray gone or names it again.
|
|
1346
|
+
*/
|
|
1347
|
+
const STRAY_DETACH_DEADLINE_MS = 30_000;
|
|
1348
|
+
/** Disk tools delegate their lease and process lifetime to cowshed, with one image policy. */
|
|
1349
|
+
export function diskCommand(file, args, deadlineMs) {
|
|
1350
|
+
const program = basename(file);
|
|
1351
|
+
if (program !== 'hdiutil' && program !== 'diskutil') {
|
|
1352
|
+
return null;
|
|
1353
|
+
}
|
|
1354
|
+
// Match SystemCommandRunner's protected-image policy, including its case-sensitive verbs.
|
|
1355
|
+
const image = program === 'hdiutil' || args[0] === 'image' || args[0] === 'eject';
|
|
1356
|
+
return {
|
|
1357
|
+
args: image
|
|
1358
|
+
? ['disk-command', program, '--', ...args]
|
|
1359
|
+
: ['disk-command', program, '--deadline-ms', String(deadlineMs), '--', ...args],
|
|
1360
|
+
lifetime: image ? 'stop-wait' : 'facade-owned',
|
|
1361
|
+
};
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* The variable naming the cowshed executable that runs `cowshed disk-command`. Unset, it is the installed `cowshed`
|
|
1365
|
+
* on PATH. A repository that tests its own cowshed sets it, on the targets that test it, to that checkout's build:
|
|
1366
|
+
* an installed cowshed that predates a command its own checkout added would fail the very code under test.
|
|
1367
|
+
*/
|
|
1368
|
+
export const COWSHED_EXECUTABLE_ENV = 'COWSHED_EXECUTABLE';
|
|
1369
|
+
/** Whether this process already said the cowshed gateway is not there to lease disk commands. */
|
|
1370
|
+
let gatewayAbsenceSaid = false;
|
|
1371
|
+
/**
|
|
1372
|
+
* How long this process stops asking a gateway that predates leases before it asks again: each
|
|
1373
|
+
* ask costs that gateway's 2 s of silence, and `cowshed setup` may replace it meanwhile. A gateway
|
|
1374
|
+
* that predates only `alone` leases is asked shared for as long.
|
|
1375
|
+
*/
|
|
1376
|
+
const PREDATES_RECHECK_MS = 60_000;
|
|
1377
|
+
/** When this process last found the gateway at each control socket predating leases, or `alone` ones. */
|
|
1378
|
+
const predatesSeen = new Map();
|
|
1379
|
+
/**
|
|
1380
|
+
* hdiutil and diskutil delegate process lifetime and disk leasing to cowshed: safe commands
|
|
1381
|
+
* pass their total budget and wait for the facade's actual exit after child reap. Only image
|
|
1382
|
+
* commands have a caller stop-wait deadline; killing an attach or detach can leak its kernel
|
|
1383
|
+
* device until reboot. Never acquire a second lease around the facade. Other leased helpers
|
|
1384
|
+
* take their lease here. Recorded lease waits ask for `alone`; an older gateway is asked
|
|
1385
|
+
* shared at once, and for a minute after.
|
|
1386
|
+
*/
|
|
1387
|
+
export function createHostCommands(defaultDeadlineMs = HOST_COMMAND_DEADLINE_MS, gatewaySocket = GATEWAY_SOCKET, wantedShare = recordedShare(), cowshed = process.env[COWSHED_EXECUTABLE_ENV] ?? 'cowshed') {
|
|
1388
|
+
return {
|
|
1389
|
+
async run(file, args, callDeadlineMs) {
|
|
1390
|
+
const deadlineMs = callDeadlineMs ?? defaultDeadlineMs;
|
|
1391
|
+
const disk = diskCommand(file, args, deadlineMs);
|
|
1392
|
+
if (disk !== null) {
|
|
1393
|
+
return runBounded(cowshed, disk.args, deadlineMs, disk.lifetime);
|
|
1394
|
+
}
|
|
1395
|
+
const diskClass = diskClassOf(file);
|
|
1396
|
+
const seen = predatesSeen.get(gatewaySocket);
|
|
1397
|
+
const predates = seen !== undefined && Date.now() - seen.at < PREDATES_RECHECK_MS ? seen.what : null;
|
|
1398
|
+
if (diskClass === null || predates === 'leases') {
|
|
1399
|
+
return runBounded(file, args, deadlineMs);
|
|
1400
|
+
}
|
|
1401
|
+
const command = [file, ...args].join(' ');
|
|
1402
|
+
const bounds = { ackMs: 2_000, afterCloseMs: 5_000, grantMs: deadlineMs };
|
|
1403
|
+
const share = predates === 'alone' ? 'shared' : wantedShare;
|
|
1404
|
+
let lease = await takeDiskLease(gatewaySocket, diskClass, command, bounds, share);
|
|
1405
|
+
if (!lease.granted && lease.cause === 'predates-field' && share === 'alone') {
|
|
1406
|
+
predatesSeen.set(gatewaySocket, { at: Date.now(), what: 'alone' });
|
|
1407
|
+
lease = await takeDiskLease(gatewaySocket, diskClass, command, bounds, 'shared');
|
|
1408
|
+
}
|
|
1409
|
+
if (!lease.granted) {
|
|
1410
|
+
if (lease.cause === 'predates') {
|
|
1411
|
+
predatesSeen.set(gatewaySocket, { at: Date.now(), what: 'leases' });
|
|
1412
|
+
}
|
|
1413
|
+
if (!(lease.cause === 'absent' && gatewayAbsenceSaid)) {
|
|
1414
|
+
gatewayAbsenceSaid ||= lease.cause === 'absent';
|
|
1415
|
+
process.stderr.write(`RAM temp volume: ${file} runs without cowshed's disk lease: ${lease.reason}\n`);
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
try {
|
|
1419
|
+
return await runBounded(file, args, deadlineMs);
|
|
1420
|
+
}
|
|
1421
|
+
finally {
|
|
1422
|
+
if (lease.granted) {
|
|
1423
|
+
lease.release();
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
},
|
|
1427
|
+
};
|
|
1428
|
+
}
|
|
1429
|
+
function runBounded(file, args, deadlineMs, lifetime = 'kill') {
|
|
1430
|
+
// `Promise.withResolvers` would read better but needs lib es2024; this package inherits lib
|
|
1431
|
+
// es2022 from tsconfig.base.json.
|
|
1432
|
+
let resolve;
|
|
1433
|
+
const promise = new Promise((settled) => {
|
|
1434
|
+
resolve = settled;
|
|
1435
|
+
});
|
|
1436
|
+
// Ordinary helpers own their process group. The facade alone owns disk-child termination,
|
|
1437
|
+
// reap and lease custody; a wrapper's exit or signal is not the disk child's exit or signal.
|
|
1438
|
+
const child = spawn(file, [...args], { detached: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
1439
|
+
const stdout = [];
|
|
1440
|
+
const stderr = [];
|
|
1441
|
+
child.stdout.on('data', (chunk) => stdout.push(chunk));
|
|
1442
|
+
child.stderr.on('data', (chunk) => stderr.push(chunk));
|
|
1443
|
+
const text = (chunks) => Buffer.concat(chunks).toString('utf8');
|
|
1444
|
+
// Safe disk commands have no Node timer: the CLI owns the total admission + child budget,
|
|
1445
|
+
// kills only its owned safe child, and returns after reap. Protected image output is already
|
|
1446
|
+
// file-backed in cowshed, so only that caller may close its pipes and stop waiting.
|
|
1447
|
+
const timer = lifetime === 'facade-owned'
|
|
1448
|
+
? undefined
|
|
1449
|
+
: setTimeout(() => {
|
|
1450
|
+
if (lifetime === 'stop-wait') {
|
|
1451
|
+
child.stdout.destroy();
|
|
1452
|
+
child.stderr.destroy();
|
|
1453
|
+
child.unref();
|
|
1454
|
+
// A piped child's streams are sockets; only a socket can be unref'd.
|
|
1455
|
+
if (child.stdout instanceof Socket)
|
|
1456
|
+
child.stdout.unref();
|
|
1457
|
+
if (child.stderr instanceof Socket)
|
|
1458
|
+
child.stderr.unref();
|
|
1459
|
+
resolve({
|
|
1460
|
+
status: 1,
|
|
1461
|
+
stdout: text(stdout),
|
|
1462
|
+
stderr: [
|
|
1463
|
+
text(stderr),
|
|
1464
|
+
`no answer within ${deadlineMs} ms; cowshed command ${child.pid ?? 'unknown'} left running, not killed: disk-image operations must finish to release their kernel devices`,
|
|
1465
|
+
]
|
|
1466
|
+
.filter(Boolean)
|
|
1467
|
+
.join('\n'),
|
|
1468
|
+
});
|
|
1469
|
+
return;
|
|
1470
|
+
}
|
|
1471
|
+
resolve({
|
|
1472
|
+
status: 1,
|
|
1473
|
+
stdout: text(stdout),
|
|
1474
|
+
stderr: `no answer within ${deadlineMs} ms; ${killGroup(child)}`,
|
|
1475
|
+
});
|
|
1476
|
+
}, deadlineMs);
|
|
1477
|
+
child.on('error', (error) => {
|
|
1478
|
+
clearTimeout(timer);
|
|
1479
|
+
resolve({ status: 1, stdout: text(stdout), stderr: error.message });
|
|
1480
|
+
});
|
|
1481
|
+
child.on('close', (code, signal) => {
|
|
1482
|
+
clearTimeout(timer);
|
|
1483
|
+
const error = text(stderr);
|
|
1484
|
+
resolve({
|
|
1485
|
+
status: code ?? 1,
|
|
1486
|
+
stdout: text(stdout),
|
|
1487
|
+
stderr: signal === null ? error : [error, `killed by ${signal}`].filter(Boolean).join('\n'),
|
|
1488
|
+
});
|
|
1489
|
+
});
|
|
1490
|
+
return promise;
|
|
1491
|
+
}
|
|
1492
|
+
/** SIGKILL the child's whole process group; what happened, for the error. */
|
|
1493
|
+
function killGroup(child) {
|
|
1494
|
+
if (child.pid === undefined) {
|
|
1495
|
+
return 'never started';
|
|
1496
|
+
}
|
|
1497
|
+
try {
|
|
1498
|
+
process.kill(-child.pid, 'SIGKILL');
|
|
1499
|
+
return `killed process group ${child.pid}`;
|
|
1500
|
+
}
|
|
1501
|
+
catch (error) {
|
|
1502
|
+
return `killing process group ${child.pid} failed: ${errorText(error)}`;
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
function processIsAlive(pid) {
|
|
1506
|
+
try {
|
|
1507
|
+
process.kill(pid, 0);
|
|
1508
|
+
return true;
|
|
1509
|
+
}
|
|
1510
|
+
catch (error) {
|
|
1511
|
+
// EPERM: alive, owned by someone else.
|
|
1512
|
+
return errorCode(error) === 'EPERM';
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
function failed(step, detail) {
|
|
1516
|
+
return { ok: false, error: { kind: 'provision-failed', step, detail } };
|
|
1517
|
+
}
|
|
1518
|
+
function commandText(output) {
|
|
1519
|
+
return `exit ${output.status}: ${(output.stderr || output.stdout).trim()}`;
|
|
1520
|
+
}
|
|
1521
|
+
function errorCode(error) {
|
|
1522
|
+
return typeof error === 'object' && error !== null && 'code' in error && typeof error.code === 'string'
|
|
1523
|
+
? error.code
|
|
1524
|
+
: undefined;
|
|
1525
|
+
}
|
|
1526
|
+
function errorText(error) {
|
|
1527
|
+
return error instanceof Error ? error.message : String(error);
|
|
1528
|
+
}
|