fix: close rows after query timeout to prevent hang#702
Open
dlevy-msft-sql wants to merge 1 commit intomainfrom
Open
fix: close rows after query timeout to prevent hang#702dlevy-msft-sql wants to merge 1 commit intomainfrom
dlevy-msft-sql wants to merge 1 commit intomainfrom
Conversation
shueybubbles
approved these changes
Feb 6, 2026
f345ddf to
2e67c32
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes sqlcmd hanging for ~10 minutes after query timeout on Linux.
Problem
When using the
-t(query timeout) flag, sqlcmd correctly prints "Timeout expired" but then hangs for approximately 10 minutes before exiting. This occurs because therowsresult set fromQueryContextis never closed when a timeout occurs.Reproduction
Root Cause
In
pkg/sqlcmd/sqlcmd.go, therunQueryfunction callsdb.QueryContext()but doesn't close the returnedrowsobject. When the context times out, the query is cancelled but therowshandle remains open, causing the underlying connection to wait for the driver's internal cleanup timeout.Fix
Add
defer rows.Close()immediately afterQueryContextreturns:This ensures the result set is always closed, even when the context is cancelled due to timeout.
Changes
pkg/sqlcmd/sqlcmd.godefer rows.Close()afterQueryContextcmd/modern/e2e_test.goTestE2E_QueryTimeout_NoHangregression testTesting
The new test verifies that a 1-second timeout query completes within 30 seconds (not 10 minutes):
Related
Replaces #682 (closed due to lint error and code quality issues).