Fix potential memory leak in Agent
Fix memory leak in IO when getting EPIPE