Skip to content
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

sql/parser: fix memory leak of scanned comments #127711

Merged
merged 1 commit into from
Jul 26, 2024

Conversation

mgartner
Copy link
Collaborator

@mgartner mgartner commented Jul 25, 2024

This commit fixes a memory leak that cause a session's
scanner.Scanner, which is embedded in a pgwire.conn via a
parser.Parser, to retain previously scanned SQL comments in all parsed
SQL statements. The scanner's slice of comments would continue to grow
as it parsed new SQL strings with comments, as long as the session
remained open. Also, the slice of comments holds references to the bytes
in pgwire read buffers, preventing those buffers from being GC'd.

Fixes #127710

Release note (bug fix): A bug has been fixed that caused a memory leak
when executing SQL statements with comments, for example,
SELECT /* comment */ 1;. Memory owned by a SQL session would continue
to grow as these types of statements were executed. The memory would
only be released when closing the SQL session. This bug has been present
since version 23.1.

@mgartner mgartner added backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.2.x Flags PRs that need to be backported to 24.2 labels Jul 25, 2024
@mgartner mgartner requested review from a team and michae2 and removed request for a team July 25, 2024 21:03
Copy link

blathers-crl bot commented Jul 25, 2024

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Collaborator

@fqazi fqazi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, find!

:lgtm_strong:

Reviewed all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice find!

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (waiting on @mgartner and @michae2)


-- commits line 14 at r1:
nit: should we add a release note for it? It looks like this behavior has been present since forever22.2.

@yuzefovich yuzefovich added the backport-23.1.x Flags PRs that need to be backported to 23.1 label Jul 25, 2024
@mgartner
Copy link
Collaborator Author

TFTRs!

bors r+

@mgartner
Copy link
Collaborator Author

bors r-

@craig
Copy link
Contributor

craig bot commented Jul 26, 2024

Canceled.

This commit fixes a memory leak that cause a session's
`scanner.Scanner`, which is embedded in a `pgwire.conn` via a
`parser.Parser`, to retain previously scanned SQL comments in all parsed
SQL statements. The scanner's slice of comments would continue to grow
as it parsed new SQL strings with comments, as long as the session
remained open. Also, the slice of comments holds references to the bytes
in `pgwire` read buffers, preventing those buffers from being GC'd.

Fixes cockroachdb#127710

Release note (bug fix): A bug has been fixed that caused a memory leak
when executing SQL statements with comments, for example,
`SELECT /* comment */ 1;`. Memory owned by a SQL session would continue
to grow as these types of statements were executed. The memory would
only be released when closing the SQL session. This bug has been present
since version 23.1.
@mgartner
Copy link
Collaborator Author

nit: should we add a release note for it? It looks like this behavior has been present since forever22.2.

Done. Looks like it made it into 23.1, but the backport to 22.2 was not merged: #92844.

@mgartner
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jul 26, 2024

Build failed (retrying...):

Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: Nice find!

Reviewed 1 of 1 files at r1, all commit messages.
Reviewable status: :shipit: complete! 2 of 0 LGTMs obtained (and 1 stale) (waiting on @mgartner)

@craig craig bot merged commit 066169c into cockroachdb:master Jul 26, 2024
22 checks passed
@mgartner mgartner deleted the fix-comment-leak branch July 26, 2024 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.1.x Flags PRs that need to be backported to 23.1 backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.2.x Flags PRs that need to be backported to 24.2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql/parser: memory leak of parsed SQL comments
5 participants