-
Notifications
You must be signed in to change notification settings - Fork 111
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
ndk/looper: Add remove_fd()
method
#416
Conversation
We've previously only been able to unregister file descriptors by using a callback and returning `false` from it to be unregistered. Add the missing method that allows users to unregister their file descriptors from the looper for any reason. There has always been a "concern" about leaking the `Box` inside `add_fd_with_callback()`, but this is already very easy by never returning `false` from the callback or registering a different callback / event on the same `fd`, so this new function doesn't really expose that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me.
Maybe for the issue with leaking the Box there could be some way of having add_fd_with_callback
return some form of handle that could then be used to remove that callback, which could also make sure to drop the Box.
Not sure how that would work with respect to how removeFd doesn't guarantee that there can't be more calls to the callback after removing the fd.
I wonder if it's possible to get better guarantees with addFd(fd, callback=null)
to explicitly clear the callback before removeFd
Skimming the implementation I'm not super sure: https://android.googlesource.com/platform/system/core/+/master/libutils/Looper.cpp#432
Perhaps we could, since a Alternatively we could keep a Anyway I tried to not mention (in the docs) the fact that it might be hard to trigger the callback just to return |
Regardless, leaking memory isn't unsafe/UB (these APIs playing with random |
One of the risks with this kind of stuff is that technically other non-Rust code, (or potentially even Rust code that has ended up linking in multiple versions of the ndk crate) could end up messing up this kind of state tracking. This is because the underlying loopers are shared via TLS but this state may only be associated with a single |
I intentionally didn't bring that up. Many parts of the |
We've previously only been able to unregister file descriptors by using a callback and returning
false
from it to be unregistered. Add the missing method that allows users to unregister their file descriptors from the looper for any reason.There has always been a "concern" about leaking the
Box
insideadd_fd_with_callback()
, but this is already very easy by never returningfalse
from the callback or registering a different callback / event on the samefd
, so this new function doesn't really expose that.