fix duplicate headers when calling WriteError in some cases.