Skip to content

fix unsafe rows.Close() race#726

Closed
erykwalder wants to merge 1 commit intolib:masterfrom
erykwalder:scratch-race
Closed

fix unsafe rows.Close() race#726
erykwalder wants to merge 1 commit intolib:masterfrom
erykwalder:scratch-race

Conversation

@erykwalder
Copy link
Contributor

@erykwalder erykwalder commented Mar 15, 2018

According to https://godoc.org/database/sql/driver#Rows, the Close
operation should not affect the values returned by Next. In normal flow,
this is not an issue, but if Rows is closed by a cancelled context or
another routine, it can be.

This changes the scratch to be a slice, and changes its memory location
when rows are closed before they are done, so that any scanning will not
be affected.

In an attempt to prevent allocations every time the rows are closed,
this only creates a new scratch when rows are closed before they
are done.

According to https://godoc.org/database/sql/driver#Rows, the Close
operation should not affect the values returned by Scan. In normal flow,
this is not an issue, but if Rows is closed by a cancelled context or
another routine, it can be.

This changes the scratch to be a slice, and changes its memory location
when rows are closed before they are done, so that any scanning will not
be affected.
@arp242 arp242 added bug needs-test Needs a test before it can be merged labels Jan 1, 2026
@arp242
Copy link
Collaborator

arp242 commented Jan 24, 2026

I can't really simulate a case where this causes problems for us. I think the current logic should be fine? I see a number of changes/fixes in database/sql related to this as well over the last few years so it's possible this used to be a problem but is no longer.

Since this is a few years old I'll just go ahead and close it, but feel free to come back with more details if I'm mistaken we can re-open.

@arp242 arp242 closed this Jan 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug needs-test Needs a test before it can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments