Ref T13149. I ran into this while double checking query timeout behavior for T13149.
After a query times out, we close the connection. We may then try to close the connection again in the destructor. Modern mysqli, at least, doesn't like this very much.
Instead, keep track of whether the connection needs to be closed or not.