Thread close on right place
Fixed deadlock