Should close $wpdb via $wpdb->close()

The close() function was added for completeness in the database abstraction. If the database is prematurely closed in a plugin or theme (as you are using it), the next call to wpdb::query() will re-open the connection.

Your Commands out of sync error is a MySQL error, not a WordPress one, and it might be coming from a plugin or theme that you’re using. Googling for wordpress database commands out of sync comes up with a lot of possible issues, including caching plugins. Try disabling all plugins and switching to a default theme (like Twenty Twenty); if the issue goes away, turn plugins back on till you find the culprit. Then contact that plugin’s support crew.