Wait for the finalizer thread to actually exit before shutting down. Fixes #4389.