Correctly stop and close recovery threads and cleanups global context.

Also decreased total number of threads and processes created and used by pytests.
It is exactly the same code as several lines above in this pull request, why is this copy-paste?
Please refactor code to eliminate any code duplication