Skip to content

refactor(libcgroups): store DbusConnection socket as OwnedFd - #3630

Open
tkshsbcue wants to merge 3 commits into
youki-dev:mainfrom
tkshsbcue:refactor/dbus-owned-fd
Open

refactor(libcgroups): store DbusConnection socket as OwnedFd#3630
tkshsbcue wants to merge 3 commits into
youki-dev:mainfrom
tkshsbcue:refactor/dbus-owned-fd

Conversation

@tkshsbcue

Copy link
Copy Markdown
Contributor

Description

Follow-up to the review note in #3557 (#3557 (comment)).

DbusConnection stored its socket as a bare RawFd (socket: i32). The fd was
created in new() as a ManuallyDrop<OwnedFd> and then only the raw fd
(socket.as_raw_fd()) was kept. Since the OwnedFd was wrapped in ManuallyDrop
and there is no Drop impl on DbusConnection, the socket fd was never closed —
it leaked for the lifetime of the process.

This PR replaces the field with an owned OwnedFd:

  • ownership and lifetime are explicit and compiler-enforced
  • the socket is closed automatically via RAII when the connection drops (fixing the leak)
  • the ManuallyDrop in new() is no longer needed

The nix::sys::socket calls (send/recv/sendmsg/recvmsg/connect) still take a
RawFd, so they now pass self.socket.as_raw_fd(). There is no behavioural change to
the dbus protocol handling.

Note: the issue mentions removing a manual close() in a Drop impl. There is
currently no Drop impl in dbus.rs, so nothing needed to be removed — the
OwnedFd switch is what provides the (previously missing) cleanup.

Type of Change

  • Refactoring (no functional changes)
  • Bug fix (non-breaking change that fixes an issue) — closes a socket fd leak

Testing

  • Ran existing test suite

The existing #[cfg(feature = "systemd")] dbus tests in dbus.rs
(test_dbus_connection_auth, test_dbus_function_calls, …) exercise this path
unchanged. cargo +nightly fmt is clean.

Related Issues

Closes #3629

DbusConnection held its socket as a bare RawFd: connect() created the fd
as a ManuallyDrop<OwnedFd> and then stored socket.as_raw_fd(). Because the
OwnedFd was wrapped in ManuallyDrop and there was no Drop impl to close it,
the socket fd was effectively leaked for the lifetime of the process.

Store the socket as an OwnedFd instead. Ownership and lifetime are now
explicit and compiler-enforced, the fd is closed automatically when the
connection is dropped, and the ManuallyDrop dance in new() is gone. The
nix socket calls still take a RawFd, so they now pass self.socket.as_raw_fd();
behaviour is otherwise unchanged.

Closes youki-dev#3629

Signed-off-by: tkshsbcue <kumar.tanay@somaiya.edu>
@saku3 saku3 added the kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. label Jun 30, 2026
@nayuta723

Copy link
Copy Markdown
Contributor

@tkshsbcue
Please rebase it, when you have a moment.

@tkshsbcue

Copy link
Copy Markdown
Contributor Author

hey @nayuta723 just did that hope you can have a look
thanks!

@saku3

saku3 commented Jul 13, 2026

Copy link
Copy Markdown
Member

Please make sure the CI checks pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(libcgroups): let DbusConnection own OwnedFd instead of ManuallyDrop + RawFd

3 participants