In case of something goes wrong during a database action and DB_driver's display_error() gets called, the query_builder doesn't get cleared causing a conflict with the subsequent update of the register_shutdown_function _update_db(). For example, with some data in a order_by clause.
Shouldn't we reset the $db before display_error()?
Sounds good, care to make a pull request?
Issue #2086 Session_cookie's _update_db not guaranteed to update
Take a look on pull #2267.