forked from denisenkom/go-mssqldb
-
Notifications
You must be signed in to change notification settings - Fork 91
fix: propagate context deadline to readPrelogin to prevent hangs #360
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
dlevy-msft-sql
wants to merge
22
commits into
microsoft:main
Choose a base branch
from
dlevy-msft-sql:fix/prelogin-context-deadline
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 5 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0d5021f
fix: propagate context deadline to readPrelogin to prevent hangs
dlevy-msft-sql 205da72
fix: align TestLoginTimeout error handling with TestQueryTimeout
dlevy-msft-sql d5a8863
fix: close connection on prelogin error, extract timeout helper, impr…
dlevy-msft-sql 1e6a016
fix: check ctx.Err() before deadline in preloginTimeout
dlevy-msft-sql 1f33bbf
fix: close connection on context cancel during prelogin read
dlevy-msft-sql 02fc140
fix: use deferred close to prevent socket leaks on all connect error …
dlevy-msft-sql 0caca81
fix: prevent data race on timeout restore and double close on reroute
dlevy-msft-sql 997d219
test: assert error types in prelogin integration tests
dlevy-msft-sql ef5cff6
fix: wait for cancel-watcher goroutine before restoring timeout
dlevy-msft-sql c2f757b
fix: clarify cancel watcher comment to match unconditional behavior
dlevy-msft-sql e128bf0
fix: defer cancel and stop timer in context cancel test
dlevy-msft-sql 0967bd8
fix: wrap db.Conn in goroutine with hard timeout in integration tests
dlevy-msft-sql f635122
fix: handle socket timeout racing with context deadline in prelogin
dlevy-msft-sql f82172f
fix: remove overly broad strings.Contains timeout fallback in test
dlevy-msft-sql dd27d3d
test: add coverage for socket timeout, expired context, and routing r…
dlevy-msft-sql 7b213d2
style: align whitespace in routing test
dlevy-msft-sql 9955d5a
fix: improve prelogin timeout error handling and test assertions
dlevy-msft-sql b8cf900
fix: use t.Errorf in goroutines and handle Accept errors in routing test
dlevy-msft-sql 3b19ff3
fix: add tokenLoginAck to routing mock and use PingContext with timeout
dlevy-msft-sql 5e5c647
test: improve patch coverage for prelogin context deadline
dlevy-msft-sql 5358a71
fix: avoid t.Fatal from goroutine in TestConnectSuccessfulPreloginAnd…
dlevy-msft-sql 4d3b4ac
fix: correct comment order in pastDeadlineContext documentation
dlevy-msft-sql 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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| package mssql | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "fmt" | ||
| "net" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| func TestPreloginTimeout(t *testing.T) { | ||
| t.Run("no deadline keeps connection timeout", func(t *testing.T) { | ||
| got, err := preloginTimeout(context.Background(), 30*time.Second) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != 30*time.Second { | ||
| t.Fatalf("timeout=%v, want %v", got, 30*time.Second) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("sooner deadline wins", func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| got, err := preloginTimeout(ctx, 30*time.Second) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got <= 0 || got > 250*time.Millisecond { | ||
| t.Fatalf("timeout=%v, want a positive value no greater than %v", got, 250*time.Millisecond) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("shorter connection timeout stays in effect", func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| got, err := preloginTimeout(ctx, 250*time.Millisecond) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got != 250*time.Millisecond { | ||
| t.Fatalf("timeout=%v, want %v", got, 250*time.Millisecond) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("zero connection timeout uses context deadline", func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| got, err := preloginTimeout(ctx, 0) | ||
| if err != nil { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| if got <= 0 || got > 250*time.Millisecond { | ||
| t.Fatalf("timeout=%v, want a positive value no greater than %v", got, 250*time.Millisecond) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("expired deadline returns context error", func(t *testing.T) { | ||
| ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) | ||
| defer cancel() | ||
|
|
||
| _, err := preloginTimeout(ctx, 30*time.Second) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if err != context.DeadlineExceeded { | ||
| t.Fatalf("error=%v, want %v", err, context.DeadlineExceeded) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("canceled context without deadline returns context error", func(t *testing.T) { | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
|
|
||
| _, err := preloginTimeout(ctx, 30*time.Second) | ||
| if err == nil { | ||
| t.Fatal("expected error, got nil") | ||
| } | ||
| if err != context.Canceled { | ||
| t.Fatalf("error=%v, want %v", err, context.Canceled) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // TestPreloginRespectsContextDeadline verifies that readPrelogin honors the | ||
| // context deadline rather than hanging for the full ConnTimeout when the | ||
| // server never responds. | ||
| func TestPreloginRespectsContextDeadline(t *testing.T) { | ||
| // Start a TCP listener that accepts connections but never sends data, | ||
| // simulating a server that hangs during prelogin. | ||
| addr := &net.TCPAddr{IP: net.IP{127, 0, 0, 1}} | ||
| listener, err := net.ListenTCP("tcp", addr) | ||
| if err != nil { | ||
| t.Fatal("Cannot start listener:", err) | ||
| } | ||
| defer listener.Close() | ||
| resolved := listener.Addr().(*net.TCPAddr) | ||
|
|
||
| done := make(chan struct{}) | ||
| defer close(done) | ||
|
|
||
| go func() { | ||
| for { | ||
| conn, err := listener.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| // Read the prelogin request but never respond. | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| // Hold connection open until the test finishes. | ||
| <-done | ||
| conn.Close() | ||
| } | ||
| }() | ||
|
|
||
| // Use a long ConnTimeout (30s) so if the context deadline is NOT | ||
| // respected, the test will hang noticeably. | ||
| dsn := fmt.Sprintf("sqlserver://sa:unused@%s:%d?connection+timeout=30&dial+timeout=2&protocol=tcp&encrypt=disable", | ||
| resolved.IP.String(), resolved.Port) | ||
|
|
||
| db, err := sql.Open("sqlserver", dsn) | ||
| if err != nil { | ||
| t.Fatal("sql.Open failed:", err) | ||
| } | ||
| defer db.Close() | ||
|
|
||
| // Context with a short deadline — this is the one that should win. | ||
| ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| start := time.Now() | ||
| conn, err := db.Conn(ctx) | ||
| elapsed := time.Since(start) | ||
|
|
||
| if err == nil { | ||
| conn.Close() | ||
| t.Fatal("Expected connection to fail, but it succeeded") | ||
| } | ||
|
|
||
| // The connection should fail well before the full ConnTimeout (30s). | ||
| // We use a generous 5s bound to avoid flakes on slow CI; the real | ||
| // expectation is ~500ms from the context deadline. | ||
| if elapsed > 5*time.Second { | ||
| t.Errorf("Connection took %v, expected it to respect the 500ms context deadline", elapsed) | ||
| } | ||
|
dlevy-msft-sql marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // TestPreloginRespectsContextCancel verifies that readPrelogin unblocks | ||
| // when the context is canceled even without a deadline set. | ||
| func TestPreloginRespectsContextCancel(t *testing.T) { | ||
| addr := &net.TCPAddr{IP: net.IP{127, 0, 0, 1}} | ||
| listener, err := net.ListenTCP("tcp", addr) | ||
| if err != nil { | ||
| t.Fatal("Cannot start listener:", err) | ||
| } | ||
| defer listener.Close() | ||
| resolved := listener.Addr().(*net.TCPAddr) | ||
|
|
||
| done := make(chan struct{}) | ||
| defer close(done) | ||
|
|
||
| go func() { | ||
| for { | ||
| conn, err := listener.Accept() | ||
| if err != nil { | ||
| return | ||
| } | ||
| buf := make([]byte, 4096) | ||
| _, _ = conn.Read(buf) | ||
| <-done | ||
| conn.Close() | ||
| } | ||
| }() | ||
|
|
||
| // connTimeout=30 and no context deadline: without the cancel watcher, | ||
| // this would block for the full 30s. | ||
| dsn := fmt.Sprintf("sqlserver://sa:unused@%s:%d?connection+timeout=30&dial+timeout=2&protocol=tcp&encrypt=disable", | ||
| resolved.IP.String(), resolved.Port) | ||
|
|
||
| db, err := sql.Open("sqlserver", dsn) | ||
| if err != nil { | ||
| t.Fatal("sql.Open failed:", err) | ||
| } | ||
| defer db.Close() | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
|
|
||
| // Cancel after 500ms to simulate a caller-driven cancellation. | ||
| time.AfterFunc(500*time.Millisecond, cancel) | ||
|
dlevy-msft-sql marked this conversation as resolved.
Outdated
|
||
|
|
||
| start := time.Now() | ||
| conn, err := db.Conn(ctx) | ||
| elapsed := time.Since(start) | ||
|
|
||
| if err == nil { | ||
| conn.Close() | ||
| t.Fatal("Expected connection to fail, but it succeeded") | ||
| } | ||
|
|
||
| if elapsed > 5*time.Second { | ||
| t.Errorf("Connection took %v, expected it to respect context cancellation within ~500ms", elapsed) | ||
| } | ||
|
dlevy-msft-sql marked this conversation as resolved.
|
||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.