Close Rows object if the context is canceled before deferred function
First-time contribution here, I stumbled upon a bug while working at Rancher as part of my job at SUSE and figured to publish the fix. I hope this is useful, let me know if anything needs to be done to have it in.
This fixes a situation in which the driver will leak unclosed Rows, which result in read transactions which never finish, which will in turn block the checkpointer process and cause the WAL to grow unbounded, and other read queries to slow down unbounded (more background here under "Checkpoint Starvation").
Specifically this is useful in the following situation:
-
queryfunction gets called - SQL query is executed correctly, the
rresult (Rows) is set - after that, but before
queryreturns,ctxis canceled -
interruptOnDone’s goroutine catches the cancelation and signals the completion back toqueryby turning the atomic signaling variabledoneto 1 -
queryfinishes and returns - the anonymous deferred function patched here is popped off the deferred stack and executed
The current code, seeing that done is not zero, will overwrite r (Rows) with nil, and that implies the Rows can never be Closed.
I hope this helps!
Edited by Silvio Moioli