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

Fix context cancellation racy handling #744

Merged
merged 1 commit into from Nov 18, 2019
Merged

Conversation

azavorotnii
Copy link
Contributor

[why]
Context cancellation goroutine is not in sync with Next() method lifetime.
It leads to sql.ErrNoRows instead of context.Canceled often (easy to reproduce).
It leads to interruption of next query executed on same connection (harder to reproduce).

[how]
Do query in goroutine, wait when interruption done.

[testing]
Add unit test that reproduces error cases.

@coveralls
Copy link

coveralls commented Sep 6, 2019

Coverage Status

Coverage increased (+0.9%) to 51.293% when pulling 7e1a61d on azavorotnii:ctx_cancel into d3c6909 on mattn:master.

@azavorotnii
Copy link
Contributor Author

azavorotnii commented Sep 6, 2019

will need to check this:

=== RUN   TestQueryRowContextCancel
--- FAIL: TestQueryRowContextCancel (4.86s)
	sqlite3_go18_test.go:164: sql: Rows are closed 1

wondering if that something different in 1.9 and 1.10 that I didn't assume.

UPD: there was improvement in 1.11 in Rows.Scan method.
In 1.10.8:

func (rs *Rows) Scan(dest ...interface{}) error {
	rs.closemu.RLock()
	if rs.closed {
		rs.closemu.RUnlock()
		return errors.New("sql: Rows are closed")
	}
	rs.closemu.RUnlock()
...

In 1.11.x:

func (rs *Rows) Scan(dest ...interface{}) error {
	rs.closemu.RLock()

	if rs.lasterr != nil && rs.lasterr != io.EOF {
		rs.closemu.RUnlock()
		return rs.lasterr
	}
	if rs.closed {
		rs.closemu.RUnlock()
		return errors.New("sql: Rows are closed")
	}
	rs.closemu.RUnlock()
...

So before 1.11 if there was context cancellation, Rows.Scan don't return ctx.Err(), but "Rows are closed" instead.

Will update test to treat "Rows are closed" as "ok" error.

[why]
Context cancellation goroutine is not in sync with Next() method lifetime.
It leads to sql.ErrNoRows instead of context.Canceled often (easy to reproduce).
It leads to interruption of next query executed on same connection (harder to reproduce).

[how]
Do query in goroutine, wait when interruption done.

[testing]
Add unit test that reproduces error cases.
@mattn
Copy link
Owner

mattn commented Nov 18, 2019

Sorry about delay. I'll look into it in later.

@mattn mattn merged commit 590d44c into mattn:master Nov 18, 2019
@mattn
Copy link
Owner

mattn commented Nov 18, 2019

Thank you

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 this pull request may close these issues.

None yet

3 participants