@native-sdk/cli 0.5.0 → 0.5.2
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/build/app.zig +198 -13
- package/build/ts_run.mjs +58 -19
- package/build.zig +42 -0
- package/package.json +13 -9
- package/packages/core/package-lock.json +4 -3
- package/packages/core/package.json +3 -2
- package/packages/core/src/typed_ast.ts +11 -1
- package/skill-data/native-ui/SKILL.md +2 -2
- package/src/platform/types.zig +9 -0
- package/src/platform/windows/webview2_host.cpp +24 -0
- package/src/primitives/canvas/layout_audit.zig +5 -5
- package/src/primitives/canvas/root.zig +6 -0
- package/src/primitives/canvas/tokens.zig +185 -7
- package/src/primitives/canvas/ui.zig +27 -5
- package/src/primitives/canvas/ui_markup.zig +27 -4
- package/src/primitives/canvas/ui_markup_compiled.zig +9 -1
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +127 -0
- package/src/primitives/canvas/ui_markup_tests.zig +47 -0
- package/src/primitives/canvas/ui_markup_view.zig +10 -1
- package/src/primitives/canvas/ui_markup_view_tests.zig +78 -0
- package/src/primitives/canvas/ui_schema.zig +15 -1
- package/src/primitives/canvas/ui_schema_tests.zig +5 -4
- package/src/primitives/canvas/widget_builtin_tests.zig +439 -5
- package/src/primitives/canvas/widget_layout.zig +142 -3
- package/src/primitives/canvas/widget_layout_tests.zig +82 -0
- package/src/primitives/canvas/widget_metrics.zig +16 -8
- package/src/primitives/canvas/widget_tree.zig +14 -2
- package/src/primitives/canvas/widgets.zig +12 -0
- package/src/runtime/automation_widget_dispatch.zig +95 -28
- package/src/runtime/canvas_frame.zig +15 -0
- package/src/runtime/canvas_widget_anchored_tests.zig +253 -65
- package/src/runtime/canvas_widget_context_menu.zig +3 -2
- package/src/runtime/canvas_widget_events.zig +1271 -3
- package/src/runtime/canvas_widget_floating_tests.zig +2801 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +1 -1
- package/src/runtime/canvas_widget_state.zig +82 -5
- package/src/runtime/core.zig +30 -0
- package/src/runtime/effects.zig +1037 -133
- package/src/runtime/effects_fetch_tests.zig +42 -0
- package/src/runtime/effects_file_tests.zig +220 -0
- package/src/runtime/effects_tests.zig +287 -0
- package/src/runtime/flow.zig +24 -1
- package/src/runtime/gpu_surface_events.zig +44 -5
- package/src/runtime/session_journal.zig +25 -13
- package/src/runtime/session_record.zig +166 -0
- package/src/runtime/session_tests.zig +453 -0
- package/src/runtime/ui_app.zig +137 -39
- package/src/runtime/ui_app_tests.zig +681 -0
- package/src/runtime/ui_app_window_tests.zig +182 -0
- package/src/runtime/view.zig +86 -0
- package/src/runtime/view_widget_text.zig +18 -1
- package/src/runtime/view_widget_tree.zig +339 -20
- package/src/runtime/widget_bridge.zig +26 -0
- package/src/runtime/window_storage.zig +61 -15
- package/src/runtime/window_view_runtime.zig +13 -0
- package/src/runtime/window_views.zig +6 -0
- package/src/tooling/package.zig +311 -8
- package/src/tooling/templates.zig +97 -9
- package/src/tooling/ts_core.zig +626 -13
- package/src/tooling/verbs.zig +88 -2
package/src/runtime/effects.zig
CHANGED
|
@@ -149,6 +149,18 @@ pub const max_effect_file_path_bytes: usize = 1024;
|
|
|
149
149
|
/// write would corrupt the file on disk, which truncation flags cannot
|
|
150
150
|
/// undo.
|
|
151
151
|
pub const max_effect_file_bytes: usize = 1024 * 1024;
|
|
152
|
+
/// How long teardown waits (total, in milliseconds) for file workers
|
|
153
|
+
/// stuck in blocking I/O before abandoning them — the default of the
|
|
154
|
+
/// channel's injectable `file_join_deadline_ms`. File I/O is the one
|
|
155
|
+
/// effect with no converging force at teardown (nothing to kill, no
|
|
156
|
+
/// flag the OS call polls): a write to a FIFO with no reader or a read
|
|
157
|
+
/// against a stalled network filesystem can block forever. Halfway
|
|
158
|
+
/// through this budget teardown cancels the blocked task (best-effort
|
|
159
|
+
/// syscall interruption); a worker still stuck when it expires is
|
|
160
|
+
/// abandoned with a bounded, loud leak (see `Effects.deinit`). Generous
|
|
161
|
+
/// on purpose — healthy local I/O finishes in milliseconds and never
|
|
162
|
+
/// meets this bound.
|
|
163
|
+
pub const default_effect_file_join_deadline_ms: u64 = 15_000;
|
|
152
164
|
|
|
153
165
|
/// Maximum clipboard-effect payload: what one `writeClipboard` writes
|
|
154
166
|
/// and the most one `readClipboard` delivers — the platform clipboard
|
|
@@ -345,8 +357,10 @@ pub const EffectFetchOutcome = enum {
|
|
|
345
357
|
/// `body` is its (possibly truncated) body.
|
|
346
358
|
ok,
|
|
347
359
|
/// The fetch never started: all slots busy, a duplicate active key,
|
|
348
|
-
/// a malformed URL or non-http(s) scheme,
|
|
349
|
-
///
|
|
360
|
+
/// a malformed URL or non-http(s) scheme, URL/headers/payload over
|
|
361
|
+
/// capacity, or the executor could not start the exchange as a
|
|
362
|
+
/// cancelable task (an uncancelable exchange would evade `cancel`,
|
|
363
|
+
/// the timeout, and teardown, so it is refused up front).
|
|
350
364
|
rejected,
|
|
351
365
|
/// DNS resolution or the TCP connect failed (unknown host,
|
|
352
366
|
/// connection refused, network unreachable).
|
|
@@ -767,6 +781,90 @@ fn fallbackEnviron() std.process.Environ {
|
|
|
767
781
|
/// host, so nothing is lost.
|
|
768
782
|
const io_threaded_supported = builtin.target.os.tag != .freestanding;
|
|
769
783
|
const IoThreaded = if (io_threaded_supported) std.Io.Threaded else void;
|
|
784
|
+
/// Worker-thread handles exist only where the threaded executor does
|
|
785
|
+
/// (the same seam as `IoThreaded`): on freestanding targets no worker
|
|
786
|
+
/// ever spawns, so the slot field compiles away to a `?void`.
|
|
787
|
+
const WorkerThread = if (io_threaded_supported) std.Thread else void;
|
|
788
|
+
|
|
789
|
+
/// Process-lifetime allocator backing everything an ABANDONED file
|
|
790
|
+
/// worker can still reach: its `FileWorkerContext`, that context's
|
|
791
|
+
/// data buffer, and the shared executor io (`IoThreaded` and its
|
|
792
|
+
/// internals). The channel's own `allocator` is the OWNER's — an
|
|
793
|
+
/// arena or GPA typically deinitialized right after `Effects.deinit`
|
|
794
|
+
/// returns — so "leaking" caller-allocator memory under a live thread
|
|
795
|
+
/// would only reintroduce the use-after-free the abandon path exists
|
|
796
|
+
/// to prevent; abandonable memory must come from storage nothing ever
|
|
797
|
+
/// tears down. The page allocator is that storage: process-lived by
|
|
798
|
+
/// construction (it has no deinit), thread-safe, and available on
|
|
799
|
+
/// every target including the docs' wasm32-freestanding preview.
|
|
800
|
+
/// Page granularity is irrelevant here — file effects allocate one
|
|
801
|
+
/// small context and one bounded (~1 MiB) buffer per op, and the
|
|
802
|
+
/// executor io is created once, none of it on a hot path. The happy
|
|
803
|
+
/// path frees through this same allocator: `joinWorker` releases the
|
|
804
|
+
/// context and its buffer, `deinit` releases the executor io.
|
|
805
|
+
const process_allocator: std.mem.Allocator = std.heap.page_allocator;
|
|
806
|
+
|
|
807
|
+
/// Everything a real file worker's BLOCKING phase may touch, held
|
|
808
|
+
/// out-of-line from the channel on its own heap block. File I/O is the
|
|
809
|
+
/// one effect teardown cannot always interrupt (a write to a FIFO with
|
|
810
|
+
/// no reader, a stalled network filesystem), so `deinit` bounds its
|
|
811
|
+
/// wait and may ABANDON the worker: it detaches the thread and
|
|
812
|
+
/// deliberately leaks this context together with the buffer it points
|
|
813
|
+
/// at. The invariant that makes the leak safe: an abandoned worker may
|
|
814
|
+
/// wake at ANY later time, so everything it can still reach — this
|
|
815
|
+
/// context, `buffer`, and the executor io — lives in process-lifetime
|
|
816
|
+
/// storage (`process_allocator`, never the channel's caller-owned
|
|
817
|
+
/// allocator) and stays allocated forever, while everything it must
|
|
818
|
+
/// never touch again (the slot, the queue, the channel itself) is
|
|
819
|
+
/// fenced off by the commit/abandon handshake under `mutex`. The leak
|
|
820
|
+
/// is bounded (at most one context and buffer per file slot per
|
|
821
|
+
/// torn-down channel, plus the shared executor io) and loud (`deinit`
|
|
822
|
+
/// warns with the stuck op and path when it gives up).
|
|
823
|
+
const FileWorkerContext = struct {
|
|
824
|
+
/// Serializes the worker's commit-to-publish transition against
|
|
825
|
+
/// teardown's abandon decision: whoever locks first decides
|
|
826
|
+
/// whether the worker may still touch the channel (teardown
|
|
827
|
+
/// joins it) or must walk away (teardown leaks it).
|
|
828
|
+
mutex: SpinMutex = .{},
|
|
829
|
+
/// Teardown gave up on this worker: it must never touch the
|
|
830
|
+
/// channel again — the owner is about to free that memory.
|
|
831
|
+
abandoned: bool = false,
|
|
832
|
+
/// The worker finished its blocking phase while the channel was
|
|
833
|
+
/// still alive: teardown must join it (its epilogue touches the
|
|
834
|
+
/// slot and queue, and converges within milliseconds because the
|
|
835
|
+
/// terminal post gives up on shutdown).
|
|
836
|
+
committed: bool = false,
|
|
837
|
+
/// Set by `deinit` halfway through its file deadline: the
|
|
838
|
+
/// supervisor cancels the blocking task (best-effort syscall
|
|
839
|
+
/// interruption through the threaded io).
|
|
840
|
+
interrupt: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
841
|
+
/// Set by the task after `outcome`/`read_len` are recorded.
|
|
842
|
+
done: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
843
|
+
op: EffectFileOp,
|
|
844
|
+
/// The worker's PRIVATE data buffer (a write's payload copy, a
|
|
845
|
+
/// read's content space) — its own `process_allocator` allocation,
|
|
846
|
+
/// distinct from the slot's caller-allocated delivery buffer,
|
|
847
|
+
/// leaked alongside this context on abandon and freed with it by
|
|
848
|
+
/// `joinWorker` otherwise. A committed read's bytes are copied
|
|
849
|
+
/// into the slot's delivery buffer by the worker epilogue.
|
|
850
|
+
buffer: []u8,
|
|
851
|
+
payload_len: usize,
|
|
852
|
+
read_len: usize = 0,
|
|
853
|
+
outcome: EffectFileOutcome = .io_failed,
|
|
854
|
+
/// Copy of the effect's path: the slot's copy lives inside the
|
|
855
|
+
/// channel, which the owner frees right after an abandoning
|
|
856
|
+
/// teardown returns.
|
|
857
|
+
path_storage: [max_effect_file_path_bytes]u8 = undefined,
|
|
858
|
+
path_len: usize = 0,
|
|
859
|
+
|
|
860
|
+
fn path(ctx: *const FileWorkerContext) []const u8 {
|
|
861
|
+
return ctx.path_storage[0..ctx.path_len];
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
fn payload(ctx: *const FileWorkerContext) []const u8 {
|
|
865
|
+
return ctx.buffer[0..ctx.payload_len];
|
|
866
|
+
}
|
|
867
|
+
};
|
|
770
868
|
|
|
771
869
|
/// Map a fetch-side error onto the delivered failure taxonomy. The
|
|
772
870
|
/// worker refines `.cancelled` into `.timed_out` when the deadline (not
|
|
@@ -1378,6 +1476,115 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1378
1476
|
}
|
|
1379
1477
|
};
|
|
1380
1478
|
|
|
1479
|
+
/// Everything a real spawn worker's BLOCKING phase may touch,
|
|
1480
|
+
/// held out-of-line from the channel on its own heap block —
|
|
1481
|
+
/// the spawn twin of `FileWorkerContext`, and the same
|
|
1482
|
+
/// invariant: teardown may ABANDON a spawn worker whose child's
|
|
1483
|
+
/// group-kill did not converge it (a descendant that escaped
|
|
1484
|
+
/// the process group — `setsid`, a shell's `set -m` background
|
|
1485
|
+
/// job — keeps the inherited stdout write end open, so the
|
|
1486
|
+
/// worker's read never sees EOF), and everything the abandoned
|
|
1487
|
+
/// worker can still wake into must live in process-lifetime
|
|
1488
|
+
/// storage (`process_allocator`) and stay allocated forever.
|
|
1489
|
+
/// The blocking phase therefore runs against copies: argv,
|
|
1490
|
+
/// stdin, the line-framing and collect buffers, the stderr
|
|
1491
|
+
/// tail ring, and the published-child handshake all live here,
|
|
1492
|
+
/// never in the slot. Unlike a file op, a spawn DELIVERS while
|
|
1493
|
+
/// it blocks (streaming lines ride the queue as the child
|
|
1494
|
+
/// produces them), so the commit/abandon handshake fences two
|
|
1495
|
+
/// things: every mid-stream channel touch happens under
|
|
1496
|
+
/// `mutex` with `abandoned` re-checked (see
|
|
1497
|
+
/// `produceSpawnLine`), and the terminal epilogue (publishing
|
|
1498
|
+
/// collect payloads into the slot, posting the exit) runs only
|
|
1499
|
+
/// after `committed` is taken while the channel provably
|
|
1500
|
+
/// lives. Generic over the channel because it carries the
|
|
1501
|
+
/// effect's Msg-typed `on_line` constructor.
|
|
1502
|
+
const SpawnWorkerContext = struct {
|
|
1503
|
+
/// Serializes the worker's channel touches (mid-stream
|
|
1504
|
+
/// line enqueues, the commit-to-publish transition)
|
|
1505
|
+
/// against teardown's abandon decision: whoever locks
|
|
1506
|
+
/// first decides whether the worker may still touch the
|
|
1507
|
+
/// channel (teardown joins it) or must walk away
|
|
1508
|
+
/// (teardown leaks it).
|
|
1509
|
+
mutex: SpinMutex = .{},
|
|
1510
|
+
/// Teardown gave up on this worker: it must never touch
|
|
1511
|
+
/// the channel again — the owner is about to free that
|
|
1512
|
+
/// memory.
|
|
1513
|
+
abandoned: bool = false,
|
|
1514
|
+
/// The worker finished its blocking phase while the
|
|
1515
|
+
/// channel was still alive: teardown must join it (its
|
|
1516
|
+
/// epilogue touches the slot and queue, and converges
|
|
1517
|
+
/// within milliseconds because the terminal post gives up
|
|
1518
|
+
/// on shutdown).
|
|
1519
|
+
committed: bool = false,
|
|
1520
|
+
/// Set by `deinit` halfway through its spawn deadline:
|
|
1521
|
+
/// the supervisor cancels the blocking task (best-effort
|
|
1522
|
+
/// syscall interruption through the threaded io — the net
|
|
1523
|
+
/// under a group-kill that could not converge the child).
|
|
1524
|
+
interrupt: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
1525
|
+
/// Set by the task after `exit` is recorded.
|
|
1526
|
+
done: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
1527
|
+
/// Guards `child_id`/`reaping` between the blocking task
|
|
1528
|
+
/// and `killPublishedChild` (cancel on the loop thread,
|
|
1529
|
+
/// teardown): a kill is only sent while the task has not
|
|
1530
|
+
/// started reaping, so the pid/handle is guaranteed
|
|
1531
|
+
/// un-reaped (alive or zombie) when signaled. Lives here —
|
|
1532
|
+
/// not on the slot — so the handshake works against
|
|
1533
|
+
/// process-lived memory: the task locks it mid-blocking-
|
|
1534
|
+
/// phase, which an abandoned worker may do long after the
|
|
1535
|
+
/// channel is freed (the exact stale-slot `child_mutex`
|
|
1536
|
+
/// crash the join discipline exists to prevent).
|
|
1537
|
+
child_mutex: SpinMutex = .{},
|
|
1538
|
+
child_id: ?std.process.Child.Id = null,
|
|
1539
|
+
reaping: bool = false,
|
|
1540
|
+
/// A kill was requested (cancel or teardown). Read by the
|
|
1541
|
+
/// task so a kill that raced the child's publish still
|
|
1542
|
+
/// lands, and so the recorded exit reports `.cancelled` —
|
|
1543
|
+
/// the context's copy of the slot's `cancel_requested`,
|
|
1544
|
+
/// readable without touching the channel.
|
|
1545
|
+
kill_requested: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
1546
|
+
/// The task's terminal result, published to the committed
|
|
1547
|
+
/// epilogue (never read on abandon).
|
|
1548
|
+
exit: EffectExit = .{ .key = 0, .code = effect_error_exit_code, .reason = .spawn_failed },
|
|
1549
|
+
// ---- blocking-phase copies of the effect's parameters ----
|
|
1550
|
+
key: u64 = 0,
|
|
1551
|
+
on_line: ?LineMsgFn = null,
|
|
1552
|
+
output_mode: EffectOutputMode = .lines,
|
|
1553
|
+
line_limit: usize = max_effect_line_bytes,
|
|
1554
|
+
/// Raised-bound line-framing buffer (its own
|
|
1555
|
+
/// `process_allocator` allocation, leaked on abandon,
|
|
1556
|
+
/// freed with the context by `joinWorker`); null means the
|
|
1557
|
+
/// task frames into its stack buffer. Distinct from the
|
|
1558
|
+
/// slot's `line_buffer`, which the fake executor keeps
|
|
1559
|
+
/// using.
|
|
1560
|
+
line_buffer: ?[]u8 = null,
|
|
1561
|
+
/// Collect-mode stdout accumulation — the worker-private
|
|
1562
|
+
/// twin of the slot's caller-allocated `collect_buffer`
|
|
1563
|
+
/// (the committed epilogue publishes into that one).
|
|
1564
|
+
collect_buffer: ?[]u8 = null,
|
|
1565
|
+
collect_len: usize = 0,
|
|
1566
|
+
collect_truncated: bool = false,
|
|
1567
|
+
stderr_ring: [max_effect_stderr_tail_bytes]u8 = undefined,
|
|
1568
|
+
stderr_total: usize = 0,
|
|
1569
|
+
/// Producer-side drop accounting (the context twin of the
|
|
1570
|
+
/// slot fields the fake executor uses).
|
|
1571
|
+
dropped_pending: u32 = 0,
|
|
1572
|
+
dropped_total: u32 = 0,
|
|
1573
|
+
argv_slices: [max_effect_argv][]const u8 = undefined,
|
|
1574
|
+
argv_count: usize = 0,
|
|
1575
|
+
argv_storage: [max_effect_argv_bytes]u8 = undefined,
|
|
1576
|
+
stdin_storage: [max_effect_stdin_bytes]u8 = undefined,
|
|
1577
|
+
stdin_len: usize = 0,
|
|
1578
|
+
|
|
1579
|
+
fn argv(ctx: *const SpawnWorkerContext) []const []const u8 {
|
|
1580
|
+
return ctx.argv_slices[0..ctx.argv_count];
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
fn stdinBytes(ctx: *const SpawnWorkerContext) []const u8 {
|
|
1584
|
+
return ctx.stdin_storage[0..ctx.stdin_len];
|
|
1585
|
+
}
|
|
1586
|
+
};
|
|
1587
|
+
|
|
1381
1588
|
const Slot = struct {
|
|
1382
1589
|
state: std.atomic.Value(SlotState) = std.atomic.Value(SlotState).init(.idle),
|
|
1383
1590
|
generation: u32 = 0,
|
|
@@ -1398,13 +1605,34 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1398
1605
|
/// lines the drain discards and whose exit reports
|
|
1399
1606
|
/// `.cancelled`. Zero means none (generations start at 1).
|
|
1400
1607
|
cancelled_generation: u32 = 0,
|
|
1401
|
-
///
|
|
1402
|
-
///
|
|
1403
|
-
///
|
|
1404
|
-
///
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1608
|
+
/// The worker thread driving this occupancy (real spawn,
|
|
1609
|
+
/// fetch, and file effects; null for fake slots, host
|
|
1610
|
+
/// requests, and idle slots). Loop-thread bookkeeping:
|
|
1611
|
+
/// stored where the thread spawns, cleared by `joinWorker`.
|
|
1612
|
+
/// A worker holds pointers into the whole channel (its
|
|
1613
|
+
/// slot, the completion queue, the executor io) for its
|
|
1614
|
+
/// entire thread lifetime, so the channel's memory must
|
|
1615
|
+
/// never be freed while a handle here is outstanding —
|
|
1616
|
+
/// `deinit` joins every one before it returns, except a
|
|
1617
|
+
/// file or spawn worker stuck past its teardown deadline,
|
|
1618
|
+
/// which is detached after the handshake that guarantees
|
|
1619
|
+
/// it will never touch the channel again (see
|
|
1620
|
+
/// `FileWorkerContext` / `SpawnWorkerContext`).
|
|
1621
|
+
worker_thread: ?WorkerThread = null,
|
|
1622
|
+
/// The out-of-line world of a real file worker (null for
|
|
1623
|
+
/// every other occupancy). Created alongside the worker
|
|
1624
|
+
/// thread, freed by `joinWorker` once the join proves the
|
|
1625
|
+
/// worker is gone — or deliberately leaked when teardown
|
|
1626
|
+
/// abandons the worker (see `FileWorkerContext`).
|
|
1627
|
+
file_ctx: ?*FileWorkerContext = null,
|
|
1628
|
+
/// The out-of-line world of a real spawn worker (null for
|
|
1629
|
+
/// every other occupancy, and for fake spawns). Same
|
|
1630
|
+
/// lifecycle as `file_ctx`: freed by `joinWorker`, leaked
|
|
1631
|
+
/// on abandon (see `SpawnWorkerContext`). Also where
|
|
1632
|
+
/// `killPublishedChild` finds the published-child
|
|
1633
|
+
/// handshake — the loop thread only dereferences it while
|
|
1634
|
+
/// the channel is alive.
|
|
1635
|
+
spawn_ctx: ?*SpawnWorkerContext = null,
|
|
1408
1636
|
/// Producer-side drop accounting (worker in real mode, loop
|
|
1409
1637
|
/// thread in fake mode; never both).
|
|
1410
1638
|
dropped_pending: u32 = 0,
|
|
@@ -1572,6 +1800,59 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1572
1800
|
environ: ?std.process.Environ = null,
|
|
1573
1801
|
io_threaded: ?*IoThreaded = null,
|
|
1574
1802
|
shutdown: std.atomic.Value(bool) = std.atomic.Value(bool).init(false),
|
|
1803
|
+
/// Teardown budget (total milliseconds) for file workers stuck
|
|
1804
|
+
/// in blocking I/O that nothing can force to converge; see
|
|
1805
|
+
/// `deinit` and `default_effect_file_join_deadline_ms`.
|
|
1806
|
+
/// Injectable so tests pin the interrupt and abandon paths
|
|
1807
|
+
/// with a tiny bound.
|
|
1808
|
+
file_join_deadline_ms: u64 = default_effect_file_join_deadline_ms,
|
|
1809
|
+
/// Best-effort interruption switch: halfway through the file
|
|
1810
|
+
/// deadline, teardown cancels a stuck file worker's blocking
|
|
1811
|
+
/// task (the threaded io interrupts the blocked syscall —
|
|
1812
|
+
/// pthread_kill(SIG.IO)/tgkill on POSIX,
|
|
1813
|
+
/// NtCancelSynchronousIoFile on Windows). Always true in real
|
|
1814
|
+
/// use; tests disable it to reach the abandon-and-leak safety
|
|
1815
|
+
/// net deterministically.
|
|
1816
|
+
file_join_interrupt: bool = true,
|
|
1817
|
+
/// How many file workers teardown has abandoned (each one a
|
|
1818
|
+
/// bounded, warned leak — see `FileWorkerContext`). The seam
|
|
1819
|
+
/// tests assert against: zero on every healthy teardown.
|
|
1820
|
+
abandoned_file_workers: u32 = 0,
|
|
1821
|
+
/// Teardown budget (total milliseconds) for spawn workers the
|
|
1822
|
+
/// group-kill could not converge — a descendant that escaped
|
|
1823
|
+
/// the child's process group (`setsid`, a shell's `set -m`
|
|
1824
|
+
/// background job) keeps the inherited stdout write end open,
|
|
1825
|
+
/// so the worker's read never sees EOF. Shares the file
|
|
1826
|
+
/// deadline's default deliberately (the rationale is
|
|
1827
|
+
/// identical: generous, and a healthy teardown — the kill
|
|
1828
|
+
/// forcing EOF within milliseconds — never meets it) but stays
|
|
1829
|
+
/// its own field so tests pin each worker class independently.
|
|
1830
|
+
spawn_join_deadline_ms: u64 = default_effect_file_join_deadline_ms,
|
|
1831
|
+
/// Best-effort interruption switch for spawn workers,
|
|
1832
|
+
/// mirroring `file_join_interrupt`: halfway through the spawn
|
|
1833
|
+
/// deadline, teardown cancels a stuck worker's blocking task
|
|
1834
|
+
/// (the threaded io interrupts the blocked pipe read). Always
|
|
1835
|
+
/// true in real use; tests disable it to reach the
|
|
1836
|
+
/// abandon-and-leak safety net deterministically.
|
|
1837
|
+
spawn_join_interrupt: bool = true,
|
|
1838
|
+
/// How many spawn workers teardown has abandoned (each one a
|
|
1839
|
+
/// bounded, warned leak — see `SpawnWorkerContext`). The seam
|
|
1840
|
+
/// tests assert against: zero on every healthy teardown.
|
|
1841
|
+
abandoned_spawn_workers: u32 = 0,
|
|
1842
|
+
/// Injectable concurrent-start switch for fetch supervisors,
|
|
1843
|
+
/// following `file_join_interrupt`: always true in real use;
|
|
1844
|
+
/// tests disable it to pin the no-capacity rejection path
|
|
1845
|
+
/// deterministically (the real trigger — the executor refusing
|
|
1846
|
+
/// `std.Io.concurrent` — needs resource exhaustion). See
|
|
1847
|
+
/// `fetchWorkerMain`.
|
|
1848
|
+
fetch_concurrent_start: bool = true,
|
|
1849
|
+
/// How many fetches were REFUSED because the executor could
|
|
1850
|
+
/// not start their exchange as a cancelable task (each one a
|
|
1851
|
+
/// `.rejected` terminal and a one-time warning — never an
|
|
1852
|
+
/// uncancelable inline exchange). Incremented by worker
|
|
1853
|
+
/// threads; read it from the loop thread after the terminal
|
|
1854
|
+
/// drained.
|
|
1855
|
+
fetch_start_rejections: std.atomic.Value(u32) = std.atomic.Value(u32).init(0),
|
|
1575
1856
|
next_generation: u32 = 1,
|
|
1576
1857
|
slots: [max_effects]Slot = [_]Slot{.{}} ** max_effects,
|
|
1577
1858
|
/// Fixed fx timer table (see `max_effect_timers`): timers live
|
|
@@ -1616,8 +1897,32 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1616
1897
|
}
|
|
1617
1898
|
|
|
1618
1899
|
/// Kill every running effect, wait for the workers to finish
|
|
1619
|
-
/// (draining their final queue posts),
|
|
1620
|
-
///
|
|
1900
|
+
/// (draining their final queue posts), JOIN every worker
|
|
1901
|
+
/// thread, and release the executor io. Fetch joins are
|
|
1902
|
+
/// unconditional — fetch supervisors cancel on the shutdown
|
|
1903
|
+
/// flag (and an exchange that cannot start cancellably is
|
|
1904
|
+
/// rejected up front), so no fetch worker survives this call.
|
|
1905
|
+
/// Spawn and file workers get a deadline each
|
|
1906
|
+
/// (`spawn_join_deadline_ms` / `file_join_deadline_ms`), with
|
|
1907
|
+
/// a best-effort cancel of the blocked task at the halfway
|
|
1908
|
+
/// mark: a spawn normally converges from the process-group
|
|
1909
|
+
/// kill within milliseconds, but a descendant that escaped the
|
|
1910
|
+
/// group (`setsid`, a shell's `set -m` background job) keeps
|
|
1911
|
+
/// the stdout pipe open past every kill, and file I/O has no
|
|
1912
|
+
/// converging force at all (a write to a FIFO with no reader,
|
|
1913
|
+
/// a stalled network filesystem). A worker still stuck when
|
|
1914
|
+
/// its budget expires is ABANDONED — thread detached, its
|
|
1915
|
+
/// out-of-line context, private buffers, and the executor io
|
|
1916
|
+
/// deliberately leaked, one warning naming the stuck op (see
|
|
1917
|
+
/// `FileWorkerContext` / `SpawnWorkerContext` for the
|
|
1918
|
+
/// invariant). Either way the owner may free the channel's
|
|
1919
|
+
/// memory — and tear down the allocator behind it — the moment
|
|
1920
|
+
/// this returns: nothing an abandoned worker can still reach
|
|
1921
|
+
/// lives in the channel or in caller-allocator storage (the
|
|
1922
|
+
/// leakable set is allocated from `process_allocator`). All
|
|
1923
|
+
/// three worker classes share one terminal guarantee: teardown
|
|
1924
|
+
/// returns bounded, and every byte a live thread can still
|
|
1925
|
+
/// touch stays valid forever.
|
|
1621
1926
|
///
|
|
1622
1927
|
/// Idempotent, and only the FIRST call may touch platform
|
|
1623
1928
|
/// services — it ends by severing the services binding, so a
|
|
@@ -1665,22 +1970,186 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1665
1970
|
}
|
|
1666
1971
|
if (io_threaded_supported) {
|
|
1667
1972
|
if (self.io_threaded) |threaded| {
|
|
1973
|
+
// Wait for every worker's terminal post, then JOIN
|
|
1974
|
+
// every worker thread. For fetch workers the wait
|
|
1975
|
+
// is unbounded on purpose: a worker holds pointers
|
|
1976
|
+
// into this struct (its slot, the completion
|
|
1977
|
+
// queue, this io) for its whole thread lifetime,
|
|
1978
|
+
// and the owner frees this memory right after
|
|
1979
|
+
// deinit returns — a bounded give-up here once
|
|
1980
|
+
// left an abandoned worker writing into freed
|
|
1981
|
+
// memory, which is exactly how a torn-down app
|
|
1982
|
+
// crashed inside a stale slot's `child_mutex`.
|
|
1983
|
+
// Fetch convergence is the shutdown flag's doing:
|
|
1984
|
+
// supervisors poll it and cancel their exchange
|
|
1985
|
+
// (one that cannot start cancellably is rejected
|
|
1986
|
+
// up front, never run inline), and the
|
|
1987
|
+
// terminal-post retry loops give up on `shutdown`
|
|
1988
|
+
// within a millisecond. Spawn and file workers get
|
|
1989
|
+
// a deadline each instead, because their blocking
|
|
1990
|
+
// phase can outlive every converging force: the
|
|
1991
|
+
// group-kill above forces most spawn reads to EOF
|
|
1992
|
+
// within milliseconds (`child.wait` then reaps a
|
|
1993
|
+
// dead process, and a raced pre-publish cancel is
|
|
1994
|
+
// re-checked by the task itself), but a descendant
|
|
1995
|
+
// that escaped the process group keeps the pipe
|
|
1996
|
+
// open past every kill; file I/O has nothing to
|
|
1997
|
+
// kill at all. Halfway through each deadline the
|
|
1998
|
+
// supervisor is asked to cancel the blocked task
|
|
1999
|
+
// (the threaded io interrupts blocked syscalls
|
|
2000
|
+
// with a no-op signal on POSIX and
|
|
2001
|
+
// NtCancelSynchronousIoFile on Windows), and a
|
|
2002
|
+
// worker still stuck at the end is abandoned below
|
|
2003
|
+
// instead of hanging teardown forever. The queue
|
|
2004
|
+
// is still drained while waiting so posts that
|
|
2005
|
+
// landed before the flag retire promptly.
|
|
1668
2006
|
const io = threaded.io();
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
2007
|
+
const file_deadline_ms = self.file_join_deadline_ms;
|
|
2008
|
+
const spawn_deadline_ms = self.spawn_join_deadline_ms;
|
|
2009
|
+
var waited_ms: u64 = 0;
|
|
2010
|
+
var file_interrupt_sent = false;
|
|
2011
|
+
var spawn_interrupt_sent = false;
|
|
2012
|
+
while (true) {
|
|
2013
|
+
var converging = false;
|
|
2014
|
+
var file_pending = false;
|
|
2015
|
+
var spawn_pending = false;
|
|
1672
2016
|
for (&self.slots) |*slot| {
|
|
1673
|
-
if (slot.state.load(.acquire)
|
|
2017
|
+
if (slot.fake or slot.state.load(.acquire) != .running) continue;
|
|
2018
|
+
switch (slot.kind) {
|
|
2019
|
+
.file => file_pending = true,
|
|
2020
|
+
.spawn => spawn_pending = true,
|
|
2021
|
+
else => converging = true,
|
|
2022
|
+
}
|
|
2023
|
+
}
|
|
2024
|
+
if (!converging and
|
|
2025
|
+
(!file_pending or waited_ms >= file_deadline_ms) and
|
|
2026
|
+
(!spawn_pending or waited_ms >= spawn_deadline_ms)) break;
|
|
2027
|
+
if (file_pending and !file_interrupt_sent and self.file_join_interrupt and waited_ms >= file_deadline_ms / 2) {
|
|
2028
|
+
file_interrupt_sent = true;
|
|
2029
|
+
for (&self.slots) |*slot| {
|
|
2030
|
+
if (slot.kind != .file or slot.fake) continue;
|
|
2031
|
+
if (slot.state.load(.acquire) != .running) continue;
|
|
2032
|
+
if (slot.file_ctx) |ctx| ctx.interrupt.store(true, .release);
|
|
2033
|
+
}
|
|
2034
|
+
}
|
|
2035
|
+
if (spawn_pending and !spawn_interrupt_sent and self.spawn_join_interrupt and waited_ms >= spawn_deadline_ms / 2) {
|
|
2036
|
+
spawn_interrupt_sent = true;
|
|
2037
|
+
for (&self.slots) |*slot| {
|
|
2038
|
+
if (slot.kind != .spawn or slot.fake) continue;
|
|
2039
|
+
if (slot.state.load(.acquire) != .running) continue;
|
|
2040
|
+
if (slot.spawn_ctx) |ctx| ctx.interrupt.store(true, .release);
|
|
2041
|
+
}
|
|
1674
2042
|
}
|
|
1675
|
-
if (!running) break;
|
|
1676
|
-
// Keep the queue drained so exit-post retries finish.
|
|
1677
2043
|
self.clearQueue();
|
|
1678
2044
|
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(1), .awake) catch break;
|
|
2045
|
+
waited_ms += 1;
|
|
2046
|
+
}
|
|
2047
|
+
var abandoned_any = false;
|
|
2048
|
+
for (&self.slots) |*slot| {
|
|
2049
|
+
if (slot.kind != .file or slot.fake) continue;
|
|
2050
|
+
if (slot.state.load(.acquire) != .running) continue;
|
|
2051
|
+
const ctx = slot.file_ctx orelse continue;
|
|
2052
|
+
// The commit/abandon handshake: whoever locks
|
|
2053
|
+
// first decides. A worker that just finished
|
|
2054
|
+
// has committed — it is touching the slot and
|
|
2055
|
+
// queue right now and converges in
|
|
2056
|
+
// milliseconds, so the unconditional join
|
|
2057
|
+
// below collects it like any other worker.
|
|
2058
|
+
ctx.mutex.lock();
|
|
2059
|
+
const committed = ctx.committed;
|
|
2060
|
+
if (!committed) ctx.abandoned = true;
|
|
2061
|
+
ctx.mutex.unlock();
|
|
2062
|
+
if (committed) continue;
|
|
2063
|
+
std.debug.print(
|
|
2064
|
+
"effects teardown: a file {s} against '{s}' is still blocked in I/O after {d}ms; abandoning its worker thread and leaking its buffers so teardown can return safely\n",
|
|
2065
|
+
.{ @tagName(slot.file_op), slot.filePath(), file_deadline_ms },
|
|
2066
|
+
);
|
|
2067
|
+
// DELIBERATE LEAK — the invariant that makes it
|
|
2068
|
+
// safe: the abandoned worker may wake at any
|
|
2069
|
+
// later time, so everything it can still reach
|
|
2070
|
+
// must stay allocated forever — its context
|
|
2071
|
+
// (`ctx`, dropped here without freeing), that
|
|
2072
|
+
// context's data buffer, and the executor io
|
|
2073
|
+
// (leaked after the joins). All three live in
|
|
2074
|
+
// process-lifetime storage
|
|
2075
|
+
// (`process_allocator`), NOT the channel's
|
|
2076
|
+
// caller-owned allocator, so the leak survives
|
|
2077
|
+
// the owner deinitializing its arena or GPA
|
|
2078
|
+
// right after this returns. The handshake above
|
|
2079
|
+
// guarantees the worker touches nothing else —
|
|
2080
|
+
// not this slot (whose caller-owned delivery
|
|
2081
|
+
// buffer the sweep below frees normally), not
|
|
2082
|
+
// the queue, not the channel — so the owner may
|
|
2083
|
+
// free the channel's memory the moment deinit
|
|
2084
|
+
// returns. Bounded (one context and buffer per
|
|
2085
|
+
// abandoned worker, one executor io per
|
|
2086
|
+
// abandoning teardown) and loud (the warning
|
|
2087
|
+
// above).
|
|
2088
|
+
if (slot.worker_thread) |thread| {
|
|
2089
|
+
slot.worker_thread = null;
|
|
2090
|
+
thread.detach();
|
|
2091
|
+
}
|
|
2092
|
+
slot.file_ctx = null;
|
|
2093
|
+
slot.generation = 0;
|
|
2094
|
+
slot.state.store(.idle, .release);
|
|
2095
|
+
self.abandoned_file_workers += 1;
|
|
2096
|
+
abandoned_any = true;
|
|
2097
|
+
}
|
|
2098
|
+
for (&self.slots) |*slot| {
|
|
2099
|
+
if (slot.kind != .spawn or slot.fake) continue;
|
|
2100
|
+
if (slot.state.load(.acquire) != .running) continue;
|
|
2101
|
+
const ctx = slot.spawn_ctx orelse continue;
|
|
2102
|
+
// The same commit/abandon handshake as the file
|
|
2103
|
+
// block above: a worker that committed is in
|
|
2104
|
+
// its epilogue right now and the unconditional
|
|
2105
|
+
// join below collects it; one still blocked is
|
|
2106
|
+
// fenced off the channel forever.
|
|
2107
|
+
ctx.mutex.lock();
|
|
2108
|
+
const committed = ctx.committed;
|
|
2109
|
+
if (!committed) ctx.abandoned = true;
|
|
2110
|
+
ctx.mutex.unlock();
|
|
2111
|
+
if (committed) continue;
|
|
2112
|
+
std.debug.print(
|
|
2113
|
+
"effects teardown: spawned command '{s}' still holds its worker after {d}ms (an escaped descendant is keeping its pipe open past the group kill); abandoning the worker thread and leaking its context so teardown can return safely\n",
|
|
2114
|
+
.{ if (slot.argv_count > 0) slot.argv_slices[0] else "", spawn_deadline_ms },
|
|
2115
|
+
);
|
|
2116
|
+
// DELIBERATE LEAK, same invariant as the file
|
|
2117
|
+
// abandon above: the worker may wake at any
|
|
2118
|
+
// later time (the escaped descendant exits and
|
|
2119
|
+
// EOF finally arrives), so its context — the
|
|
2120
|
+
// argv/stdin copies, the child handshake it
|
|
2121
|
+
// will still lock to reap, its private
|
|
2122
|
+
// framing/collect buffers — and the executor
|
|
2123
|
+
// io stay allocated forever in
|
|
2124
|
+
// `process_allocator` storage. The handshake
|
|
2125
|
+
// guarantees it touches nothing else: not this
|
|
2126
|
+
// slot (whose caller-owned collect buffer the
|
|
2127
|
+
// sweep below frees normally), not the queue
|
|
2128
|
+
// (`produceSpawnLine` re-checks the abandon
|
|
2129
|
+
// under the fence), not the channel. Bounded
|
|
2130
|
+
// and loud, exactly like the file leak.
|
|
2131
|
+
if (slot.worker_thread) |thread| {
|
|
2132
|
+
slot.worker_thread = null;
|
|
2133
|
+
thread.detach();
|
|
2134
|
+
}
|
|
2135
|
+
slot.spawn_ctx = null;
|
|
2136
|
+
slot.generation = 0;
|
|
2137
|
+
slot.state.store(.idle, .release);
|
|
2138
|
+
self.abandoned_spawn_workers += 1;
|
|
2139
|
+
abandoned_any = true;
|
|
2140
|
+
}
|
|
2141
|
+
for (&self.slots) |*slot| joinWorker(slot);
|
|
2142
|
+
if (abandoned_any) {
|
|
2143
|
+
// Leak the executor io too: the abandoned
|
|
2144
|
+
// worker's blocked syscall runs inside it, and
|
|
2145
|
+
// `IoThreaded.deinit` would wait on (or free
|
|
2146
|
+
// under) that thread.
|
|
2147
|
+
self.io_threaded = null;
|
|
1679
2148
|
}
|
|
1680
2149
|
}
|
|
1681
2150
|
if (self.io_threaded) |threaded| {
|
|
1682
2151
|
threaded.deinit();
|
|
1683
|
-
|
|
2152
|
+
process_allocator.destroy(threaded);
|
|
1684
2153
|
self.io_threaded = null;
|
|
1685
2154
|
}
|
|
1686
2155
|
}
|
|
@@ -1944,8 +2413,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1944
2413
|
// from a cancelled previous occupant may still sit in the
|
|
1945
2414
|
// queue, and the sticky value keeps filtering them after the
|
|
1946
2415
|
// slot is reused (the new generation never matches it).
|
|
1947
|
-
slot.child_id = null;
|
|
1948
|
-
slot.reaping = false;
|
|
1949
2416
|
slot.dropped_pending = 0;
|
|
1950
2417
|
slot.dropped_total = 0;
|
|
1951
2418
|
slot.argv_count = options.argv.len;
|
|
@@ -1971,14 +2438,19 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1971
2438
|
}
|
|
1972
2439
|
slot.line_limit = options.max_line_bytes;
|
|
1973
2440
|
if (options.output == .collect) {
|
|
2441
|
+
// The slot's CHANNEL-owned collect buffer: the drain
|
|
2442
|
+
// hands it to `update` (real mode receives the bytes
|
|
2443
|
+
// from the worker context at commit; fake mode
|
|
2444
|
+
// accumulates into it directly).
|
|
1974
2445
|
slot.collect_buffer = self.allocator.alloc(u8, max_effect_collect_bytes) catch {
|
|
1975
2446
|
return self.reject(options);
|
|
1976
2447
|
};
|
|
1977
|
-
} else if (options.max_line_bytes > max_effect_line_bytes) {
|
|
1978
|
-
slot.line_buffer = self.allocator.alloc(u8, options.max_line_bytes) catch {
|
|
1979
|
-
return self.reject(options);
|
|
1980
|
-
};
|
|
1981
2448
|
}
|
|
2449
|
+
// No slot-side line buffer for spawns: a real worker frames
|
|
2450
|
+
// raised-bound lines into its context's process-lived
|
|
2451
|
+
// buffer (see `SpawnWorkerContext.line_buffer`), and the
|
|
2452
|
+
// fake executor's `feedLine` never frames. (`.stream`
|
|
2453
|
+
// fetches still allocate the slot buffer in `fetch`.)
|
|
1982
2454
|
slot.fake = self.executor == .fake;
|
|
1983
2455
|
slot.state.store(.running, .release);
|
|
1984
2456
|
|
|
@@ -1988,11 +2460,60 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
1988
2460
|
self.releaseSpawnSlot(slot);
|
|
1989
2461
|
return self.reject(options);
|
|
1990
2462
|
};
|
|
1991
|
-
|
|
2463
|
+
// The worker's blocking phase touches only this out-of-line
|
|
2464
|
+
// context (argv/stdin copies, the child handshake, its own
|
|
2465
|
+
// framing/collect buffers), never the channel — the seam
|
|
2466
|
+
// that lets teardown abandon a worker whose child's
|
|
2467
|
+
// group-kill did not converge it and still free the channel
|
|
2468
|
+
// (see `SpawnWorkerContext`). Context and buffers come from
|
|
2469
|
+
// `process_allocator`, never `self.allocator`: teardown may
|
|
2470
|
+
// leak them under a live thread, and the channel's
|
|
2471
|
+
// caller-owned allocator dies with the owner. The happy
|
|
2472
|
+
// path frees them in `joinWorker`, through the same
|
|
2473
|
+
// process-lifetime seam.
|
|
2474
|
+
const ctx = process_allocator.create(SpawnWorkerContext) catch {
|
|
1992
2475
|
self.releaseSpawnSlot(slot);
|
|
1993
2476
|
return self.reject(options);
|
|
1994
2477
|
};
|
|
1995
|
-
|
|
2478
|
+
ctx.* = .{
|
|
2479
|
+
.key = options.key,
|
|
2480
|
+
.on_line = options.on_line,
|
|
2481
|
+
.output_mode = options.output,
|
|
2482
|
+
.line_limit = options.max_line_bytes,
|
|
2483
|
+
.exit = .{ .key = options.key, .code = effect_error_exit_code, .reason = .spawn_failed },
|
|
2484
|
+
.argv_count = options.argv.len,
|
|
2485
|
+
.stdin_len = stdin_bytes.len,
|
|
2486
|
+
};
|
|
2487
|
+
// Slices must point into the heap context, so they are
|
|
2488
|
+
// built in place after the copy above.
|
|
2489
|
+
var ctx_offset: usize = 0;
|
|
2490
|
+
for (options.argv, 0..) |arg, index| {
|
|
2491
|
+
@memcpy(ctx.argv_storage[ctx_offset .. ctx_offset + arg.len], arg);
|
|
2492
|
+
ctx.argv_slices[index] = ctx.argv_storage[ctx_offset .. ctx_offset + arg.len];
|
|
2493
|
+
ctx_offset += arg.len;
|
|
2494
|
+
}
|
|
2495
|
+
@memcpy(ctx.stdin_storage[0..stdin_bytes.len], stdin_bytes);
|
|
2496
|
+
if (options.output == .collect) {
|
|
2497
|
+
ctx.collect_buffer = process_allocator.alloc(u8, max_effect_collect_bytes) catch {
|
|
2498
|
+
process_allocator.destroy(ctx);
|
|
2499
|
+
self.releaseSpawnSlot(slot);
|
|
2500
|
+
return self.reject(options);
|
|
2501
|
+
};
|
|
2502
|
+
} else if (options.max_line_bytes > max_effect_line_bytes) {
|
|
2503
|
+
ctx.line_buffer = process_allocator.alloc(u8, options.max_line_bytes) catch {
|
|
2504
|
+
process_allocator.destroy(ctx);
|
|
2505
|
+
self.releaseSpawnSlot(slot);
|
|
2506
|
+
return self.reject(options);
|
|
2507
|
+
};
|
|
2508
|
+
}
|
|
2509
|
+
slot.spawn_ctx = ctx;
|
|
2510
|
+
const thread = std.Thread.spawn(.{}, spawnWorkerMain, .{ self, slot_index, slot.generation, io, ctx }) catch {
|
|
2511
|
+
destroySpawnContext(ctx);
|
|
2512
|
+
slot.spawn_ctx = null;
|
|
2513
|
+
self.releaseSpawnSlot(slot);
|
|
2514
|
+
return self.reject(options);
|
|
2515
|
+
};
|
|
2516
|
+
slot.worker_thread = thread;
|
|
1996
2517
|
}
|
|
1997
2518
|
|
|
1998
2519
|
/// Run an HTTP(S) request on a worker thread and deliver its
|
|
@@ -2063,8 +2584,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2063
2584
|
slot.cancel_requested.store(false, .release);
|
|
2064
2585
|
slot.fetch_done.store(false, .release);
|
|
2065
2586
|
// `cancelled_generation` stays sticky, exactly as in `spawn`.
|
|
2066
|
-
slot.child_id = null;
|
|
2067
|
-
slot.reaping = false;
|
|
2068
2587
|
slot.dropped_pending = 0;
|
|
2069
2588
|
slot.dropped_total = 0;
|
|
2070
2589
|
@memcpy(slot.url_storage[0..options.url.len], options.url);
|
|
@@ -2101,7 +2620,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2101
2620
|
self.releaseFetchSlot(slot);
|
|
2102
2621
|
return self.rejectFetch(options);
|
|
2103
2622
|
};
|
|
2104
|
-
thread
|
|
2623
|
+
slot.worker_thread = thread;
|
|
2105
2624
|
}
|
|
2106
2625
|
|
|
2107
2626
|
/// Write a whole file on a worker thread — TEA-friendly
|
|
@@ -2142,7 +2661,11 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2142
2661
|
const slot = &self.slots[slot_index];
|
|
2143
2662
|
// A write's buffer holds its payload copy; a read's holds
|
|
2144
2663
|
// the content space plus one spare byte that detects
|
|
2145
|
-
// over-bound files without a stat round trip.
|
|
2664
|
+
// over-bound files without a stat round trip. This is the
|
|
2665
|
+
// slot's CHANNEL-owned buffer (fake-mode storage, real-mode
|
|
2666
|
+
// delivery space the drain hands to `update`); a real
|
|
2667
|
+
// worker's blocking phase gets its own process-lived copy
|
|
2668
|
+
// in `FileWorkerContext.buffer` below.
|
|
2146
2669
|
const buffer_len = if (op == .write) bytes.len else max_effect_file_bytes + 1;
|
|
2147
2670
|
const buffer = self.allocator.alloc(u8, buffer_len) catch {
|
|
2148
2671
|
return self.rejectFile(key, op, on_result);
|
|
@@ -2159,8 +2682,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2159
2682
|
slot.on_file = on_result;
|
|
2160
2683
|
slot.cancel_requested.store(false, .release);
|
|
2161
2684
|
// `cancelled_generation` stays sticky, exactly as in `spawn`.
|
|
2162
|
-
slot.child_id = null;
|
|
2163
|
-
slot.reaping = false;
|
|
2164
2685
|
slot.dropped_pending = 0;
|
|
2165
2686
|
slot.dropped_total = 0;
|
|
2166
2687
|
@memcpy(slot.url_storage[0..file_path.len], file_path);
|
|
@@ -2187,11 +2708,42 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2187
2708
|
self.releaseFetchSlot(slot);
|
|
2188
2709
|
return self.rejectFile(key, op, on_result);
|
|
2189
2710
|
};
|
|
2190
|
-
|
|
2711
|
+
// The worker's blocking phase touches only this out-of-line
|
|
2712
|
+
// context (path copy, op, its own data buffer), never the
|
|
2713
|
+
// channel — the seam that lets teardown abandon a stuck
|
|
2714
|
+
// worker and still free the channel (see
|
|
2715
|
+
// `FileWorkerContext`). Context and buffer both come from
|
|
2716
|
+
// `process_allocator`, never `self.allocator`: teardown may
|
|
2717
|
+
// leak them under a live thread, and the channel's
|
|
2718
|
+
// caller-owned allocator dies with the owner. The happy
|
|
2719
|
+
// path frees both in `joinWorker`, through the same
|
|
2720
|
+
// process-lifetime seam.
|
|
2721
|
+
const ctx = process_allocator.create(FileWorkerContext) catch {
|
|
2722
|
+
self.releaseFetchSlot(slot);
|
|
2723
|
+
return self.rejectFile(key, op, on_result);
|
|
2724
|
+
};
|
|
2725
|
+
const worker_buffer = process_allocator.alloc(u8, buffer_len) catch {
|
|
2726
|
+
process_allocator.destroy(ctx);
|
|
2727
|
+
self.releaseFetchSlot(slot);
|
|
2728
|
+
return self.rejectFile(key, op, on_result);
|
|
2729
|
+
};
|
|
2730
|
+
ctx.* = .{
|
|
2731
|
+
.op = op,
|
|
2732
|
+
.buffer = worker_buffer,
|
|
2733
|
+
.payload_len = slot.payload_len,
|
|
2734
|
+
};
|
|
2735
|
+
if (op == .write) @memcpy(worker_buffer[0..bytes.len], bytes);
|
|
2736
|
+
@memcpy(ctx.path_storage[0..file_path.len], file_path);
|
|
2737
|
+
ctx.path_len = file_path.len;
|
|
2738
|
+
slot.file_ctx = ctx;
|
|
2739
|
+
const thread = std.Thread.spawn(.{}, fileWorkerMain, .{ self, slot_index, slot.generation, io, ctx }) catch {
|
|
2740
|
+
process_allocator.free(worker_buffer);
|
|
2741
|
+
process_allocator.destroy(ctx);
|
|
2742
|
+
slot.file_ctx = null;
|
|
2191
2743
|
self.releaseFetchSlot(slot);
|
|
2192
2744
|
return self.rejectFile(key, op, on_result);
|
|
2193
2745
|
};
|
|
2194
|
-
thread
|
|
2746
|
+
slot.worker_thread = thread;
|
|
2195
2747
|
}
|
|
2196
2748
|
|
|
2197
2749
|
/// Put text on the system clipboard through the platform
|
|
@@ -2255,8 +2807,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2255
2807
|
slot.on_clipboard = on_result;
|
|
2256
2808
|
slot.cancel_requested.store(false, .release);
|
|
2257
2809
|
// `cancelled_generation` stays sticky, exactly as in `spawn`.
|
|
2258
|
-
slot.child_id = null;
|
|
2259
|
-
slot.reaping = false;
|
|
2260
2810
|
slot.dropped_pending = 0;
|
|
2261
2811
|
slot.dropped_total = 0;
|
|
2262
2812
|
slot.url_len = 0;
|
|
@@ -2409,8 +2959,6 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
2409
2959
|
slot.on_host = options.on_result;
|
|
2410
2960
|
slot.cancel_requested.store(false, .release);
|
|
2411
2961
|
// `cancelled_generation` stays sticky, exactly as in `spawn`.
|
|
2412
|
-
slot.child_id = null;
|
|
2413
|
-
slot.reaping = false;
|
|
2414
2962
|
slot.dropped_pending = 0;
|
|
2415
2963
|
slot.dropped_total = 0;
|
|
2416
2964
|
@memcpy(slot.url_storage[0..options.name.len], options.name);
|
|
@@ -3776,11 +4324,18 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
3776
4324
|
fn ensureIo(self: *Self) !std.Io {
|
|
3777
4325
|
if (io_threaded_supported) {
|
|
3778
4326
|
if (self.io_threaded == null) {
|
|
3779
|
-
|
|
4327
|
+
// The executor and everything it allocates internally
|
|
4328
|
+
// come from `process_allocator`, never the channel's
|
|
4329
|
+
// caller-owned allocator: an abandoning teardown
|
|
4330
|
+
// leaks the executor under a live worker's blocked
|
|
4331
|
+
// syscall, so nothing the worker can reach through
|
|
4332
|
+
// it may die with the owner. The healthy teardown
|
|
4333
|
+
// frees it through the same seam (`deinit`).
|
|
4334
|
+
const threaded = try process_allocator.create(IoThreaded);
|
|
3780
4335
|
// `environ` defaults to `.empty` in `InitOptions`, which
|
|
3781
4336
|
// would hand every spawned child a blank environment (no
|
|
3782
4337
|
// HOME, no PATH) — always pass the host environment.
|
|
3783
|
-
threaded.* = IoThreaded.init(
|
|
4338
|
+
threaded.* = IoThreaded.init(process_allocator, .{
|
|
3784
4339
|
.environ = self.environ orelse fallbackEnviron(),
|
|
3785
4340
|
});
|
|
3786
4341
|
self.io_threaded = threaded;
|
|
@@ -4056,14 +4611,60 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4056
4611
|
return index;
|
|
4057
4612
|
}
|
|
4058
4613
|
|
|
4614
|
+
/// Join a finished worker's thread and clear its handle.
|
|
4615
|
+
/// Loop-thread only. The reclaim path reaches this only after
|
|
4616
|
+
/// the worker published a non-running slot state — its last
|
|
4617
|
+
/// slot access — so the join blocks for the thread's epilogue
|
|
4618
|
+
/// (a wake nudge and the OS exit), never on child I/O. The
|
|
4619
|
+
/// teardown path (`deinit`) may join a still-running worker;
|
|
4620
|
+
/// convergence there is the kill's and the shutdown flag's
|
|
4621
|
+
/// doing, as documented at that call site.
|
|
4622
|
+
fn joinWorker(slot: *Slot) void {
|
|
4623
|
+
if (io_threaded_supported) {
|
|
4624
|
+
const thread = slot.worker_thread orelse return;
|
|
4625
|
+
slot.worker_thread = null;
|
|
4626
|
+
thread.join();
|
|
4627
|
+
// The join proves the worker is gone: its out-of-line
|
|
4628
|
+
// context (file and spawn workers) has no user left.
|
|
4629
|
+
// Contexts and their buffers are process-lifetime
|
|
4630
|
+
// allocations (see `process_allocator`), so they free
|
|
4631
|
+
// through that seam, not the channel's allocator.
|
|
4632
|
+
if (slot.file_ctx) |ctx| {
|
|
4633
|
+
process_allocator.free(ctx.buffer);
|
|
4634
|
+
process_allocator.destroy(ctx);
|
|
4635
|
+
slot.file_ctx = null;
|
|
4636
|
+
}
|
|
4637
|
+
if (slot.spawn_ctx) |ctx| {
|
|
4638
|
+
destroySpawnContext(ctx);
|
|
4639
|
+
slot.spawn_ctx = null;
|
|
4640
|
+
}
|
|
4641
|
+
}
|
|
4642
|
+
}
|
|
4643
|
+
|
|
4644
|
+
/// Return a spawn worker context and its private buffers to
|
|
4645
|
+
/// `process_allocator` — the happy-path counterpart of the
|
|
4646
|
+
/// abandon leak (spawn-time failures and `joinWorker`).
|
|
4647
|
+
fn destroySpawnContext(ctx: *SpawnWorkerContext) void {
|
|
4648
|
+
if (ctx.line_buffer) |buffer| process_allocator.free(buffer);
|
|
4649
|
+
if (ctx.collect_buffer) |buffer| process_allocator.free(buffer);
|
|
4650
|
+
process_allocator.destroy(ctx);
|
|
4651
|
+
}
|
|
4652
|
+
|
|
4059
4653
|
fn reclaimSlots(self: *Self) void {
|
|
4060
4654
|
for (&self.slots) |*slot| {
|
|
4061
4655
|
switch (slot.state.load(.acquire)) {
|
|
4062
|
-
.done =>
|
|
4656
|
+
.done => {
|
|
4657
|
+
joinWorker(slot);
|
|
4658
|
+
slot.state.store(.idle, .release);
|
|
4659
|
+
},
|
|
4063
4660
|
// A draining slot is reusable once the drain took its
|
|
4064
4661
|
// heap buffer (fetch body or collected stdout — the
|
|
4065
|
-
// terminal Msg was delivered).
|
|
4066
|
-
|
|
4662
|
+
// terminal Msg was delivered). Its worker is already
|
|
4663
|
+
// finished either way: retire the thread now.
|
|
4664
|
+
.draining => {
|
|
4665
|
+
joinWorker(slot);
|
|
4666
|
+
if (slot.fetch_buffer == null and slot.collect_buffer == null) slot.state.store(.idle, .release);
|
|
4667
|
+
},
|
|
4067
4668
|
else => {},
|
|
4068
4669
|
}
|
|
4069
4670
|
}
|
|
@@ -4171,77 +4772,241 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4171
4772
|
self.wakeHost();
|
|
4172
4773
|
}
|
|
4173
4774
|
|
|
4174
|
-
///
|
|
4175
|
-
///
|
|
4176
|
-
///
|
|
4775
|
+
/// `produceLine` for a real spawn's blocking task: entry
|
|
4776
|
+
/// parameters and drop accounting come from the worker context,
|
|
4777
|
+
/// and every channel touch (the caller-owned allocator for an
|
|
4778
|
+
/// oversized line, the queue, the wake) happens under the
|
|
4779
|
+
/// context's abandon fence — teardown may ABANDON this worker
|
|
4780
|
+
/// at any pre-commit moment, and whoever takes `ctx.mutex`
|
|
4781
|
+
/// first decides: an abandoned task drops the line and walks
|
|
4782
|
+
/// away (the channel may already be freed), while a delivery in
|
|
4783
|
+
/// flight under the lock finishes before teardown can mark the
|
|
4784
|
+
/// abandon. Streaming deliveries therefore keep their live
|
|
4785
|
+
/// arrival order and byte identity on every healthy path.
|
|
4786
|
+
fn produceSpawnLine(self: *Self, ctx: *SpawnWorkerContext, slot_index: u16, generation: u32, bytes: []const u8, truncated: bool) void {
|
|
4787
|
+
var len = @min(bytes.len, ctx.line_limit);
|
|
4788
|
+
var entry: Entry = .{
|
|
4789
|
+
.kind = .line,
|
|
4790
|
+
.slot_index = slot_index,
|
|
4791
|
+
.generation = generation,
|
|
4792
|
+
.key = ctx.key,
|
|
4793
|
+
.truncated = truncated or bytes.len > ctx.line_limit,
|
|
4794
|
+
.dropped_before = ctx.dropped_pending,
|
|
4795
|
+
.line_fn = ctx.on_line,
|
|
4796
|
+
};
|
|
4797
|
+
ctx.mutex.lock();
|
|
4798
|
+
defer ctx.mutex.unlock();
|
|
4799
|
+
if (ctx.abandoned) return;
|
|
4800
|
+
if (len <= max_effect_line_bytes) {
|
|
4801
|
+
@memcpy(entry.line_bytes[0..len], bytes[0..len]);
|
|
4802
|
+
} else if (self.allocator.alloc(u8, len)) |heap| {
|
|
4803
|
+
@memcpy(heap, bytes[0..len]);
|
|
4804
|
+
entry.heap_line = heap;
|
|
4805
|
+
} else |_| {
|
|
4806
|
+
len = max_effect_line_bytes;
|
|
4807
|
+
entry.truncated = true;
|
|
4808
|
+
@memcpy(entry.line_bytes[0..len], bytes[0..len]);
|
|
4809
|
+
}
|
|
4810
|
+
entry.line_len = @intCast(len);
|
|
4811
|
+
if (self.enqueue(&entry)) {
|
|
4812
|
+
ctx.dropped_pending = 0;
|
|
4813
|
+
} else {
|
|
4814
|
+
if (entry.heap_line) |heap| self.allocator.free(heap);
|
|
4815
|
+
ctx.dropped_pending +|= 1;
|
|
4816
|
+
ctx.dropped_total +|= 1;
|
|
4817
|
+
}
|
|
4818
|
+
self.wakeHost();
|
|
4819
|
+
}
|
|
4820
|
+
|
|
4821
|
+
/// Request a kill of a real spawn's child through its worker
|
|
4822
|
+
/// context: record the request (so a kill that races the
|
|
4823
|
+
/// child's publish still lands — the task re-checks after
|
|
4824
|
+
/// publishing) and signal the published id. Loop-thread only
|
|
4825
|
+
/// (cancel, teardown), while the channel is alive; the
|
|
4826
|
+
/// handshake itself lives in the process-lived context so the
|
|
4827
|
+
/// blocking task can take it channel-blind.
|
|
4177
4828
|
fn killPublishedChild(self: *Self, slot: *Slot) void {
|
|
4178
4829
|
_ = self;
|
|
4179
|
-
slot.
|
|
4180
|
-
|
|
4181
|
-
|
|
4182
|
-
|
|
4830
|
+
const ctx = slot.spawn_ctx orelse return;
|
|
4831
|
+
ctx.kill_requested.store(true, .release);
|
|
4832
|
+
killPublishedChildCtx(ctx);
|
|
4833
|
+
}
|
|
4834
|
+
|
|
4835
|
+
/// Send a kill to the published child id, but only while the
|
|
4836
|
+
/// task has not begun reaping (guarded by the context mutex),
|
|
4837
|
+
/// so the pid/handle is guaranteed to still name this process.
|
|
4838
|
+
fn killPublishedChildCtx(ctx: *SpawnWorkerContext) void {
|
|
4839
|
+
ctx.child_mutex.lock();
|
|
4840
|
+
defer ctx.child_mutex.unlock();
|
|
4841
|
+
if (ctx.reaping) return;
|
|
4842
|
+
const id = ctx.child_id orelse return;
|
|
4183
4843
|
if (builtin.os.tag == .windows) {
|
|
4844
|
+
// Windows has no process groups in this sense: the
|
|
4845
|
+
// direct-handle terminate cannot reach descendants, so
|
|
4846
|
+
// an escaped grandchild holding the stdout pipe is
|
|
4847
|
+
// converged by the teardown interruption (the threaded
|
|
4848
|
+
// io cancels the blocked read) or, past the deadline,
|
|
4849
|
+
// by the abandon net. Job objects would kill the whole
|
|
4850
|
+
// tree properly — the future strengthening; not built
|
|
4851
|
+
// here.
|
|
4184
4852
|
_ = std.os.windows.ntdll.NtTerminateProcess(id, @enumFromInt(1));
|
|
4185
4853
|
} else {
|
|
4186
|
-
|
|
4854
|
+
// The child owns its process group (see the spawn in
|
|
4855
|
+
// `runChild`), so the negative-pid form signals every
|
|
4856
|
+
// descendant still in it — the grandchildren of a
|
|
4857
|
+
// shell-wrapped command included, which is what lets
|
|
4858
|
+
// the worker's stdout read reach EOF promptly. The
|
|
4859
|
+
// direct-pid fallback covers a group already gone. A
|
|
4860
|
+
// descendant that LEFT the group (`setsid`, `set -m`)
|
|
4861
|
+
// is out of reach by construction; the teardown
|
|
4862
|
+
// interruption and abandon net bound the worker
|
|
4863
|
+
// instead.
|
|
4864
|
+
std.posix.kill(-id, .KILL) catch {
|
|
4865
|
+
std.posix.kill(id, .KILL) catch {};
|
|
4866
|
+
};
|
|
4187
4867
|
}
|
|
4188
4868
|
}
|
|
4189
4869
|
|
|
4190
4870
|
// ------------------------------------------------------------ worker
|
|
4191
4871
|
|
|
4192
|
-
|
|
4872
|
+
/// Supervises one real spawn, mirroring `fileWorkerMain`: the
|
|
4873
|
+
/// blocking phase (child spawn, stream reads, wait/reap) runs
|
|
4874
|
+
/// as a cancelable `Io` task that touches only its out-of-line
|
|
4875
|
+
/// `SpawnWorkerContext` — plus the queue, under the context's
|
|
4876
|
+
/// abandon fence, for streaming line deliveries (see
|
|
4877
|
+
/// `produceSpawnLine`). Convergence at teardown is normally
|
|
4878
|
+
/// the group-kill's doing (EOF within milliseconds); when a
|
|
4879
|
+
/// descendant escaped the group and keeps the pipe open,
|
|
4880
|
+
/// teardown sets `ctx.interrupt` at half its spawn deadline
|
|
4881
|
+
/// and the supervisor cancels the task (the threaded io
|
|
4882
|
+
/// interrupts the blocked read), and past the full deadline
|
|
4883
|
+
/// the worker is abandoned. After the task ends the supervisor
|
|
4884
|
+
/// COMMITS under the context's mutex — from that point it may
|
|
4885
|
+
/// touch the slot and queue (publishing collect payloads into
|
|
4886
|
+
/// the slot's delivery buffer, posting the exit), and teardown
|
|
4887
|
+
/// will join it — or finds itself abandoned and returns
|
|
4888
|
+
/// without touching the channel again.
|
|
4889
|
+
fn spawnWorkerMain(self: *Self, slot_index: usize, generation: u32, io: std.Io, ctx: *SpawnWorkerContext) void {
|
|
4890
|
+
supervise: {
|
|
4891
|
+
var future = std.Io.concurrent(io, spawnTask, .{ self, ctx, @as(u16, @intCast(slot_index)), generation, io }) catch {
|
|
4892
|
+
// No concurrent capacity: run the blocking phase
|
|
4893
|
+
// inline. Teardown cannot interrupt it, but the
|
|
4894
|
+
// group-kill converges the common case and the
|
|
4895
|
+
// deadline still bounds deinit via the abandon
|
|
4896
|
+
// net. (Deliberately unlike fetch, which REJECTS
|
|
4897
|
+
// when its exchange cannot start cancellably:
|
|
4898
|
+
// fetch joins are unconditional and unbounded,
|
|
4899
|
+
// while a stuck spawn worker — like a file one —
|
|
4900
|
+
// has the abandon safety net.)
|
|
4901
|
+
spawnTask(self, ctx, @intCast(slot_index), generation, io);
|
|
4902
|
+
break :supervise;
|
|
4903
|
+
};
|
|
4904
|
+
superviseWorkerTask(io, &future, &ctx.done, &ctx.interrupt);
|
|
4905
|
+
}
|
|
4906
|
+
ctx.mutex.lock();
|
|
4907
|
+
if (ctx.abandoned) {
|
|
4908
|
+
// Teardown gave up on this worker: the channel may
|
|
4909
|
+
// already be freed. The context (and everything the
|
|
4910
|
+
// task touched through it) is intentionally leaked and
|
|
4911
|
+
// stays valid forever — touch nothing else.
|
|
4912
|
+
ctx.mutex.unlock();
|
|
4913
|
+
return;
|
|
4914
|
+
}
|
|
4915
|
+
ctx.committed = true;
|
|
4916
|
+
ctx.mutex.unlock();
|
|
4917
|
+
// Committed: the channel is alive, and a concurrent
|
|
4918
|
+
// teardown now JOINS this thread instead of abandoning it.
|
|
4919
|
+
// The epilogue below touches the slot and queue freely and
|
|
4920
|
+
// converges within milliseconds (the terminal post gives
|
|
4921
|
+
// up on shutdown).
|
|
4193
4922
|
const slot = &self.slots[slot_index];
|
|
4194
|
-
var exit
|
|
4195
|
-
|
|
4196
|
-
|
|
4197
|
-
|
|
4198
|
-
|
|
4199
|
-
|
|
4200
|
-
|
|
4923
|
+
var exit = ctx.exit;
|
|
4924
|
+
exit.dropped_lines = ctx.dropped_total;
|
|
4925
|
+
slot.dropped_total = ctx.dropped_total;
|
|
4926
|
+
if (ctx.output_mode == .collect) {
|
|
4927
|
+
// Publish the collected payloads into the slot's
|
|
4928
|
+
// channel-owned delivery storage (what the drain hands
|
|
4929
|
+
// to `update`): the blocking phase filled only the
|
|
4930
|
+
// context's process-lived private copies.
|
|
4931
|
+
if (slot.collect_buffer) |delivery| {
|
|
4932
|
+
const publish_len = @min(ctx.collect_len, delivery.len);
|
|
4933
|
+
@memcpy(delivery[0..publish_len], ctx.collect_buffer.?[0..publish_len]);
|
|
4934
|
+
slot.collect_len = publish_len;
|
|
4935
|
+
} else {
|
|
4936
|
+
slot.collect_len = 0;
|
|
4937
|
+
}
|
|
4938
|
+
slot.collect_truncated = ctx.collect_truncated;
|
|
4939
|
+
slot.stderr_ring = ctx.stderr_ring;
|
|
4940
|
+
slot.stderr_total = ctx.stderr_total;
|
|
4941
|
+
}
|
|
4201
4942
|
self.postExit(slot, @intCast(slot_index), generation, io, exit);
|
|
4202
4943
|
// A collect slot still owns its stdout buffer; park it in
|
|
4203
4944
|
// `.draining` until the drain takes the buffer (fetch-style).
|
|
4204
|
-
slot.state.store(if (
|
|
4945
|
+
slot.state.store(if (ctx.output_mode == .collect) .draining else .done, .release);
|
|
4205
4946
|
self.wakeHost();
|
|
4206
4947
|
}
|
|
4207
4948
|
|
|
4208
|
-
|
|
4949
|
+
/// The blocking spawn phase as a cancelable task on the
|
|
4950
|
+
/// executor io. Always records a terminal `ctx.exit` before
|
|
4951
|
+
/// `done`. Touches only the leaked-on-abandon context, the io,
|
|
4952
|
+
/// and — under the context's abandon fence — the completion
|
|
4953
|
+
/// queue for streaming lines (see `SpawnWorkerContext`).
|
|
4954
|
+
fn spawnTask(self: *Self, ctx: *SpawnWorkerContext, slot_index: u16, generation: u32, io: std.Io) void {
|
|
4955
|
+
defer ctx.done.store(true, .release);
|
|
4956
|
+
self.runChild(ctx, slot_index, generation, io);
|
|
4957
|
+
}
|
|
4958
|
+
|
|
4959
|
+
fn runChild(self: *Self, ctx: *SpawnWorkerContext, slot_index: u16, generation: u32, io: std.Io) void {
|
|
4209
4960
|
var child = std.process.spawn(io, .{
|
|
4210
|
-
.argv =
|
|
4211
|
-
.stdin = if (
|
|
4961
|
+
.argv = ctx.argv(),
|
|
4962
|
+
.stdin = if (ctx.stdin_len > 0) .pipe else .ignore,
|
|
4212
4963
|
.stdout = .pipe,
|
|
4213
|
-
.stderr = if (
|
|
4964
|
+
.stderr = if (ctx.output_mode == .collect) .pipe else .ignore,
|
|
4965
|
+
// POSIX: land the child in its OWN process group (id ==
|
|
4966
|
+
// its pid, set before exec) so `killPublishedChild` can
|
|
4967
|
+
// signal the whole descendant tree. Killing only the
|
|
4968
|
+
// direct child leaves a shell-wrapped command's
|
|
4969
|
+
// grandchildren (`sh -c "a; b"` forks `b`) holding the
|
|
4970
|
+
// stdout pipe open — the worker would then block at
|
|
4971
|
+
// read until the orphan exits on its own, stalling
|
|
4972
|
+
// cancel's `.cancelled` terminal and any teardown
|
|
4973
|
+
// joining the worker. Windows has no process groups in
|
|
4974
|
+
// this sense; the direct-handle terminate stands alone
|
|
4975
|
+
// there, as before (see `killPublishedChildCtx` for
|
|
4976
|
+
// the descendant story on both platforms).
|
|
4977
|
+
.pgid = if (builtin.os.tag == .windows) null else 0,
|
|
4214
4978
|
}) catch return;
|
|
4215
4979
|
|
|
4216
|
-
|
|
4217
|
-
|
|
4218
|
-
|
|
4980
|
+
ctx.child_mutex.lock();
|
|
4981
|
+
ctx.child_id = child.id;
|
|
4982
|
+
ctx.child_mutex.unlock();
|
|
4219
4983
|
// A cancel that raced the spawn still lands.
|
|
4220
|
-
if (
|
|
4984
|
+
if (ctx.kill_requested.load(.acquire)) killPublishedChildCtx(ctx);
|
|
4221
4985
|
|
|
4222
4986
|
// Collect mode drains stderr concurrently so a chatty child
|
|
4223
4987
|
// can never deadlock against a full stderr pipe while we read
|
|
4224
4988
|
// stdout. The reader ends at stderr EOF (the child exiting),
|
|
4225
4989
|
// and the join below runs before the exit is assembled, so
|
|
4226
4990
|
// the tail ring is complete and single-threaded again by the
|
|
4227
|
-
// time it is read.
|
|
4991
|
+
// time it is read. The reader touches only the context (and
|
|
4992
|
+
// the child's pipe), so it is as abandon-safe as the task.
|
|
4228
4993
|
var stderr_thread: ?std.Thread = null;
|
|
4229
4994
|
defer if (stderr_thread) |thread| thread.join();
|
|
4230
4995
|
if (child.stderr) |stderr_file| {
|
|
4231
|
-
stderr_thread = std.Thread.spawn(.{}, stderrTailMain, .{
|
|
4996
|
+
stderr_thread = std.Thread.spawn(.{}, stderrTailMain, .{ ctx, io, stderr_file }) catch null;
|
|
4232
4997
|
}
|
|
4233
4998
|
|
|
4234
4999
|
if (child.stdin) |stdin_file| {
|
|
4235
|
-
stdin_file.writeStreamingAll(io,
|
|
5000
|
+
stdin_file.writeStreamingAll(io, ctx.stdinBytes()) catch {};
|
|
4236
5001
|
stdin_file.close(io);
|
|
4237
5002
|
child.stdin = null;
|
|
4238
5003
|
}
|
|
4239
5004
|
|
|
4240
5005
|
if (child.stdout) |stdout_file| {
|
|
4241
|
-
if (
|
|
4242
|
-
collectStdout(
|
|
5006
|
+
if (ctx.output_mode == .collect) {
|
|
5007
|
+
collectStdout(ctx, io, stdout_file);
|
|
4243
5008
|
} else {
|
|
4244
|
-
self.streamLines(
|
|
5009
|
+
self.streamLines(ctx, slot_index, generation, io, stdout_file);
|
|
4245
5010
|
}
|
|
4246
5011
|
}
|
|
4247
5012
|
|
|
@@ -4249,7 +5014,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4249
5014
|
// stdout closed (before reaping, so a child blocked on a full
|
|
4250
5015
|
// stderr pipe still finishes).
|
|
4251
5016
|
if (stderr_thread == null) {
|
|
4252
|
-
if (child.stderr) |stderr_file| collectStderrTail(
|
|
5017
|
+
if (child.stderr) |stderr_file| collectStderrTail(ctx, io, stderr_file);
|
|
4253
5018
|
}
|
|
4254
5019
|
|
|
4255
5020
|
// Join the stderr reader BEFORE reaping: `child.wait` closes
|
|
@@ -4263,30 +5028,31 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4263
5028
|
stderr_thread = null;
|
|
4264
5029
|
}
|
|
4265
5030
|
|
|
4266
|
-
|
|
4267
|
-
|
|
4268
|
-
|
|
5031
|
+
ctx.child_mutex.lock();
|
|
5032
|
+
ctx.reaping = true;
|
|
5033
|
+
ctx.child_mutex.unlock();
|
|
4269
5034
|
const term = child.wait(io) catch {
|
|
4270
|
-
exit
|
|
5035
|
+
ctx.exit = .{ .key = ctx.key, .code = effect_error_exit_code, .reason = .signaled };
|
|
5036
|
+
if (ctx.kill_requested.load(.acquire)) ctx.exit.reason = .cancelled;
|
|
4271
5037
|
return;
|
|
4272
5038
|
};
|
|
4273
|
-
exit
|
|
4274
|
-
.exited => |code| .{ .key =
|
|
4275
|
-
else => .{ .key =
|
|
5039
|
+
ctx.exit = switch (term) {
|
|
5040
|
+
.exited => |code| .{ .key = ctx.key, .code = code, .reason = .exited },
|
|
5041
|
+
else => .{ .key = ctx.key, .code = effect_error_exit_code, .reason = .signaled },
|
|
4276
5042
|
};
|
|
4277
|
-
if (
|
|
4278
|
-
exit.reason = .cancelled;
|
|
4279
|
-
exit.code = effect_error_exit_code;
|
|
5043
|
+
if (ctx.kill_requested.load(.acquire)) {
|
|
5044
|
+
ctx.exit.reason = .cancelled;
|
|
5045
|
+
ctx.exit.code = effect_error_exit_code;
|
|
4280
5046
|
}
|
|
4281
5047
|
}
|
|
4282
5048
|
|
|
4283
|
-
fn streamLines(self: *Self,
|
|
5049
|
+
fn streamLines(self: *Self, ctx: *SpawnWorkerContext, slot_index: u16, generation: u32, io: std.Io, stdout_file: std.Io.File) void {
|
|
4284
5050
|
var read_buffer: [1024]u8 = undefined;
|
|
4285
5051
|
// Spawns with a raised `max_line_bytes` frame into their
|
|
4286
|
-
//
|
|
5052
|
+
// context's heap buffer; everyone else uses the stack.
|
|
4287
5053
|
var stack_buffer: [max_effect_line_bytes]u8 = undefined;
|
|
4288
|
-
const line_buffer: []u8 =
|
|
4289
|
-
const limit = @min(
|
|
5054
|
+
const line_buffer: []u8 = ctx.line_buffer orelse &stack_buffer;
|
|
5055
|
+
const limit = @min(ctx.line_limit, line_buffer.len);
|
|
4290
5056
|
var line_len: usize = 0;
|
|
4291
5057
|
var truncated = false;
|
|
4292
5058
|
while (true) {
|
|
@@ -4294,7 +5060,7 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4294
5060
|
const count = stdout_file.readStreaming(io, &read_slices) catch break;
|
|
4295
5061
|
for (read_buffer[0..count]) |byte| {
|
|
4296
5062
|
if (byte == '\n') {
|
|
4297
|
-
self.
|
|
5063
|
+
self.produceSpawnLine(ctx, slot_index, generation, line_buffer[0..line_len], truncated);
|
|
4298
5064
|
line_len = 0;
|
|
4299
5065
|
truncated = false;
|
|
4300
5066
|
} else if (line_len < limit) {
|
|
@@ -4306,35 +5072,58 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4306
5072
|
}
|
|
4307
5073
|
}
|
|
4308
5074
|
if (line_len > 0 or truncated) {
|
|
4309
|
-
self.
|
|
5075
|
+
self.produceSpawnLine(ctx, slot_index, generation, line_buffer[0..line_len], truncated);
|
|
4310
5076
|
}
|
|
4311
5077
|
}
|
|
4312
5078
|
|
|
4313
|
-
/// Collect-mode stdout: raw bytes into the
|
|
4314
|
-
/// no line framing, bounded by
|
|
4315
|
-
|
|
5079
|
+
/// Collect-mode stdout: raw bytes into the context's private
|
|
5080
|
+
/// buffer, no line framing, bounded by
|
|
5081
|
+
/// `max_effect_collect_bytes`. The committed epilogue publishes
|
|
5082
|
+
/// them into the slot's delivery buffer.
|
|
5083
|
+
fn collectStdout(ctx: *SpawnWorkerContext, io: std.Io, stdout_file: std.Io.File) void {
|
|
4316
5084
|
var read_buffer: [4096]u8 = undefined;
|
|
4317
5085
|
while (true) {
|
|
4318
5086
|
const read_slices: [1][]u8 = .{&read_buffer};
|
|
4319
5087
|
const count = stdout_file.readStreaming(io, &read_slices) catch break;
|
|
4320
5088
|
if (count == 0) break;
|
|
4321
|
-
|
|
5089
|
+
appendCollectedCtx(ctx, read_buffer[0..count]);
|
|
4322
5090
|
}
|
|
4323
5091
|
}
|
|
4324
5092
|
|
|
4325
|
-
fn stderrTailMain(
|
|
4326
|
-
collectStderrTail(
|
|
5093
|
+
fn stderrTailMain(ctx: *SpawnWorkerContext, io: std.Io, stderr_file: std.Io.File) void {
|
|
5094
|
+
collectStderrTail(ctx, io, stderr_file);
|
|
4327
5095
|
}
|
|
4328
5096
|
|
|
4329
5097
|
/// Collect-mode stderr: keep the last
|
|
4330
|
-
/// `max_effect_stderr_tail_bytes` bytes in the
|
|
4331
|
-
|
|
5098
|
+
/// `max_effect_stderr_tail_bytes` bytes in the context's tail
|
|
5099
|
+
/// ring.
|
|
5100
|
+
fn collectStderrTail(ctx: *SpawnWorkerContext, io: std.Io, stderr_file: std.Io.File) void {
|
|
4332
5101
|
var read_buffer: [1024]u8 = undefined;
|
|
4333
5102
|
while (true) {
|
|
4334
5103
|
const read_slices: [1][]u8 = .{&read_buffer};
|
|
4335
5104
|
const count = stderr_file.readStreaming(io, &read_slices) catch break;
|
|
4336
5105
|
if (count == 0) break;
|
|
4337
|
-
|
|
5106
|
+
appendStderrTailCtx(ctx, read_buffer[0..count]);
|
|
5107
|
+
}
|
|
5108
|
+
}
|
|
5109
|
+
|
|
5110
|
+
/// `appendCollected` against the worker context's private
|
|
5111
|
+
/// buffer (single-writer: the blocking task).
|
|
5112
|
+
fn appendCollectedCtx(ctx: *SpawnWorkerContext, bytes: []const u8) void {
|
|
5113
|
+
const buffer = ctx.collect_buffer orelse return;
|
|
5114
|
+
const remaining = buffer.len - ctx.collect_len;
|
|
5115
|
+
const take = @min(bytes.len, remaining);
|
|
5116
|
+
@memcpy(buffer[ctx.collect_len..][0..take], bytes[0..take]);
|
|
5117
|
+
ctx.collect_len += take;
|
|
5118
|
+
if (take < bytes.len) ctx.collect_truncated = true;
|
|
5119
|
+
}
|
|
5120
|
+
|
|
5121
|
+
/// `appendStderrTail` against the worker context's private ring
|
|
5122
|
+
/// (single-writer: the stderr reader).
|
|
5123
|
+
fn appendStderrTailCtx(ctx: *SpawnWorkerContext, bytes: []const u8) void {
|
|
5124
|
+
for (bytes) |byte| {
|
|
5125
|
+
ctx.stderr_ring[ctx.stderr_total % max_effect_stderr_tail_bytes] = byte;
|
|
5126
|
+
ctx.stderr_total += 1;
|
|
4338
5127
|
}
|
|
4339
5128
|
}
|
|
4340
5129
|
|
|
@@ -4342,18 +5131,36 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4342
5131
|
|
|
4343
5132
|
/// Supervises one fetch: runs the blocking HTTP exchange as a
|
|
4344
5133
|
/// cancelable `Io` task and polls for completion, cancel, and
|
|
4345
|
-
/// the timeout deadline.
|
|
4346
|
-
///
|
|
4347
|
-
///
|
|
5134
|
+
/// the timeout deadline. An exchange that cannot start as a
|
|
5135
|
+
/// cancelable task is REJECTED, never run inline (see
|
|
5136
|
+
/// `startFetchExchange`). Ends by posting exactly one
|
|
5137
|
+
/// `.response` entry and parking the slot in `.draining` (the
|
|
5138
|
+
/// drain retires it after taking the body buffer).
|
|
4348
5139
|
fn fetchWorkerMain(self: *Self, slot_index: usize, generation: u32, io: std.Io) void {
|
|
4349
5140
|
const slot = &self.slots[slot_index];
|
|
4350
5141
|
supervise: {
|
|
4351
|
-
var future =
|
|
4352
|
-
//
|
|
4353
|
-
//
|
|
4354
|
-
//
|
|
4355
|
-
//
|
|
4356
|
-
|
|
5142
|
+
var future = self.startFetchExchange(slot, slot_index, generation, io) catch {
|
|
5143
|
+
// The executor cannot start the exchange as a
|
|
5144
|
+
// cancelable task. Running it inline instead would
|
|
5145
|
+
// observe neither `cancel` nor the timeout — and
|
|
5146
|
+
// teardown joins fetch workers UNCONDITIONALLY on
|
|
5147
|
+
// the guarantee that they poll `shutdown` and
|
|
5148
|
+
// cancel their exchange, so an inline stall would
|
|
5149
|
+
// hang `deinit` forever. REFUSE the exchange: the
|
|
5150
|
+
// honest terminal is `.rejected` (the fetch never
|
|
5151
|
+
// started), journaled at drain like every transport
|
|
5152
|
+
// failure, so replay reproduces the rejection
|
|
5153
|
+
// byte-identically.
|
|
5154
|
+
slot.fetch_status = 0;
|
|
5155
|
+
slot.body_len = 0;
|
|
5156
|
+
slot.fetch_truncated = false;
|
|
5157
|
+
slot.fetch_outcome = .rejected;
|
|
5158
|
+
if (self.fetch_start_rejections.fetchAdd(1, .monotonic) == 0) {
|
|
5159
|
+
std.debug.print(
|
|
5160
|
+
"effects fetch: the executor could not start a cancelable exchange for '{s}'; rejecting the fetch (an inline exchange would evade cancel, the timeout, and teardown)\n",
|
|
5161
|
+
.{slot.fetchUrl()},
|
|
5162
|
+
);
|
|
5163
|
+
}
|
|
4357
5164
|
break :supervise;
|
|
4358
5165
|
};
|
|
4359
5166
|
const poll_ms: u64 = 5;
|
|
@@ -4400,6 +5207,19 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4400
5207
|
self.wakeHost();
|
|
4401
5208
|
}
|
|
4402
5209
|
|
|
5210
|
+
/// Start the blocking exchange as a cancelable task on the
|
|
5211
|
+
/// executor io — the ONLY way a real exchange may run: an
|
|
5212
|
+
/// exchange the supervisor cannot cancel would break the fetch
|
|
5213
|
+
/// contract (timeout, `cancel`, teardown's unconditional join),
|
|
5214
|
+
/// so a start failure is surfaced to `fetchWorkerMain`, which
|
|
5215
|
+
/// rejects the fetch instead of running it inline. Injectable
|
|
5216
|
+
/// through `fetch_concurrent_start` so tests pin the rejection
|
|
5217
|
+
/// path deterministically.
|
|
5218
|
+
fn startFetchExchange(self: *Self, slot: *Slot, slot_index: usize, generation: u32, io: std.Io) std.Io.ConcurrentError!std.Io.Future(void) {
|
|
5219
|
+
if (!self.fetch_concurrent_start) return error.ConcurrencyUnavailable;
|
|
5220
|
+
return std.Io.concurrent(io, fetchTask, .{ self, slot, @as(u16, @intCast(slot_index)), generation, io });
|
|
5221
|
+
}
|
|
5222
|
+
|
|
4403
5223
|
/// The blocking exchange, run as a cancelable task. Always
|
|
4404
5224
|
/// records a terminal state in the slot before `fetch_done`.
|
|
4405
5225
|
fn fetchTask(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io) void {
|
|
@@ -4569,58 +5389,142 @@ pub fn Effects(comptime Msg: type) type {
|
|
|
4569
5389
|
|
|
4570
5390
|
// ------------------------------------------------------- file worker
|
|
4571
5391
|
|
|
4572
|
-
///
|
|
4573
|
-
///
|
|
4574
|
-
///
|
|
5392
|
+
/// Supervises one file effect, mirroring `fetchWorkerMain`: the
|
|
5393
|
+
/// blocking read/write runs as a cancelable `Io` task that
|
|
5394
|
+
/// touches ONLY its out-of-line `FileWorkerContext`, never the
|
|
5395
|
+
/// channel. `cancel` does not interrupt the OS call: it marks
|
|
5396
|
+
/// the generation and the drain rewrites the terminal to
|
|
5397
|
+
/// `.cancelled` (the operation itself may still have completed
|
|
5398
|
+
/// on disk, as `EffectFileOutcome.cancelled` documents).
|
|
5399
|
+
/// Teardown is the one interrupter — past half its file
|
|
5400
|
+
/// deadline it sets `ctx.interrupt` and the supervisor cancels
|
|
5401
|
+
/// the task (the threaded io interrupts the blocked syscall
|
|
5402
|
+
/// where the platform allows it). After the task ends the
|
|
5403
|
+
/// supervisor COMMITS under the context's mutex — from that
|
|
5404
|
+
/// point it may touch the slot and queue, and teardown will
|
|
5405
|
+
/// join it — or finds itself abandoned and returns without
|
|
5406
|
+
/// touching the channel again, walking only the leaked
|
|
5407
|
+
/// context. On the healthy path exactly one `.file` terminal
|
|
4575
5408
|
/// entry posts and the slot parks in `.draining` until the
|
|
4576
|
-
/// drain takes its buffer.
|
|
4577
|
-
|
|
4578
|
-
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
5409
|
+
/// drain takes its buffer.
|
|
5410
|
+
fn fileWorkerMain(self: *Self, slot_index: usize, generation: u32, io: std.Io, ctx: *FileWorkerContext) void {
|
|
5411
|
+
supervise: {
|
|
5412
|
+
var future = std.Io.concurrent(io, fileTask, .{ ctx, io }) catch {
|
|
5413
|
+
// No concurrent capacity: run the op inline.
|
|
5414
|
+
// Teardown cannot interrupt it, only outwait or
|
|
5415
|
+
// abandon it — the deadline still bounds deinit.
|
|
5416
|
+
// (Deliberately unlike fetch, which REJECTS when
|
|
5417
|
+
// its exchange cannot start cancellably: fetch
|
|
5418
|
+
// joins are unconditional and unbounded, while a
|
|
5419
|
+
// stuck file worker has the abandon safety net.)
|
|
5420
|
+
fileTask(ctx, io);
|
|
5421
|
+
break :supervise;
|
|
5422
|
+
};
|
|
5423
|
+
superviseWorkerTask(io, &future, &ctx.done, &ctx.interrupt);
|
|
5424
|
+
}
|
|
5425
|
+
ctx.mutex.lock();
|
|
5426
|
+
if (ctx.abandoned) {
|
|
5427
|
+
// Teardown gave up on this worker: the channel may
|
|
5428
|
+
// already be freed. The context (and the buffer the
|
|
5429
|
+
// task just used) is intentionally leaked and stays
|
|
5430
|
+
// valid forever — touch nothing else.
|
|
5431
|
+
ctx.mutex.unlock();
|
|
5432
|
+
return;
|
|
5433
|
+
}
|
|
5434
|
+
ctx.committed = true;
|
|
5435
|
+
ctx.mutex.unlock();
|
|
5436
|
+
// Committed: the channel is alive, and a concurrent
|
|
5437
|
+
// teardown now JOINS this thread instead of abandoning it.
|
|
5438
|
+
// The epilogue below touches the slot and queue freely and
|
|
5439
|
+
// converges within milliseconds (the terminal post gives
|
|
5440
|
+
// up on shutdown).
|
|
4582
5441
|
const slot = &self.slots[slot_index];
|
|
4583
|
-
|
|
4584
|
-
|
|
4585
|
-
|
|
4586
|
-
|
|
5442
|
+
const outcome: EffectFileOutcome = if (ctx.done.load(.acquire)) ctx.outcome else .cancelled;
|
|
5443
|
+
// Publish a read's bytes into the slot's channel-owned
|
|
5444
|
+
// delivery buffer (what the drain hands to `update`): the
|
|
5445
|
+
// blocking phase filled only the context's process-lived
|
|
5446
|
+
// private buffer. Committed means teardown JOINS this
|
|
5447
|
+
// worker, so the slot buffer is safe to touch here.
|
|
5448
|
+
if (ctx.op == .read) {
|
|
5449
|
+
if (slot.fetch_buffer) |delivery| {
|
|
5450
|
+
const publish_len = @min(ctx.read_len, delivery.len);
|
|
5451
|
+
@memcpy(delivery[0..publish_len], ctx.buffer[0..publish_len]);
|
|
5452
|
+
}
|
|
5453
|
+
}
|
|
5454
|
+
slot.body_len = ctx.read_len;
|
|
5455
|
+
self.postFile(slot, @intCast(slot_index), generation, io, outcome, ctx.read_len);
|
|
4587
5456
|
slot.state.store(.draining, .release);
|
|
4588
5457
|
self.wakeHost();
|
|
4589
5458
|
}
|
|
4590
5459
|
|
|
4591
|
-
|
|
5460
|
+
/// Poll a worker's cancelable blocking task to completion:
|
|
5461
|
+
/// converge when the task records `done`, cancel it
|
|
5462
|
+
/// (best-effort syscall interruption through the threaded io)
|
|
5463
|
+
/// when teardown sets `interrupt`, and always await the future
|
|
5464
|
+
/// so the task's frame retires. Shared by the file and spawn
|
|
5465
|
+
/// supervisors; both flags live in the worker's process-lived
|
|
5466
|
+
/// context, so the poll itself is abandon-safe.
|
|
5467
|
+
fn superviseWorkerTask(io: std.Io, future: *std.Io.Future(void), done: *const std.atomic.Value(bool), interrupt: *const std.atomic.Value(bool)) void {
|
|
5468
|
+
const poll_ms: u64 = 5;
|
|
5469
|
+
while (true) {
|
|
5470
|
+
if (done.load(.acquire)) break;
|
|
5471
|
+
if (interrupt.load(.acquire)) {
|
|
5472
|
+
future.cancel(io);
|
|
5473
|
+
break;
|
|
5474
|
+
}
|
|
5475
|
+
std.Io.sleep(io, std.Io.Duration.fromMilliseconds(poll_ms), .awake) catch {};
|
|
5476
|
+
}
|
|
5477
|
+
future.await(io);
|
|
5478
|
+
}
|
|
5479
|
+
|
|
5480
|
+
/// The blocking file op as a cancelable task on the executor
|
|
5481
|
+
/// io. Touches only the leaked-on-abandon context and the io —
|
|
5482
|
+
/// never the slot, the queue, or the channel (see
|
|
5483
|
+
/// `FileWorkerContext`).
|
|
5484
|
+
fn fileTask(ctx: *FileWorkerContext, io: std.Io) void {
|
|
5485
|
+
ctx.outcome = runFileOp(ctx, io);
|
|
5486
|
+
ctx.done.store(true, .release);
|
|
5487
|
+
}
|
|
5488
|
+
|
|
5489
|
+
fn runFileOp(ctx: *FileWorkerContext, io: std.Io) EffectFileOutcome {
|
|
4592
5490
|
const cwd = std.Io.Dir.cwd();
|
|
4593
|
-
const file_path =
|
|
4594
|
-
switch (
|
|
5491
|
+
const file_path = ctx.path();
|
|
5492
|
+
switch (ctx.op) {
|
|
4595
5493
|
.write => {
|
|
4596
5494
|
if (std.fs.path.dirname(file_path)) |parent| {
|
|
4597
|
-
cwd.createDirPath(io, parent) catch return
|
|
5495
|
+
cwd.createDirPath(io, parent) catch |err| return fileOpFailure(err);
|
|
4598
5496
|
}
|
|
4599
5497
|
cwd.writeFile(io, .{
|
|
4600
5498
|
.sub_path = file_path,
|
|
4601
|
-
.data =
|
|
4602
|
-
}) catch return
|
|
5499
|
+
.data = ctx.payload(),
|
|
5500
|
+
}) catch |err| return fileOpFailure(err);
|
|
4603
5501
|
return .ok;
|
|
4604
5502
|
},
|
|
4605
5503
|
.read => {
|
|
4606
5504
|
var file = cwd.openFile(io, file_path, .{}) catch |err| {
|
|
4607
|
-
return if (err == error.FileNotFound) .not_found else
|
|
5505
|
+
return if (err == error.FileNotFound) .not_found else fileOpFailure(err);
|
|
4608
5506
|
};
|
|
4609
5507
|
defer file.close(io);
|
|
4610
5508
|
// The buffer has one byte past the delivery bound:
|
|
4611
5509
|
// filling it proves the file is over-bound.
|
|
4612
|
-
const
|
|
4613
|
-
const len = file.readPositionalAll(io, buffer, 0) catch return .io_failed;
|
|
5510
|
+
const len = file.readPositionalAll(io, ctx.buffer, 0) catch |err| return fileOpFailure(err);
|
|
4614
5511
|
if (len > max_effect_file_bytes) {
|
|
4615
|
-
read_len
|
|
5512
|
+
ctx.read_len = max_effect_file_bytes;
|
|
4616
5513
|
return .truncated;
|
|
4617
5514
|
}
|
|
4618
|
-
read_len
|
|
5515
|
+
ctx.read_len = len;
|
|
4619
5516
|
return .ok;
|
|
4620
5517
|
},
|
|
4621
5518
|
}
|
|
4622
5519
|
}
|
|
4623
5520
|
|
|
5521
|
+
/// An interrupted blocking op reports `.cancelled` (only
|
|
5522
|
+
/// teardown cancels the task); every other failure stays the
|
|
5523
|
+
/// blanket `.io_failed`.
|
|
5524
|
+
fn fileOpFailure(err: anyerror) EffectFileOutcome {
|
|
5525
|
+
return if (err == error.Canceled) .cancelled else .io_failed;
|
|
5526
|
+
}
|
|
5527
|
+
|
|
4624
5528
|
/// The terminal file result must never be dropped: retry until
|
|
4625
5529
|
/// the loop thread drains space, giving up only on shutdown.
|
|
4626
5530
|
fn postFile(self: *Self, slot: *Slot, slot_index: u16, generation: u32, io: std.Io, outcome: EffectFileOutcome, read_len: usize) void {
|