Skip to content

fix(agent): replace a subscription the RIC installs a second time - #25

Draft
Thecave3 wants to merge 7 commits into
duranta-project:devfrom
Thecave3:agent-replace-subscription-on-reinstall
Draft

Thecave3 wants to merge 7 commits into
duranta-project:devfrom
Thecave3:agent-replace-subscription-on-reinstall

Conversation

@Thecave3

@Thecave3 Thecave3 commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Sits on #19, beside #20; stack, oldest first: #36 → #18 → #19 → #25. It also carries a copy of the commit of #8. Until the PRs before it merge the diff here also contains their commits; only the last one belongs to this PR.

A RIC_SUBSCRIPTION_REQUEST for a RIC ID the agent already holds leaves two subscriptions under one key.

Why it happens

assoc_rb_tree_insert() does not reject equality, so ag->ind_event accepts a second entry under the same ric_gen_id_t, each with its own timer and action definition. A near-RT RIC produces that when the delete of the first subscription never arrived, or by reusing one Requestor ID, as the O-RAN SC near-RT RIC does (const ricRequestorId = 123 in pkg/control/control.go).

Keying subscriptions on their fd already makes such duplicates survivable. This stops them forming.

The change

e2ap_handle_subscription_request_agent() releases the subscription already installed under the request's RIC ID, if there is one, before installing the new one. It runs before on_subscription(): an aperiodic entry's free_subs_aperiodic() lets the RAN un-register the RIC ID, which on_subscription() may have just registered, so releasing afterwards could undo the new subscription.

Behaviour change

A re-install now tears down the previous subscription and its timer. Before, both stayed live. A RIC that wants two subscriptions has to use two RIC IDs, which is what E2AP expects.

Tests

test/agent/test_replace_subscription.c is new. It runs the handler twice with the same RIC ID over a stub service model and checks one entry is left and the first was released, then that a different RIC ID adds a second entry. It fails on the previous handler (two entries).

Verified only on E2AP_V3/KPM_V3_00: the new test, test_setup_response and test_association_loss pass. The full tree and the other two CI combinations were not built for this branch.

@Thecave3

Copy link
Copy Markdown
Collaborator Author

This PR is part of a stack and is open as a draft for visibility only; it is not ready for review yet.

It is based on #19, so its diff also contains the commits of the PRs below it until those merge. The stack, bottom to top: #16, #18, #19, then this one. Please review in that order; the commit specific to this PR is the last one in the list.

It will be rebased and marked ready for review once the PR below it has merged.

@Thecave3
Thecave3 force-pushed the agent-replace-subscription-on-reinstall branch from 7677880 to ee3174a Compare October 28, 2026 16:05
Thecave3 and others added 7 commits November 26, 2026 08:38
e2_async_event_agent() resolved the subscription an aperiodic indication
answers with eq_ind_event_ric_req_id, which compares ric_req_id only.
ag->ind_event is keyed on the full ric_gen_id_t and can hold several entries
sharing a RIC Requestor ID: E2AP identifies a subscription by
{RequestorID, InstanceID}, and the O-RAN SC near-RT RIC's subscription manager
sets one fixed Requestor ID on every subscription it sends (ricRequestorId =
123, pkg/control/control.go).

First match wins, and cmp_ric_gen_id orders ran_func_id descending, so the
highest-numbered RAN function under that Requestor ID captures every aperiodic
indication. Its service model is then handed another model's ind_data, decodes
a foreign struct and aborts the RAN process.

Carry ric_gen_id_t end to end (sm_subs_data_t, wr_rc_sub_data_t, the async API,
free_aper_subs) and match on it with the existing eq_ind_event. Delete
eq_ind_event_ric_req_id, which has no correct use.

The aperiodic fd had the same defect. It was derived as -ric_req_id, so two
subscriptions sharing a Requestor ID collide on the bi_map key, and Requestor
ID 0 yields fd 0, which stop_ind_event() reads back as a real fd. Count down
from -1 instead.

An indication for a subscription that was just deleted is a race the RAN can
lose legitimately, so log and drop it instead of asserting.

It is dormant with FlexRIC's own near-RT RIC, which allocates a new Requestor
ID per subscription.

test/aperiodic_routing/ routes three subscriptions under one Requestor ID and
fails on the Requestor-ID-only matching.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
ag->ind_event is a multiset: assoc_rb_tree_insert does not reject equality and
cmp_ind_event compared RIC IDs only, so two subscriptions can sit under one
ric_gen_id_t. A near-RT RIC produces that by installing a subscription the
agent still holds, or by reusing one Requestor ID, as the O-RAN SC one does.

A delete then corrupted the registry. stop_ind_event() found its entry with a
linear scan (find_if_rb_tree) but unlinked with a binary search on the same
ambiguous key (bi_map_extract_right), and those are not the same node. One
entry's act_def was freed while another was unlinked, so the survivor kept an
armed timer and a freed act_def, which the next tick handed to the service
model.

The fd already identifies a subscription: a timerfd when periodic, a negative
synthetic one when aperiodic. Record it in ind_event_t, break ties on it in
cmp_ind_event so the tree is a total order, and extract by it with
bi_map_extract_left. eq_ind_event still matches on the RIC ID alone, which is
what lets e2_async_event_agent() find a subscription without knowing its fd.

