-
Notifications
You must be signed in to change notification settings - Fork 364
fix: don't close shared session store in runtime.Close #2879
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
Open
dgageot
wants to merge
1
commit into
docker:main
Choose a base branch
from
dgageot:board/2c638431e2df4d72
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−18
Open
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 |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package runtime | ||
|
|
||
| import ( | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/docker/docker-agent/pkg/agent" | ||
| "github.com/docker/docker-agent/pkg/session" | ||
| "github.com/docker/docker-agent/pkg/team" | ||
| ) | ||
|
|
||
| // trackingStore wraps an in-memory store so a test can detect Close() calls. | ||
| type trackingStore struct { | ||
| session.Store | ||
|
|
||
| closes atomic.Int32 | ||
| } | ||
|
|
||
| func (s *trackingStore) Close() error { | ||
| s.closes.Add(1) | ||
| return s.Store.Close() | ||
| } | ||
|
|
||
| // TestRuntimeClose_DoesNotCloseExternalSessionStore is a regression test for | ||
| // the bug fixed in #2872: closing one runtime would close the SQLite session | ||
| // store shared with other runtimes (e.g. spawned by the TUI's session | ||
| // browser), making subsequent reads fail with "sql: database is closed". | ||
| func TestRuntimeClose_DoesNotCloseExternalSessionStore(t *testing.T) { | ||
| store := &trackingStore{Store: session.NewInMemorySessionStore()} | ||
|
|
||
| prov := &mockProvider{id: "test/mock-model", stream: &mockStream{}} | ||
| root := agent.New("root", "You are a test agent", agent.WithModel(prov)) | ||
| tm := team.New(team.WithAgents(root)) | ||
|
|
||
| rt, err := New(tm, | ||
| WithSessionCompaction(false), | ||
| WithModelStore(mockModelStore{}), | ||
| WithSessionStore(store), | ||
| ) | ||
| require.NoError(t, err) | ||
|
|
||
| require.NoError(t, rt.Close()) | ||
| assert.Zero(t, store.closes.Load(), | ||
| "runtime.Close() must not close an externally supplied session store; "+ | ||
| "the store may be shared with other runtimes") | ||
|
|
||
| // The store is still usable after the runtime is closed. | ||
| _, err = store.GetSessionSummaries(t.Context()) | ||
| require.NoError(t, err) | ||
| } |
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
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.
[MEDIUM] Potential data race:
Close()readsb.storeoutsidesync.OncelocalBackend.Close()readsb.storedirectly (line 130) without any synchronization, whilesessionStore()writesb.storeinsidestoreOnce.Do(...). The Go memory model only guarantees that the write tob.storeinside theDoclosure is visible afterDoreturns — it does not provide a happens-before guarantee for concurrent reads ofb.storethat happen outside ofDo.If
Close()races with an in-progressstoreOnce.Do()call (e.g., a session is still being initialized when shutdown begins), the Go race detector would flag this.Suggested fix — either use
storeOnce.Doto query the store inClose()as well, or protectb.storewith a mutex:Or, more robustly, add a
sync.Mutexto guard access tob.storein bothsessionStoreandClose.