Skip to content

Fix leaks on failed call_method and get_property - #124

Open
peturingi wants to merge 1 commit into
systemd:mainfrom
peturingi:fix-dbus-error-path-leaks
Open

peturingi wants to merge 1 commit into
systemd:mainfrom
peturingi:fix-dbus-error-path-leaks

Conversation

@peturingi

Copy link
Copy Markdown

When sd_bus_call fails, DBus.call_method raises before unref-ing the call message and freeing the sd_bus_error. Any file descriptor appended to the message (h) leaks with it, so a long-running process that makes failing calls eventually hits EMFILE. DBus.get_property never freed its error at all.

This releases both in a finally block and adds an e2e test that makes 100 failing calls with an fd argument and checks the process fd count stays flat.

Measured on Debian trixie (systemd 257, Python 3.13), 20,000 failing StartTransientUnit calls with an fd argument:

open fds max RSS
before 20,007 51 MB
after 7 15 MB

The new test fails on main (116 fds vs. 16) and passes with the fix; the existing unit tests pass.

🤖 Generated with Claude Code

When sd_bus_call failed, call_method raised before unref-ing the call
message and freeing the sd_bus_error. Any file descriptor appended to the
message (type "h") was leaked with it, so a long-running process making
failing calls eventually hit EMFILE. get_property never freed its error.

Release both in a finally block, and add an e2e test that makes 100
failing calls with an fd argument and checks the fd count stays flat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI balanced review requested due to automatic review settings October 18, 2026 03:38

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

Cleanup preserves ownership and exception handling, with no blocking issues identified; tests were not executed during review.

No open issues 🎉

What changed in this PR

Fixes resource leaks in the D-Bus wrapper when method calls or property reads fail.

Changes:

  • Ensures call messages and error resources are released.
  • Adds a regression test checking descriptor counts after repeated failing calls.
  • Documents the fix in the changelog.
File Description
pystemd/​dbuslib.pyx Adds guaranteed cleanup using finally blocks.
e2e/​test_dbus.py Tests descriptor cleanup across 100 failed calls.
CHANGES.md Records the resource-leak fix.

Review effort: Balanced

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