Add task cleanup handlers. Ensure socket spawn flags are correctly set on task exit.