-
Notifications
You must be signed in to change notification settings - Fork 9
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -64,16 +64,19 @@ public class EventHandlerImpl< | |||||||||
* @param eventStatusFactory Factory of an event status. | ||||||||||
* @param callback The callback function that will be called when the event | ||||||||||
* is triggered. | ||||||||||
* @param removeCallback Callback that will be called when this object is being disposed. | ||||||||||
*/ | ||||||||||
public EventHandlerImpl( | ||||||||||
final WeakReference<ParentT> parentReference, | ||||||||||
final long handle, | ||||||||||
final Supplier<T> eventStatusFactory, | ||||||||||
final Consumer<T> callback) { | ||||||||||
final Consumer<T> callback, | ||||||||||
final Consumer<EventHandler> removeCallback) { | ||||||||||
this.parentReference = parentReference; | ||||||||||
this.handle = handle; | ||||||||||
this.eventStatusFactory = eventStatusFactory; | ||||||||||
this.callback = callback; | ||||||||||
this.removeCallback = removeCallback; | ||||||||||
} | ||||||||||
|
||||||||||
/** | ||||||||||
|
@@ -102,9 +105,11 @@ public final long getHandle() { | |||||||||
* {@inheritDoc} | ||||||||||
*/ | ||||||||||
public synchronized final void dispose() { | ||||||||||
if (this.handle != 0) { | ||||||||||
nativeDispose(this.handle); | ||||||||||
if (this.handle == 0) { | ||||||||||
return; | ||||||||||
} | ||||||||||
this.removeCallback.accept(this); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 If I try: node.dispose()
pub.publish() Then I get the following exception:
I think the exception message is fairly informative, but of course we could change the type like you suggest. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ros2_java/rcljava/src/main/cpp/org_ros2_rcljava_publisher_PublisherImpl.cpp Lines 74 to 77 in 6291847
About:
I think that one is UB, as the node has been destroyed and not the publisher (it likely depends on the rmw implementation).
👍 |
||||||||||
nativeDispose(this.handle); | ||||||||||
this.handle = 0; | ||||||||||
} | ||||||||||
|
||||||||||
|
@@ -126,11 +131,12 @@ public synchronized final void executeCallback() { | |||||||||
nativeTake(this.handle, nativeEventStatusHandle); | ||||||||||
eventStatus.fromRCLEvent(nativeEventStatusHandle); | ||||||||||
eventStatus.deallocateRCLStatusEvent(nativeEventStatusHandle); | ||||||||||
callback.accept(eventStatus); | ||||||||||
this.callback.accept(eventStatus); | ||||||||||
} | ||||||||||
|
||||||||||
private final Supplier<T> eventStatusFactory; | ||||||||||
private final WeakReference<ParentT> parentReference; | ||||||||||
private long handle; | ||||||||||
private final Consumer<T> callback; | ||||||||||
private final Consumer<EventHandler> removeCallback; | ||||||||||
} |
Uh oh!
There was an error while loading. Please reload this page.