The check for a closed session should never be needed as the core correctly orders the calls to the module functions. The version numbering was also not used. Fixed the incrementation of the query counter so that it uses relaxed atomic operations.