Skip to content

Autoremove EventHandler from parent entity #16

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

Merged

Conversation

ivanpauno
Copy link
Collaborator

Depends on #13.

I can add an equivalent test for subscription events after #14 gets in.

@ivanpauno ivanpauno added the enhancement New feature or request label Aug 20, 2020
@ivanpauno ivanpauno requested a review from jacobperron August 20, 2020 21:22
@ivanpauno ivanpauno self-assigned this Aug 20, 2020
Base automatically changed from ivanpauno/support-events-in-subscriptions to feature/events August 24, 2020 16:56
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
@ivanpauno ivanpauno force-pushed the ivanpauno/autoremove-eventhandler-from-parent branch from 8648aba to bb403a3 Compare August 25, 2020 14:33
@ivanpauno ivanpauno marked this pull request as ready for review August 25, 2020 14:33
}
this.removeCallback.accept(this);

Choose a reason for hiding this comment

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

Instead of having the parent register a callback, what about instead we call the parent's removeEventHandler() method? This is a pattern I choose when implementing "remove" methods for Node entities like publishers and subscriptions in ros2-java#110; the node removes entities from it's list (and does not dispose them), but if dispose() is called on the entity it removes itself from it's parent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I didn't follow that approach because of the following problem:

Publisher pub = node.createPublisher(...);
node.dispose();
pub.dispose();  // Use after free here, likely a segfault

My idea is that the above code should work perfectly, pub.dispose() should only be a noop in that case.

In the following example:

Publisher pub = node.createPublisher(...);
node.dispose();
pub.publish();  // I would like an InvalidHandleException thrown here

I would like to see what the inline comment says 😂.

I think that the current implementation of event handlers is solving both problems correctly.
It's a bit annoying to pass the remove callback, but that's an implementation detail that the user doesn't have to know about (and without the callback, there's an infinite recursion problem)

Choose a reason for hiding this comment

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

Hmm.. it looks like both your examples are handled in the approach I proposed.

If I try:

node.dispose()
pub.dispose()

The program exits cleanly (I believe the pub.dispose() is a noop).

If I try:

node.dispose()
pub.publish()

Then I get the following exception:

Exception in thread "main" org.ros2.rcljava.exceptions.RCLException: Failed to publish: publisher pointer is invalid

I think the exception message is fairly informative, but of course we could change the type like you suggest.

Choose a reason for hiding this comment

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

We can go forward with the callback approach if you like; I just thought that avoiding the callback registration seemed a bit cleaner.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I have to double check how the approach works, I was almost sure there was an use after free.

Choose a reason for hiding this comment

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

hmm, I just took another look at the code. I thought we were handling it in Java, but it looks like I may have just got lucky . I'm guessing rcl or rmw is failing gracefully on the publish call. I'm not sure what's going on with the dispose call 🙃. We can leave this PR as-is and I'll have to revisit my approach in ros2-java#110 (with some more unit tests).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

node.dispose()
pub.dispose()

There isn't actually an use after free, but the publisher is leaked

if (node_handle == 0) {
// TODO(esteve): handle this, node is null, but publisher isn't
return;
}
.

About:

node.dispose()
pub.publish()

I think that one is UB, as the node has been destroyed and not the publisher (it likely depends on the rmw implementation).

We can leave this PR as-is and I'll have to revisit my approach in ros2-java#110 (with some more unit tests).

👍

Signed-off-by: Ivan Santiago Paunovic <[email protected]>
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
@ivanpauno ivanpauno merged commit 9b1ad76 into feature/events Aug 26, 2020
@ivanpauno ivanpauno deleted the ivanpauno/autoremove-eventhandler-from-parent branch August 26, 2020 20:56
ivanpauno added a commit that referenced this pull request Aug 31, 2020
ivanpauno added a commit that referenced this pull request May 17, 2021
ivanpauno added a commit to ros2-java/ros2_java that referenced this pull request Jan 14, 2022
ivanpauno added a commit to ros2-java/ros2_java that referenced this pull request Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants