Skip to content

Add SubscriptionStateChangeHandler support#390

Open
crimson11 wants to merge 5 commits intomainfrom
mf_subscription_state_change_handler
Open

Add SubscriptionStateChangeHandler support#390
crimson11 wants to merge 5 commits intomainfrom
mf_subscription_state_change_handler

Conversation

@crimson11
Copy link
Copy Markdown
Contributor

Although we had already an explicit requirement for the support of ProxyEvent/Field side
SetSubscriptionStateChangeHandler() and the related Unset-API implementation was still
missing.

This PR provides the implementation and updates the requirements and design accordingly.

@crimson11 crimson11 force-pushed the mf_subscription_state_change_handler branch 6 times, most recently from d25466a to a376f74 Compare May 6, 2026 21:25
"Invalid subscription state machine state.");
return SubscriptionState::kSubscribed;
}
return SubscriptionStateMachineState2SubscriptionState(current_state);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like this name but i think we do not follow this convention of using a digit in the Method names,
best to use SubscriptionStateMachineStateToSubscriptionState

auto subscription_state_change_handler = [](SubscriptionState new_subscription_state) {
return true;
};
state_machine_.SetSubscriptionStateChangeHandler(subscription_state_change_handler);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i dont see any explicit expectations or conditions in this test,
does having this really add value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment, which clarifies the need/idea.

Comment thread score/mw/com/impl/proxy_event_base.h Outdated
* \brief Unsets/Unregisters a SubscriptionStateChangeHandler for this event. After this method returns, it is
* guaranteed, that the previously registered handler is neither active nor will be called anymore.
*/
Result<void> UnSetSubscriptionStateChangeHandler() noexcept;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor nitpick ;)
we have UnSet upper case S in the public API and other places have lowercase S,


/* broken_link_c/issue/21466236 */
ScoreReq.CompReq BehaviourOfUnsetSubscriptionStateChangeHandler {
description = '''{{UnsetSubscriptionStateChangeHandler}} {{UnsetSubscriptionStateChangeHandler}}{{SubscriptionStateChangeHandler}}will unregister the {{SubscriptionStateChangeHandler}} (See SubscriptionStateChangeHandler) that was registered by the class instance with a call to {{SetSubscriptionStateChangeHandler}} After calling the will no longer be triggered when the {{SubscriptionState}} (See progress-cursor SubscriptionState changes.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not changed in this PR,
but is here
{{UnsetSubscriptionStateChangeHandler}} {{UnsetSubscriptionStateChangeHandler}}{{SubscriptionStateChangeHandler}}
is expected to be like this ?
{{UnsetSubscriptionStateChangeHandler}} {{SubscriptionStateChangeHandler}}
?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is broken anyhow :) because this is copied over from AUTOSAR requirements - I do not even get all these curly braces! But I cleaned up the mess a bit ;)


/**
* \api
* \brief Sets/Registers a SubscriptionStateChangeHandler for this event. This handler will be called whenever the
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick,
should this be called as field ?
or since it dispatches to the event_base it is still accurate to call this a event?
(from other methods we use field instead of event even if we dispatch to the event_base.

/// might also require the same lock, which is not guaranteed to be recursive, thus leading to a deadlock. If the user
/// intends to do such activities from this callback, he shall dispatch it to a separate thread.
/// However, unsetting the handler within its call is a reasonable/supported use-case. Instead of calling
/// UnsetSubscriptionStateChangeHandler, the user shall return false from the handler. See return-value description.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eventhough we warn users about the consequence of deadlock,
is it better or worth doing something like we do with "is_in_receive_handler_context" if we are in the same ctx then we dont acquire the lock to prevent deadlock?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking/verifying it comes at additional costs! We need to manage a thread-local variable to monitor the mis-use.
Here we are basically adding an AoU - like many other AoUs we have and where we expect the user to adhere to. I.e.: We disallow, that a user calls APIs of a mw::com proxy/skeleton/event/field instance concurrently. We do NOT supervise this ... although we could do this! So - it is the same here! We don't supervise, that the user follows our AoU.

SetReceiveHandler is a "different animal". Because there we explicitly allow, that the user calls UnsetReceiveHandler from within the handler-context! Thus, we have to detect it, because we then have different code paths for UnsetReceiveHandler depending on the context. And the only reason is: In case of ReceiveHandler I wasn't smart enough to come up with the same idea as in SubscriptionStateHandler -> if you want "unset" it, just tell it via return-value. The underlying code is so much more effective! I'm tempted to change the signature/idea for ReceiveHandler maybe the same at some time ...

crimson11 added 5 commits May 8, 2026 20:06
Implementation of ProxyEvent/Field
SetSubscriptionStateChangeHandler and the corresponding
Unset-API.

Introduce SubscriptionStateChangeHandler with revised
signature, which supports implicit Unset via return
value.
Updated requirements for
SubscriptionStateChangeHandler
Updated design to clarify, how SubscriptionStateChange
handlers work.
The API to set/unset a SubscriptionStateChangeHandler
is now also added to the Field.
Incorporated review comments:
Fixed naming to always use UnsetStateChangeHandler().
Extended unit tests.
@crimson11 crimson11 force-pushed the mf_subscription_state_change_handler branch from 63b6c36 to 3fe9989 Compare May 8, 2026 18:06
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