lean4-htt/src/runtime/uv
Joachim Breitner c36b0fb165
refactor: make CancelToken Promise-based (#13303)
This PR moves `IO.CancelToken` from `Init.System.IO` to its own file
`Init.System.CancelToken`, backed by `IO.Promise Unit` instead of
`IO.Ref Bool`. This enables non-polling cancellation propagation: the
token's underlying promise can be used directly with `IO.waitAny`, and
callbacks can be registered to fire when cancellation is requested.

The structure carries both the promise *and* a plain `IO.Ref Bool` flag,
set in lockstep by `set`. `isSet` reads the flag directly (used on hot
paths like `Core.checkInterrupted`); `task`/`onSet` go through the
promise. The avoids a ~0.4% regression that a pure-promise
representation introduced.

API additions:

- `CancelToken.task : Task (Option Unit)`. Returns the underlying
promise's `result?` task directly — the same task object on every call,
so further `Task.map`/`BaseIO.bindTask` dependencies can be safely
attached. Resolves with `some ()` when `set` is called, or `none` if the
token is dropped without ever being set.
- `CancelToken.onSet : BaseIO Unit → BaseIO Unit`. Registers a callback
that runs synchronously on the cancelling thread when `set` is called
(or immediately if the token is already set). Implemented via
`BaseIO.chainTask` on `result?`, so no fresh `Task.map` per call and no
GC hazard.

Runtime cleanup:

- Add `LEAN_TASK_STATE_{WAITING,RUNNING,FINISHED}` constants in `lean.h`
matching `IO.TaskState`.
- Factor `lean::promise_is_resolved` inline in `object.h`, replacing
three open-coded `lean_io_get_task_state_core(...) == 2` checks (in
`interrupt.cpp`, `uv/timer.cpp`, `uv/signal.cpp`).
- Drop the manual `inc_ref(g_cancel_tk)` in `check_interrupted`; the
token is owned by the enclosing `scope_cancel_tk` for the duration of
the call (documented).
- Replace the bare `lean_always_assert(g_task_manager)` in
`lean_promise_new` with an explicit `lean_internal_panic` carrying a
message that names `Promise.new`, identifies the typical trigger
(`initialize` blocks, transitively via `IO.CancelToken.new`), and
recommends lazy construction. Without this, users got an opaque "LEAN
ASSERTION VIOLATION ... Condition: g_task_manager" with no actionable
hint.

Behavioural notes documented inline:

- `new` cannot be called from `initialize` blocks (task manager not
running yet); construct lazily.
- `task` documents the dropped-promise case (`none`) and steers callers
to `onSet` for callback chaining.

A consumer of `onSet` for parent → child cancel-token propagation in
parallel tactic combinators is in #13428 (fixes #13300).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-28 21:50:54 +00:00
..
dns.cpp fix: propagate memory errors when allocating for libuv (#13546) 2026-04-28 15:29:25 +00:00
dns.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
event_loop.cpp feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
event_loop.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
net_addr.cpp feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
net_addr.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
signal.cpp refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
signal.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
system.cpp fix: propagate memory errors when allocating for libuv (#13546) 2026-04-28 15:29:25 +00:00
system.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
tcp.cpp fix: propagate memory errors when allocating for libuv (#13546) 2026-04-28 15:29:25 +00:00
tcp.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
timer.cpp refactor: make CancelToken Promise-based (#13303) 2026-04-28 21:50:54 +00:00
timer.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00
udp.cpp fix: propagate memory errors when allocating for libuv (#13546) 2026-04-28 15:29:25 +00:00
udp.h feat: zero cost BaseIO (#10625) 2025-10-22 10:55:12 +02:00