Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

monitor_t is not (easily) usable in composition. #619

Open
3311anta opened this issue Sep 28, 2023 · 0 comments
Open

monitor_t is not (easily) usable in composition. #619

3311anta opened this issue Sep 28, 2023 · 0 comments

Comments

@3311anta
Copy link

3311anta commented Sep 28, 2023

There is a potential race condition when using monitor_t in an inheritance tree upon deallocation.

If the monitored socket is created in the child class, the monitor socket will be closed only after the monitored socket, introducing a potential race condition, because the monitored socket may get reaped by the time the deallocation is still ongoing, possibly resulting in undefined behavior when accessing the _Tag member to verify whether the socket being monitored is still valid or not.

It is therefor 'a good idea' to deallocate the monitor socket prior to the monitored socket.

This, however, is not easy to achieve with the inheritance model.

To remedy the issue, I'd like to suggest composition, with the monitor_t instance being cleaned up prior to the monitored socket.
This, however, is not easily possible, because 'process_event' is only available in a protected context and requires overloading.

I therefor suggest splitting the function into two parts:
get_event (public) would fetch the event and store it into a structure containing the zmq_event_t as well as the address.
process_event(protected) would call get_event to fetch the event and process it as usual, maintaining the API.

This way, the raw instance of monitor_t can be used in composition without subclassing.

I am attaching a suggestion: suggestion.zip

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

No branches or pull requests

1 participant