Ensure that streams drain the socket and cleanup when there is an exception in user code.