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

Replace FdGuard with OwnedFd #234

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

WhyNotHugo
Copy link
Contributor

An OwnedFd provides the same guarantees: that the underlying file descriptor is closed only once. The file descriptor is behind an Arc, so it will be closed once the last copy of the Arc is dropped.

Events and Watches now have a lifetime referencing the original file descriptor, since they have a BorrowedFd referencing it.

Pending issues

A Stream implementation cannot return items referencing itself. The Stream currently owns the buffer, so items have a lifetime referencing the stream Stream. I'm not yet sure how to resolve this.

This is not a problem for the Iterator implementation because the iterator (Inotify) does not own the buffer.

An OwnedFd provides the same guarantees: that the underlying file
descriptor is closed only once. The file descriptor is behind an Arc, so
it will be closed once the last copy of the Arc is dropped.

Events and Watches now have a lifetime referencing the original file
descriptor, since they have a BorrowedFd referencing it.
@WhyNotHugo WhyNotHugo marked this pull request as draft January 17, 2025 21:44
@WhyNotHugo WhyNotHugo changed the title Draft: Replace FdGuard with OwnedFd Replace FdGuard with OwnedFd Jan 17, 2025
Copy link
Owner

@hannobraun hannobraun left a comment

Choose a reason for hiding this comment

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

Hey @WhyNotHugo, thank you again for the new pull request!

I had a quick look, left some comments. Not sure where to go from here, to be honest. Maybe we can have a BorrowedFd for the regular event, but use an Arc<OwnedFd> for EventOwned? Seems a bit more complex than I'd like 🤷

@@ -18,15 +16,15 @@ use crate::watches::WatchDescriptor;
/// [`Inotify::read_events_blocking`]: crate::Inotify::read_events_blocking
/// [`Inotify::read_events`]: crate::Inotify::read_events
#[derive(Debug)]
pub struct Events<'a> {
fd: Weak<FdGuard>,
pub struct Events<'a, 'i> {
Copy link
Owner

Choose a reason for hiding this comment

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

Adding a second lifetime seems redundant with the one that's already there. Aren't they both the same lifetime, in the end?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One lifetime references the buffer, the other references the fd. They're not the same, but it's likely that we can use just one.

Copy link
Owner

Choose a reason for hiding this comment

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

Ah, and what I was missing, is that those two things only live together in EventStream, but not in Inotify. Sorry for the noise.

@@ -188,7 +186,7 @@ impl<'a> Event<&'a OsStr> {
}

/// An owned version of `Event`
pub type EventOwned = Event<OsString>;
pub type EventOwned<'i> = Event<'i, OsString>;
Copy link
Owner

Choose a reason for hiding this comment

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

Ah, so we already have an owned event, like I was suggesting in the other issue 😄

By adding a lifetime to it, it's no longer owned though, which defeats the purpose of having it in the first place. That's also the source of your problem with the stream, I'd say. I guess whoever wrote this originally had the same problem, and introduced EventOwned to solve it (a solution that you're undoing here).

I'm not sure exactly what to do. Maybe make Event and WatchDescriptor generic over the kind file descriptor type used, just like Event is already generic over the string type?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Our options here are:

  • Remove the fd from EventOwned.
  • Keep only a raw_fd in EventOwned.

@WhyNotHugo
Copy link
Contributor Author

Maybe we can have a BorrowedFd for the regular event, but use an Arc for EventOwned? Seems a bit more complex than I'd like 🤷

We can't implement into_raw_fd if we have an Arc<OwnedFd>, because we no longer own the OwnedFd.

@hannobraun
Copy link
Owner

We can't implement into_raw_fd if we have an Arc<OwnedFd>, because we no longer own the OwnedFd.

Yeah, that's a good point.

I'm trying to help, but I've noticed multiple times now, the limited time that I have available for this project isn't enough to think through this problem. Sorry, I wish I could do more, but it is what it is.

I'll be here to review whatever you come up with. But I think it'll fall to you (or any other contributor who's interested; it's not like this is your problem any more than it is mine 😄) to come up with a solution, and explain that to me well enough, that I feel confident pressing "Merge" in the end.

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