Fix ssh thread hang caused by not closing streams.

Cleanup sudo threadpool after it is done.

Fix race condition when sudo input prompt timeout happens before
the SSH connection begins.

for #311