-
Notifications
You must be signed in to change notification settings - Fork 10
fix(keychain): close D-Bus connection after each Linux operation #542
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
Merged
+95
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,74 @@ | |
| package keychain | ||
|
|
||
| import ( | ||
| "os" | ||
| "testing" | ||
|
|
||
| dbus "github.com/godbus/dbus/v5" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/secrets-engine/store" | ||
| ) | ||
|
|
||
| // openFDCount returns the number of open file descriptors held by the current | ||
| // process by counting entries in /proc/self/fd (Linux only). | ||
| func openFDCount(t *testing.T) int { | ||
| t.Helper() | ||
| entries, err := os.ReadDir("/proc/self/fd") | ||
| require.NoError(t, err) | ||
| return len(entries) | ||
| } | ||
|
|
||
| // TestKeychainDoesNotLeakConnections is a regression test for a D-Bus | ||
| // connection leak: each keychain operation dialed a fresh session-bus | ||
| // connection (kc.NewService -> dbus.ConnectSessionBus) but only closed the | ||
| // secret-service session, never the connection itself. Every operation | ||
| // therefore leaked one socket file descriptor, eventually exhausting the | ||
| // session bus's max_connections_per_user limit on long-lived processes. | ||
| // | ||
| // It reproduces the failure shape directly: perform many lookups and assert | ||
| // the process's open-fd count stays bounded instead of growing | ||
| // one-per-operation. | ||
| // | ||
| // The lookups target a non-existent id so the test exercises the full | ||
| // connection setup (NewService -> OpenSession -> resolve collection -> | ||
| // SearchCollection) and then returns ErrCredentialNotFound *before* fetching a | ||
| // secret. That keeps the test focused on the connection lifecycle — the thing | ||
| // the fix changes — without creating, reading, or deleting shared keyring | ||
| // items (which is both stateful and prone to gnome-keyring item-lock quirks). | ||
| func TestKeychainDoesNotLeakConnections(t *testing.T) { | ||
|
Member
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. Drop these tests - I have a follow-up PR which will introduce mock service which at least helps against regressions #543 |
||
| ks := setupKeychain(t, nil) | ||
| missing := store.MustParseID("com.test.test/test/does-not-exist") | ||
|
|
||
| const iterations = 30 | ||
|
|
||
| get := func() { | ||
| _, err := ks.Get(t.Context(), missing) | ||
| require.ErrorIs(t, err, store.ErrCredentialNotFound) | ||
| } | ||
|
|
||
| // Warm up so any one-time, non-leaking fds (lazy runtime/dbus init) are | ||
| // already open before we take the baseline. | ||
| for range 3 { | ||
| get() | ||
| } | ||
|
|
||
| before := openFDCount(t) | ||
| for range iterations { | ||
| get() | ||
| } | ||
| after := openFDCount(t) | ||
|
|
||
| // A correct implementation closes every connection it opens, so the fd | ||
| // count should be flat. Allow a small slack for unrelated runtime churn; | ||
| // the leak grows the count by ~iterations, far above the threshold. | ||
| const slack = 5 | ||
| assert.LessOrEqualf(t, after-before, slack, | ||
| "open fd count grew by %d over %d lookups (before=%d after=%d): D-Bus connections are leaking", | ||
| after-before, iterations, before, after) | ||
| } | ||
|
|
||
| func TestResolveDefaultCollection(t *testing.T) { | ||
| const customCollection = dbus.ObjectPath("/org/freedesktop/secrets/collection/custom") | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Let's drop this test. I don't like depending on assertions that depend on the OS environment staying a certain way. We most definitely will see flakes on this.