Unsubscribing from inside a delivery (L4 graph)¶
Applies to the default reclaim_qsbr_t only
This is the one example whose subject is policy-dependent. It demonstrates a shape that
the default reclaim_qsbr_t supports and that reclaim_strict_t forbids, so it runs its
body under the default binding and skips under reclaim_strict_t, printing
skipped: this build forbids unsubscribing from inside a delivery and passing. The policy is
a build-time type rather than a CMake option (ADR-0068),
so the target is always built and always linked — it follows the binding with if constexpr,
exactly as core/tests/reclaim_test.cpp does for its own re-entrant cases. Every other
example on this site applies to both policies.
A subscriber callback that retires its own subscription is the one case where “the edge is
gone” and “the {fn, ctx} pair is dead” are not the same moment: the fan-out that invoked
the callback is still walking a snapshot that names that pair. The default reclaim_qsbr_t
policy retires the pair and runs its release hook once every dispatching thread has passed
a quiescent state. On this single-threaded example that is this thread’s own outermost
dispatch exit, before the write() that started the delivery hands control back
(ADR-0080,
Amendment 1).
What to notice¶
Retirement is immediate; release is not. The example’s second write reaches nobody — the next snapshot already skips the slot — yet the hook has not run when the callback returns. Both halves are asserted, and the run prints the flag as observed inside the callback.
The library decides which grace point applies, and the caller never asks. With no dispatch live the same call releases inline (see unsubscribe); with one live it defers. The signal is the hook in both cases — there is no poll and no verb the embedder must remember.
The policy is a build-time type, not a runtime flag. It is named by
default_config_t::reclaim_policy_tand bound inlibtracer/config_override.hpp, on the same seam pattern as the ACL policy and the LKV slot. The example printsreclaim_policy_t::kNameso a run says which one it exercised.reclaim_strict_tforbids this shape, and does not detect it. It is the zero-cost opt-out for NARROW bare metal with no preemption and no second core. It keeps no dispatch state, so no build can see a violation. The example branches onkDefersToDispatchExitwithif constexprand skips its body there — the constant is the portable way to ask “may I do this on this target?”, and asking it is the point. An example that does not apply to a binding must skip, never fail to compile, so the branch is a runtime skip rather than astatic_assert.The hook may run on another thread. On a node that dispatches from several threads, the participant whose quiescent state completes the grace period runs the hook, which may not be the thread that called
unsubscribe(). The hook must be short, thread-safe, non-blocking andnoexcept.Retirement is bounded. Retired pairs wait in one shared table sized by
kDeferredReleaseSlots; if every slot is taken the pair is dropped and its hook never runs — a deliberate leak in preference to a use-after-free. ThatunsubscribeanswersBACKPRESSURE, andgraph_t::reclaim_refusals().release_refusedcounts it.
Source¶
1/*
2 * SPDX-License-Identifier: Apache-2.0
3 * SPDX-FileCopyrightText: Copyright 2026 avatarsd LLC
4 */
5
6/**
7 * @file
8 * @brief Unsubscribing from INSIDE a delivery — the deferred grace point (ADR-0080).
9 *
10 * A callback that retires its own subscription cannot be told "the pair is dead" on the
11 * spot: the fan-out that called it is still walking a snapshot naming that `{fn, ctx}`
12 * pair. Under the default `reclaim_qsbr_t` the retired pair is DEFERRED in the QSBR domain's
13 * table, and the release hook runs at the grace point: here, with this thread the only one
14 * dispatching, that is when its dispatch stack unwinds to depth 0 — before the `write()`
15 * that started the delivery returns. With other threads dispatching, the hook would run on
16 * whichever of them completes the grace period. The retirement itself is immediate: the next
17 * fan-out skips the slot.
18 *
19 * `reclaim_strict_t` forbids this shape outright (and cannot detect it), so THIS EXAMPLE
20 * APPLIES TO THE DEFAULT POLICY ONLY. Both policies live in
21 * `libtracer/reclaim.hpp`, and the binding is a build-time type, so the example follows it
22 * with `if constexpr` and SKIPS its body where the policy forbids the shape (the same way
23 * `core/tests/reclaim_test.cpp` drops its re-entrant cases). A policy the example does not
24 * apply to must not break the build — every example compiles under every binding.
25 *
26 * Runs under ctest as `example_sub_unsubscribe_from_dispatch`; it self-checks and returns
27 * non-zero on any mismatch. Under `reclaim_strict_t` it prints a skip line and passes.
28 */
29
30#include <cstddef>
31#include <cstdint>
32#include <cstdio>
33
34#include "libtracer/tracer.hpp"
35
36namespace {
37
38using tr::graph::path_t;
39using tr::graph::role_t;
40
41/** @brief The subscriber's `ctx`: its own state plus what it needs to retire itself. */
42struct sink_t {
43 tr::graph::graph_t* g = nullptr; /**< @brief The graph to unsubscribe from. */
44 tr::graph::subscription_t sub{}; /**< @brief This subscription's handle. */
45 int seen = 0; /**< @brief Deliveries observed. */
46 bool released = false; /**< @brief Set by the release hook, exactly once. */
47 bool released_inside_cb = false; /**< @brief Was it already set when the callback left? */
48};
49
50/** @brief The release hook — libtracer calls it once, at the policy's grace point. */
51void on_release(void* ctx) { static_cast<sink_t*>(ctx)->released = true; }
52
53/** @brief A delivery that retires its own subscription (the re-entrant case). */
54void on_delivery(void* ctx, const tr::graph::value_t&) {
55 auto* s = static_cast<sink_t*>(ctx);
56 ++s->seen;
57 (void)s->g->unsubscribe(s->sub, &on_release);
58 s->released_inside_cb = s->released; // still parked here — dispatch has not unwound
59}
60
61/** @brief A one-byte VALUE view over @p b (one heap segment). */
62tr::view::view_t value_byte(std::uint8_t b) {
63 tr::view::segment_ptr_t seg = tr::view::heap_alloc(1);
64 seg->bytes[0] = std::byte{b};
65 return tr::view::view_t::over(std::move(seg));
66}
67
68/** @brief Record a failed expectation on @p ok and report it. */
69void check(bool& ok, bool cond, const char* what) {
70 if (!cond) {
71 std::printf(" [FAIL] %s\n", what);
72 ok = false;
73 }
74}
75
76/**
77 * @brief The example proper — retire a subscription from inside its own delivery.
78 *
79 * Written unguarded, because under the default policy every line of it is valid; the ONE
80 * policy branch lives in `main`, so what a reader studies here is the pattern itself.
81 *
82 * @return True when every expectation held.
83 */
84bool run_reentrant_unsubscribe() {
85 tr::graph::graph_t g;
86 const tr::graph::vertex_handle_t src =
87 g.register_vertex(path_t("/sensor/temp"), role_t::STORED_VALUE);
88
89 sink_t sink;
90 sink.g = &g;
91 sink.sub = *g.subscribe(path_t("/sensor/temp"), &on_delivery, &sink);
92
93 (void)g.write(src, value_byte(0x01)); // delivers, and the callback retires itself
94 std::printf("after write #1: seen=%d released=%d (inside the callback it was %d)\n", sink.seen,
95 static_cast<int>(sink.released), static_cast<int>(sink.released_inside_cb));
96 (void)g.write(src, value_byte(0x02)); // the slot is already retired
97
98 bool ok = true;
99 check(ok, sink.seen == 1, "the retire took effect at once — the second write reached nobody");
100 check(ok, sink.released, "the hook ran before write() returned");
101 check(ok, !sink.released_inside_cb,
102 "and not one moment earlier — the fan-out was still walking the snapshot");
103 return ok;
104}
105
106} // namespace
107
108int main() {
109 const auto policy = tr::graph::reclaim_policy_t::kName;
110 std::printf("reclamation policy bound by this build: %.*s\n", static_cast<int>(policy.size()),
111 policy.data());
112
113 bool ok = true;
114 if constexpr (tr::graph::reclaim_policy_t::kDefersToDispatchExit) {
115 ok = run_reentrant_unsubscribe();
116 } else {
117 // `reclaim_strict_t` FORBIDS the shape this whole example is about: running the body
118 // would release a context the running fan-out still names. The example is therefore
119 // skipped, not failed, and certainly not made to break the build: it simply does not
120 // apply to this binding. Read it against the default policy.
121 std::printf("skipped: this build forbids unsubscribing from inside a delivery\n");
122 }
123 std::printf("RESULT %s\n", ok ? "ok" : "FAILED");
124 return ok ? 0 : 1;
125}
See also: reclamation policy · configuration · graph module.