fix memory leak (caused by thread never terminating)