From c4e9d031dde6607ca10f293764f14a5ef7df9774 Mon Sep 17 00:00:00 2001 From: "nekomimi.pet" Date: Thu, 29 Oct 2026 05:26:58 -0400 Subject: [PATCH] Discard late actor replies through monitored aliases --- .../references/state-and-concurrency.md | 2 ++ robot-docs/runtime.md | 6 ++++++ src/albedo/actor_call.gleam | 10 ++++++--- src/albedo/albedo_actor_call.erl | 17 +++++++++++++++ test/actor_call_test.gleam | 21 +++++++++++++++++++ test/albedo_actor_call_test_support.erl | 6 +++++- 6 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/albedo/albedo_actor_call.erl diff --git a/.agents/skills/gleam-design/references/state-and-concurrency.md b/.agents/skills/gleam-design/references/state-and-concurrency.md index f8d6ddc9e..aa34d988f 100644 --- a/.agents/skills/gleam-design/references/state-and-concurrency.md +++ b/.agents/skills/gleam-design/references/state-and-concurrency.md @@ -95,6 +95,8 @@ Track work identity and completion explicitly. Include a run or request ID, and Separate three errors: domain rejection, dependency failure, and process/transport failure. A reply type such as `Result(Value, DomainError)` does not make `process.call` return a timeout as `Error`: the inspected API returns the reply directly and can panic on transport failure. Check the installed version before selecting a wrapper. [S12] +Use a reply alias for finite calls when the caller survives a timeout. Gleam Erlang 1.3.0's `process.call` uses an ordinary subject, so removing its monitor does not reject late replies. Albedo's 1,000-timeout probe retained 1,000 mailbox entries and 62.5 MiB of reply binaries before alias retirement, and none afterwards. Retire the alias in a native `after` bracket and flush replies already delivered before retirement; test reply/death ordering too. + A timeout means the caller stopped waiting, not necessarily that the operation stopped. Cancellation also needs a real protocol. Where duplicate external effects matter, use operation IDs and an idempotency or reconciliation strategy at the authority. Do not retry a write merely because decoding its result failed: Pog explicitly performs the query before decoding returned rows. [S19] Give every queue a policy: bounded admission, rejection, batching, coalescing, pull-based demand, or a justified upper bound. Checking `Busy` only after processing a message does not bound the mailbox of already queued requests. Track active workers, queue growth, work age, and rejected/coalesced work as appropriate. No blanket queue-size threshold is supplied here. diff --git a/robot-docs/runtime.md b/robot-docs/runtime.md index 2fc8d7496..acc68852a 100644 --- a/robot-docs/runtime.md +++ b/robot-docs/runtime.md @@ -162,6 +162,12 @@ Reader connections use private SQLite caches with a 128 KiB suggested page-cache ## finite actor calls +`actor_call.try_call` uses a monitored process alias for its reply subject. +The native `albedo_actor_call.with_reply` bracket retires the alias and drains +already-delivered replies when the call returns or raises. A late reply is +then dropped before entering the caller's mailbox. The request itself is not +cancelled; domain operations must still handle an unconfirmed result. + `albedo/actor_call.gleam` owns monitored calls that return `TimedOut` or `CalleeDown` and remove their monitor on every outcome. Reuse `try_call` for finite waits instead of copying subject lookup, reply selection, and monitor diff --git a/src/albedo/actor_call.gleam b/src/albedo/actor_call.gleam index f332e76ec..08eccca6e 100644 --- a/src/albedo/actor_call.gleam +++ b/src/albedo/actor_call.gleam @@ -21,15 +21,13 @@ pub fn try_call( case process.subject_owner(subject) { Error(_) -> Error(CalleeDown) Ok(callee) -> { - let reply = process.new_subject() - let monitor = process.monitor(callee) + use reply, monitor <- with_reply(callee) process.send(subject, make_request(reply)) let answer = process.new_selector() |> process.select_map(reply, Ok) |> process.select_specific_monitor(monitor, fn(_) { Error(CalleeDown) }) |> process.selector_receive(timeout) - process.demonitor_process(monitor) case answer { Ok(outcome) -> outcome Error(_) -> Error(TimedOut) @@ -37,3 +35,9 @@ pub fn try_call( } } } + +@external(erlang, "albedo_actor_call", "with_reply") +fn with_reply( + callee: process.Pid, + run: fn(Subject(reply), process.Monitor) -> result, +) -> result diff --git a/src/albedo/albedo_actor_call.erl b/src/albedo/albedo_actor_call.erl new file mode 100644 index 000000000..df57789c7 --- /dev/null +++ b/src/albedo/albedo_actor_call.erl @@ -0,0 +1,17 @@ +-module(albedo_actor_call). +-export([with_reply/2]). + +%% The monitor doubles as a reply address. Retiring it drops later replies; +%% anything delivered before retirement must also leave the caller's mailbox. +with_reply(Callee, Run) -> + Alias = erlang:monitor(process, Callee, [{alias, demonitor}]), + try Run({subject, Alias, Alias}, Alias) + after + erlang:demonitor(Alias, [flush]), + flush(Alias) + end. + +flush(Alias) -> + receive {Alias, _} -> flush(Alias) + after 0 -> ok + end. diff --git a/test/actor_call_test.gleam b/test/actor_call_test.gleam index b39c3823a..6d9714199 100644 --- a/test/actor_call_test.gleam +++ b/test/actor_call_test.gleam @@ -95,5 +95,26 @@ pub fn finite_calls_remove_monitors_and_down_messages_test() -> Nil { monitor_state() |> should.equal(before) } +pub fn a_late_reply_cannot_accumulate_after_a_timeout_test() -> Nil { + let held = process.new_subject() + let subject = + server(fn(message) { + case message { + Ping(reply) -> process.send(held, reply) + Die -> Nil + } + actor.continue(Nil) + }) + let before = mailbox_size() + actor_call.try_call(subject, 20, Ping) + |> should.equal(Error(actor_call.TimedOut)) + let assert Ok(reply) = process.receive(held, 2000) + process.send(reply, "late") + mailbox_size() |> should.equal(before) +} + +@external(erlang, "albedo_actor_call_test_support", "mailbox_size") +fn mailbox_size() -> Int + @external(erlang, "albedo_actor_call_test_support", "monitor_state") fn monitor_state() -> #(Int, Int) diff --git a/test/albedo_actor_call_test_support.erl b/test/albedo_actor_call_test_support.erl index 015efc7c9..2fdd94a80 100644 --- a/test/albedo_actor_call_test_support.erl +++ b/test/albedo_actor_call_test_support.erl @@ -1,8 +1,12 @@ -module(albedo_actor_call_test_support). --export([monitor_state/0]). +-export([monitor_state/0, mailbox_size/0]). monitor_state() -> {monitors, Monitors} = process_info(self(), monitors), {messages, Messages} = process_info(self(), messages), Downs = [Ref || {'DOWN', Ref, process, _, _} <- Messages], {length(Monitors), length(Downs)}. + +mailbox_size() -> + {message_queue_len, Size} = process_info(self(), message_queue_len), + Size. -- knotmirror 0.0.1