Stop threads before killing the worker

It is pretty good idea to kill the threads before killing the worker
because threads are still trying to read from pipe that could be prematurely
closed when workers get killed.
Merge pull request #2645 from bundler/fix-worker-cleanup-error

Stop threads before killing the worker