-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add Test Coverage for Multiplexed Session Goroutine Leak #13529
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
MASA-JAPAN
wants to merge
1
commit into
googleapis:main
Choose a base branch
from
MASA-JAPAN:test/spanner-multiplexed-session-leak
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.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2221,3 +2221,119 @@ func TestSessionRecycle_AlreadyInvalidSession(t *testing.T) { | |
| t.Errorf("Unexpected error \"Number of sessions in use is negative\" logged: %s", logOutput) | ||
| } | ||
| } | ||
|
|
||
| // TestSessionPool_CreateMultiplexedSession_NoGoroutineLeak tests that closing | ||
| // the session pool properly cleans up the createMultiplexedSession goroutine. | ||
| // This is a regression test for issue #13396. | ||
| // | ||
| // Before the fix, the multiplexedSessionReq channel was not closed when the | ||
| // pool was closed, causing the createMultiplexedSession goroutine to block | ||
| // forever waiting for requests. | ||
| // | ||
| // Note: This test validates the fix by checking that the multiplexedSessionReq | ||
| // channel is properly closed. We test channel closure directly rather than | ||
| // counting goroutines, as goroutine counting is unreliable in test environments. | ||
| func TestSessionPool_CreateMultiplexedSession_NoGoroutineLeak(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // Create a client with multiplexed sessions enabled | ||
| _, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{ | ||
| DisableNativeMetrics: true, | ||
| SessionPoolConfig: SessionPoolConfig{ | ||
| MinOpened: 0, | ||
| MaxOpened: 1, | ||
| enableMultiplexSession: true, | ||
| }, | ||
| }) | ||
| defer teardown() | ||
|
|
||
| pool := client.idleSessions | ||
|
|
||
| // Verify the channel exists and is open by sending a request | ||
| select { | ||
| case pool.multiplexedSessionReq <- muxSessionCreateRequest{force: false, ctx: ctx}: | ||
| // Successfully sent, channel is open | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("multiplexedSessionReq channel appears to be blocked or closed before pool close") | ||
| } | ||
|
|
||
| // Give the createMultiplexedSession goroutine time to process the request | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| // Close the pool | ||
| client.Close() | ||
|
|
||
| // Give some time for cleanup | ||
| time.Sleep(100 * time.Millisecond) | ||
|
|
||
| // Verify the channel is properly closed | ||
| // A receive from a closed channel returns immediately with zero value and ok=false | ||
| select { | ||
| case _, ok := <-pool.multiplexedSessionReq: | ||
| if ok { | ||
| t.Fatal("multiplexedSessionReq channel is still open after pool close; this would cause a goroutine leak") | ||
| } | ||
| // Channel is properly closed, test passes | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("multiplexedSessionReq channel is not closed after pool close; createMultiplexedSession goroutine is leaked") | ||
| } | ||
| } | ||
|
|
||
| // TestSessionPool_MultiplexedSessionReqChannelClosed tests that the | ||
| // multiplexedSessionReq channel is properly closed when the pool is closed. | ||
| func TestSessionPool_MultiplexedSessionReqChannelClosed(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := context.Background() | ||
|
|
||
| // Create a client with multiplexed sessions enabled | ||
| _, client, teardown := setupMockedTestServerWithConfig(t, ClientConfig{ | ||
| DisableNativeMetrics: true, | ||
| SessionPoolConfig: SessionPoolConfig{ | ||
| MinOpened: 0, | ||
| MaxOpened: 1, | ||
| enableMultiplexSession: true, | ||
| }, | ||
| }) | ||
| defer teardown() | ||
|
|
||
| pool := client.idleSessions | ||
|
|
||
| // Verify the channel exists and is open | ||
| select { | ||
| case pool.multiplexedSessionReq <- muxSessionCreateRequest{force: false, ctx: ctx}: | ||
| // Successfully sent, channel is open | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("multiplexedSessionReq channel appears to be blocked or closed before pool close") | ||
| } | ||
|
|
||
| // Close the pool | ||
| client.Close() | ||
|
|
||
| // Give some time for cleanup | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| // Verify the channel is closed by attempting to send | ||
| // A send on a closed channel will panic, so we recover from it | ||
| func() { | ||
| defer func() { | ||
| if r := recover(); r != nil { | ||
| t.Fatal("Sending to multiplexedSessionReq panicked, which means channel might not be closed properly or send was attempted incorrectly") | ||
| } | ||
| }() | ||
|
|
||
| // Try to receive from the closed channel | ||
| // A receive from a closed channel returns immediately with zero value | ||
| select { | ||
| case _, ok := <-pool.multiplexedSessionReq: | ||
| if ok { | ||
| t.Fatal("multiplexedSessionReq channel is still open after pool close") | ||
| } | ||
| // Channel is properly closed | ||
| case <-time.After(100 * time.Millisecond): | ||
| t.Fatal("multiplexedSessionReq channel is not closed after pool close") | ||
| } | ||
| }() | ||
|
Comment on lines
+2320
to
+2338
Contributor
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. This anonymous function with
I suggest replacing this entire block with just the core // Verify the channel is closed by attempting to receive.
// A receive from a closed channel returns immediately with a zero value and ok=false.
select {
case _, ok := <-pool.multiplexedSessionReq:
if ok {
t.Fatal("multiplexedSessionReq channel is still open after pool close")
}
// Channel is properly closed.
case <-time.After(100 * time.Millisecond):
t.Fatal("multiplexedSessionReq channel is not closed after pool close")
} |
||
| } | ||
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.
These
time.Sleepcalls can likely be removed to make the test faster and more robust.time.Sleep(50 * time.Millisecond)) seems unnecessary. The precedingselectblock already confirms that a message can be sent to the channel. The test doesn't depend on the request being fully processed, only that the channel was open, so waiting here isn't required.time.Sleep(100 * time.Millisecond)) is redundant. The finalselectblock (lines 2273-2281) already waits for up to 100ms for the channel to be closed. By having a sleep and a timeout, the test waits longer than necessary. You can rely solely on theselectwith its timeout to handle any small delay in closing the channel afterclient.Close()returns.Removing these sleeps would make the test more efficient without compromising its correctness.