Bug 497254 - Make sure we clean up properly on terminal disconnect. Both in cases where the disconnect action is invoked, or when the output stream terminates. Change-Id: Icc5e842dc8573a00ce57d726b2878a948e01251d