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

stmt.QueryRowContext doesn't respect canceled context #1046

Closed
michaelshobbs opened this issue Jun 11, 2021 · 1 comment · Fixed by #1047
Closed

stmt.QueryRowContext doesn't respect canceled context #1046

michaelshobbs opened this issue Jun 11, 2021 · 1 comment · Fixed by #1047

Comments

@michaelshobbs
Copy link
Contributor

michaelshobbs commented Jun 11, 2021

Working on a project using https://github.com/jmoiron/sqlx, I ran into this issue when using PrepareContext()/QueryRowContext with a context with timeout.

The following test case exercises the problem

func TestIssue1046(t *testing.T) {
	ctxTimeout := time.Second * 2
	sleepSQL := `SELECT pg_sleep(10) AS id`

	db := openTestConn(t)
	defer db.Close()

	ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
	defer cancel()

	stmt, err := db.PrepareContext(ctx, sleepSQL)
	if err != nil {
		t.Fatal(err)
	}

	var d []uint8
	err = stmt.QueryRowContext(ctx).Scan(&d)
	dl, _ := ctx.Deadline()
	since := time.Since(dl)
	if since > ctxTimeout {
		t.Logf("FAIL %s: query returned after context deadline: %v\n", t.Name(), since)
		t.Fail()
	}
	expectedErr := &Error{Message: "canceling statement due to user request"}
	if err == nil || err.Error() != expectedErr.Error() {
		t.Logf("ctx.Err(): [%T]%+v\n", ctx.Err(), ctx.Err())
		t.Logf("got err: [%T] %+v expected err: [%T] %+v", err, err, expectedErr, expectedErr)
		t.Fail()
	}
}
@michaelshobbs
Copy link
Contributor Author

I found #921 and pulling that into master locally seems to resolve the issue. I'll open up a new PR and add test cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant