-
Notifications
You must be signed in to change notification settings - Fork 80
fix: command manager connect go routine leak #869
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
4cf7f0e
test: goroutine leak in cmdstate connect
hpidcock 3456d45
fix: goroutine leak in command manager connect
hpidcock ff0dc47
Merge remote-tracking branch 'upstream/master' into connect-leak
hpidcock 46dd176
refactor: use testutil leak detector
hpidcock 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // Copyright (c) 2026 Canonical Ltd | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License version 3 as | ||
| // published by the Free Software Foundation. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package cmdstate | ||
|
|
||
| // AddTestExecution inserts a fake execution into the manager's map and | ||
| // broadcasts the condition variable. | ||
| func (m *CommandManager) AddTestExecution(taskID string) { | ||
| m.executionsCond.L.Lock() | ||
| m.executions[taskID] = &execution{} | ||
| m.executionsCond.Broadcast() | ||
| m.executionsCond.L.Unlock() | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright (c) 2026 Canonical Ltd | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License version 3 as | ||
| // published by the Free Software Foundation. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package cmdstate_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
|
|
||
| . "gopkg.in/check.v1" | ||
|
|
||
| "github.com/canonical/pebble/internals/overlord/cmdstate" | ||
| "github.com/canonical/pebble/internals/overlord/state" | ||
| ) | ||
|
|
||
| type managerSuite struct{} | ||
|
|
||
| var _ = Suite(&managerSuite{}) | ||
|
|
||
| // TestConnectContextCancelledGoroutineLeak demonstrates the goroutine leak in | ||
| // Connect. executionCh is unbuffered: if Connect exits via r.Context().Done() | ||
| // after waitExecution has already found a non-nil execution, the goroutine | ||
| // blocks forever trying to send on executionCh with no receiver. | ||
| // | ||
| // The test is probabilistic. | ||
| func (s *managerSuite) TestConnectContextCancelledGoroutineLeak(c *C) { | ||
| st := state.New(nil) | ||
| runner := state.NewTaskRunner(st) | ||
| mgr := cmdstate.NewManager(runner) | ||
|
|
||
| st.Lock() | ||
| task := st.NewTask("exec", "test cmd") | ||
| chg := st.NewChange("exec", "test change") | ||
| chg.AddTask(task) | ||
| st.Unlock() | ||
|
|
||
| // Pre-register the execution so waitExecution returns immediately | ||
| // with a non-nil value, before stopWait can be closed. | ||
| mgr.AddTestExecution(task.ID()) | ||
|
|
||
| // Pre-cancel the context so Connect can exit via r.Context().Done() | ||
| // while the goroutine is still trying to send on executionCh. | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
|
|
||
| r := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx) | ||
| _ = mgr.Connect(r, httptest.NewRecorder(), task, "stdio") | ||
| } | ||
|
Comment on lines
+32
to
+60
Member
Author
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. Regression test for the go routine leak. |
||
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 |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| // Copyright (c) 2026 Canonical Ltd | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU General Public License version 3 as | ||
| // published by the Free Software Foundation. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU General Public License | ||
| // along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
|
||
| package cmdstate_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/canonical/pebble/internals/testutil" | ||
| . "gopkg.in/check.v1" | ||
| ) | ||
|
|
||
| func Test(t *testing.T) { | ||
| testutil.PrintGoroutineLeaks(t, TestingT) | ||
| } |
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.
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.
The fix for the go routine leak.