-
Notifications
You must be signed in to change notification settings - Fork 888
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
pgx.Conn: Fix memory leak: Delete items from preparedStatements #1614
Merged
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 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 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,55 @@ | ||
package pgx | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func mustParseConfig(t testing.TB, connString string) *ConnConfig { | ||
config, err := ParseConfig(connString) | ||
require.Nil(t, err) | ||
return config | ||
} | ||
|
||
func mustConnect(t testing.TB, config *ConnConfig) *Conn { | ||
conn, err := ConnectConfig(context.Background(), config) | ||
if err != nil { | ||
t.Fatalf("Unable to establish connection: %v", err) | ||
} | ||
return conn | ||
} | ||
|
||
// Ensures the connection limits the size of its cached objects. | ||
// This test examines the internals of *Conn so must be in the same package. | ||
func TestStmtCacheSizeLimit(t *testing.T) { | ||
const cacheLimit = 16 | ||
|
||
connConfig := mustParseConfig(t, os.Getenv("PGX_TEST_DATABASE")) | ||
connConfig.StatementCacheCapacity = cacheLimit | ||
conn := mustConnect(t, connConfig) | ||
defer func() { | ||
err := conn.Close(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
}() | ||
|
||
// run a set of unique queries that should overflow the cache | ||
ctx := context.Background() | ||
for i := 0; i < cacheLimit*2; i++ { | ||
uniqueString := fmt.Sprintf("unique %d", i) | ||
uniqueSQL := fmt.Sprintf("select '%s'", uniqueString) | ||
var output string | ||
err := conn.QueryRow(ctx, uniqueSQL).Scan(&output) | ||
require.NoError(t, err) | ||
require.Equal(t, uniqueString, output) | ||
} | ||
// preparedStatements contains cacheLimit+1 because deallocation happens before the query | ||
assert.Len(t, conn.preparedStatements, cacheLimit+1) | ||
assert.Equal(t, cacheLimit, conn.statementCache.Len()) | ||
} |
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.
this actually might not solve the "leak" issue, since Go won't deallocate memory after you delete a key from a map golang/go#20135
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 map itself won't shrink, but
delete()
does zero the keys and values in the map, so the*pgconn.StatementDescription
value will be garbage collected. Additionally,delete()
decrements the size of the map, which means its capacity will now be limited to the max number of prepared statements (with extra space for the map "fill factor"). This means this change should mean the amount of memory used by the c.preparedStatements is now fixed, rather than increasing without bound.Additionally, my reproduction program on the original issue can run forever without running out of memory: #1456 (comment)
So: I'm pretty confident that this fix will resolve this specific bug at least. There could be others though! I would be happy to try to figure them out if we have a sample program that reproduces the problem.
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.
yes, only if they poiners
I can be wrong, but regarding this example the allocated size of the map would be the same.
well I guess it's true if the pgx has limitations to prepared statement cache