Skip to content

feat(async): [breaking] align future API with copas.future - #6

Merged
Tieske merged 9 commits into
masterfrom
feat/align-future-api
Mar 27, 2026
Merged

Tieske merged 9 commits into
masterfrom
feat/align-future-api

Conversation

@Tieske

@Tieske Tieske commented Mar 26, 2026

Copy link
Copy Markdown
Owner
  • get() now returns pcall-style: true+results on success, false+errmsg on error
  • try() now returns 3-state: PENDING(false)/SUCCESS(true)/ERROR('error') with results
  • Replace assert-on-concurrent-access with copas.semaphore; multiple coroutines can now call get() concurrently and all are released when the result arrives
  • Wrap fn() in pcall in the Lanes thread so task errors are captured and returned via get()/try() instead of being silently lost
  • Add SUCCESS, PENDING, ERROR constants to the module (matching copas.future)
  • Add module __index guard that errors on unknown field names (typo protection)
  • Update io_popen internals: prepend true ok-flag in awake_future calls, strip it with select(2,...) on the consumer side
  • Replace deprecated copas.sleep(-1) with copas.pauseforever()

@Tieske
Tieske force-pushed the feat/align-future-api branch 2 times, most recently from 54aa0f9 to 53025eb Compare March 27, 2026 12:09
Tieske added 3 commits March 27, 2026 18:30
- get() now returns pcall-style: true+results on success, false+errmsg on error
- try() now returns 3-state: PENDING(false)/SUCCESS(true)/ERROR('error') with results
- Replace assert-on-concurrent-access with copas.semaphore; multiple coroutines
  can now call get() concurrently and all are released when the result arrives
- Wrap fn() in pcall in the Lanes thread so task errors are captured and
  returned via get()/try() instead of being silently lost
- Add SUCCESS, PENDING, ERROR constants to the module (matching copas.future)
- Add module __index guard that errors on unknown field names (typo protection)
- Update io_popen internals: prepend true ok-flag in awake_future calls,
  strip it with select(2,...) on the consumer side
- Replace deprecated copas.sleep(-1) with copas.pauseforever()
In busted, _G.unpack is overridden with a non-transferable function.
Introduce _G._TEST flag (set in before_each, cleared in after_each) to
switch to a pure recursive unpack with no external upvalues, which Lanes
can safely transfer to OS threads on any Lua version.
@Tieske
Tieske force-pushed the feat/align-future-api branch from 53025eb to 54eb1d8 Compare March 27, 2026 17:30
Tieske added 5 commits March 27, 2026 18:43
Split the multi-line @return into separate tags so ldoc renders them
as a proper list instead of a literal code block. Added a @Usage
example showing the polling pattern with error handling.
Adds future:cancel() matching the copas.future API. Cancels the Lanes
thread (best-effort), releases any coroutine blocked in get() via the
wakeup mechanism, and releases semaphore waiters. The lane is now passed
into new_future as a parameter rather than set on the future afterwards.
Adds future:cancel() matching the copas.future API. The Copas side is
always cancelled immediately: results are set to "cancelled", any
coroutine blocked in get() is woken via the wakeup mechanism, and
semaphore waiters are released.

The underlying Lanes thread is soft-cancelled first. If it is still
running after async.cancel_timeout seconds (default 5s, e.g. when
blocked in a C call like socket.sleep), it is force-killed via
pcall(lane:cancel, lane, 0, true). cancel() also checks the Linda
for an already-arrived result before deciding to cancel, so it
correctly returns false when the thread has finished even if get()/try()
was never called.

Tests cover: cancel while pending, cancel after done, cancel releasing
a blocked get(), and the force-kill firing after cancel_timeout.
Fix os_execute call in example.lua to capture the pcall-style ok flag.
Update the expected output block to match actual output. Rewrite
example-popen.lua to uncomment and clean up the examples, replacing
socket.sleep with copas.sleep and adding descriptive comments.
@Tieske
Tieske force-pushed the feat/align-future-api branch from cf88614 to fe1ce97 Compare March 27, 2026 17:44
@Tieske
Tieske force-pushed the feat/align-future-api branch from fe1ce97 to 669f837 Compare March 27, 2026 17:46
@Tieske
Tieske marked this pull request as ready for review March 27, 2026 17:47
@Tieske Tieske changed the title feat(async): align future API with copas.future feat(async): [breaking] align future API with copas.future Mar 27, 2026
@Tieske
Tieske merged commit 48d7554 into master Mar 27, 2026
7 checks passed
@Tieske
Tieske deleted the feat/align-future-api branch March 27, 2026 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant