Improve SSL handling of thread shutdown