test/ind_event_key/ installs three subscriptions under one RIC ID and deletes
them; on the previous keying one act_def is freed twice.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
A second E2 SETUP RESPONSE aborts the RAN process:

    assoc_rb_tree.c:457: assoc_rb_tree_extract: Assertion `z_node !=
      tree->dummy && "Trying to extract a key not found in the tree"' failed.

stop_pending_event() calls bi_map_extract_right(), which asserts that the key
is present. The first response removes the pending timer entry; a second one
tries to remove it again and aborts. A second assert sits right behind it,
since the first response zeroes trans_id_setup_req. Both assert on what a peer
sent rather than on an internal invariant, so a RIC that answers a setup twice
takes down the RAN.

stop_pending_event() now probes before extracting and reports whether it found
anything, and the handler ignores the message when no request is outstanding.
The probe mirrors stop_ind_event(), which has always checked it_r != end_r.

The transaction ids are still reported and not compared: not every RIC echoes
the id it was sent, and rejecting the mismatch would drop the only response
such a peer sends.

test/agent/ reproduces the abort on the unpatched handler. Built and tested
for E2AP_V1/KPM_V2_01, E2AP_V2/KPM_V2_03 and E2AP_V3/KPM_V3_00.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
(cherry picked from commit eebef0e)
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
stop_ind_event() unlinked a subscription from both trees without holding
ag->mtx_ind_event, although insert takes it and e2_async_event_agent()
traverses the right tree under it from the RAN threads. A delete could
free a node while one of them was walking it.

Split it into unlink_ind_event(), which holds the mutex while it unlinks,
and release_ind_event(), which stops the producer and hands the payload
back with the mutex released, since free_act_def and free_subs_aperiodic
are service model and RAN code. The producer is now stopped before its
payload is released, not after.

No other change in behavior. This is groundwork to drop every
subscription at once.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
On SCTP_SHUTDOWN_EVENT the agent only armed the E2 SETUP REQUEST retry.
Every installed subscription kept its timer and its action definition, so
the agent went on producing indications for a RIC that had forgotten them,
and the RAN's own registry kept the aperiodic ones, since
free_subs_aperiodic() is only reachable from the delete path. E2AP keeps no
subscription state across an association.

stop_all_ind_events() now drops every subscription before the retry is
armed. The teardown is gated on SCTP_SHUTDOWN_EVENT: every SCTP
notification reaches notification_handle_ag(), and cp_sctp_notification()
already decodes SCTP_COMM_UP, which must not tear anything down.

The retry is also armed only when none is pending, since pend_event()
asserts that the map holds exactly one entry and a second notification
before the E2 SETUP RESPONSE would abort the RAN on the next timer tick.

test_association_loss fails without the change.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
notification_handle_ag() ended the E2 session on SCTP_SHUTDOWN_EVENT alone,
and the agent did not subscribe to association events, so a SHUTDOWN was
the only notification that could arrive. A nearRT-RIC that goes away
without one (its host or the network fails, or it sends an ABORT) or that
comes back under the same addresses produces no SHUTDOWN: the kernel
reports SCTP_COMM_LOST or SCTP_RESTART, and only to a socket that asked
for association events.

Neither was seen, so subscriptions stayed installed and no E2 SETUP REQUEST
was sent again. The socket is one-to-many, so the next indication silently
opened a new association to a restarted RIC with no E2 setup on it.

Subscribe to association events, and end the session on SCTP_COMM_LOST
and SCTP_RESTART as on SCTP_SHUTDOWN_EVENT. SCTP_COMM_UP,
SCTP_CANT_STR_ASSOC and SCTP_SHUTDOWN_COMP are not an end and stay
ignored. No other event type is subscribed, since cp_sctp_notification()
asserts on the ones it does not decode.

test_association_loss covers the six association outcomes and fails
without the change.

Co-authored-by: Andrea Lacava <thecave003@gmail.com>
Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
assoc_rb_tree_insert does not reject equality, so a RIC_SUBSCRIPTION_REQUEST
for a RIC ID the agent already holds leaves two entries under one key, each
with its own timer and action definition. A near-RT RIC produces that when the
delete of the first never arrived, or by reusing one Requestor ID, as the
O-RAN SC one does.

Release the existing subscription before installing the new one, so a
re-install replaces it. It runs before on_subscription(): an aperiodic entry's
free_subs_aperiodic() lets the RAN un-register the RIC ID, which
on_subscription() may have just registered.

Keying on the fd already makes such duplicates survivable. This stops them
forming.

test/agent/test_replace_subscription.c installs the same RIC ID twice and
expects one entry and one release. It fails on the previous handler.

Assisted-by: Claude:claude-sonnet-5-5
Signed-off-by: Andrea Lacava <thecave003@gmail.com>
@Thecave3
Thecave3 force-pushed the agent-replace-subscription-on-reinstall branch from ee3174a to da9a196 Compare November 27, 2026 13:59

This branch has not been deployed

No deployments
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.

2 participants