HttpException: Server cannot set status after HTTP headers have been sent

I keep on getting this error thrown in the global UncaughtExceptionHandlers.

I am assuming it’s not as fatal as it appears. Is there any way to handle this so that it is not uncaught in ServiceStack itself?

System.Web.HttpException (0x80004005): Server cannot set status after HTTP headers have been sent.
   at System.Web.HttpResponse.set_StatusCode(Int32 value)
   at ServiceStack.HttpResponseExtensionsInternal.<WriteErrorToResponse>d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.HostContext.<RaiseAndHandleUncaughtException>d__67.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.HttpResponseExtensionsInternal.<HandleResponseWriteException>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.HttpResponseExtensionsInternal.<WriteToResponse>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at ServiceStack.HttpResponseExtensionsInternal.<WriteToResponse>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.Handlers.ServiceStackHandlerBase.<HandleResponseNext>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.Handlers.ServiceStackHandlerBase.<HandleResponse>d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at ServiceStack.Host.RestHandler.<ProcessRequestAsync>d__14.MoveNext()

Unfortunately the StackTrace isn’t clear one what’s causing it, only that the issue is that the StatusCode can’t be set after the HTTP Headers have been sent. Do you know how to rerpo the issue?

I’m not 100% sure what causes it. I have code like this to stream a video response to a client:

var fs = new FileStream(video.FileSystemAddress, FileMode.Open, FileAccess.Read);
return new HttpResult(fs, video.MimeType);

It could be that the client has cancelled/stopped the stream?

Yeah it’s something like that, trying to set the StatusCode of an ErrorResponse where the Headers have already been flushed to the client. Anyway I’ve added a fix that wont add headers when the response has already started which should prevent this error at least.

This change is available from v5.0.3 that’s now available on MyGet.