Ensure terminal gets released even in case of an exception