Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fix: Exception during content rendering kills the self host process #563

Closed
wants to merge 2 commits into from

2 participants

@thinkbeforecoding

Especially when a user refreshes the browser during a long request, the rendering raises a Disposed Stream exception that reach the top of the IO completion thread stack and kill the host process.

@thecodejunkie

Not sure what you have done here, but this pull request will not merge without conflicts. I think you might have based it of an out of date version of upstream/master?

I would highly recommend that you read out https://github.com/NancyFx/Nancy/wiki/Git-Workflow article and get into the habit of using feature branches and to be able to handle changes in upstream/master

There are also a couple of TODO comments that I think needs to be resolved before we can consider this to be in a state where it can be pulled into master

Thank you!

@thinkbeforecoding

oh.. yes, I have the same problem on my personal dev laptop.. not sure what happened on the other one.
I'll clean this, and update the request.

@thinkbeforecoding

actually, the last pull request you merged had several changes in the same files... this is why a merge is necessary
I'll rebase my changes

@thecodejunkie

Sweet! Thanks Jérémie!

@thinkbeforecoding

OK I do a new one, sorry I'm fluent in mercurial but still a newbie in git :-S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 19, 2012
  1. @thinkbeforecoding

    Fix: Exception during rendering kills the self host process

    thinkbeforecoding authored unknown committed
  2. @thinkbeforecoding

    Add TODO comments about the limitation of current SelfHost fix

    thinkbeforecoding authored unknown committed
This page is out of date. Refresh to see the latest.
View
18 src/Nancy.Hosting.Self.Tests/NancySelfHostFixture.cs
@@ -131,6 +131,7 @@ private static NancyHostWrapper CreateAndOpenSelfHost(INancyBootstrapper nancyBo
nancyBootstrapper,
BaseUri);
+
try
{
host.Start();
@@ -143,6 +144,23 @@ private static NancyHostWrapper CreateAndOpenSelfHost(INancyBootstrapper nancyBo
return new NancyHostWrapper(host);
}
+
+ [SkippableFact]
+ public void Should_be_able_to_recover_from_rendering_exception()
+ {
+ using (CreateAndOpenSelfHost())
+ {
+
+ var reader =
+ new StreamReader(WebRequest.Create(new Uri(BaseUri,"exception")).GetResponse().GetResponseStream());
+
+ var response = reader.ReadToEnd();
+
+ response.ShouldEqual("Content");
+ }
+ }
+
+
private class NancyHostWrapper : IDisposable
{
private readonly NancyHost host;
View
12 src/Nancy.Hosting.Self.Tests/TestModule.cs
@@ -1,4 +1,6 @@
-namespace Nancy.Hosting.Self.Tests
+using System;
+
+namespace Nancy.Hosting.Self.Tests
{
using System.IO;
@@ -19,6 +21,14 @@ public TestModule()
};
Post["/rel"] = parameters => new StreamReader(Request.Body).ReadToEnd();
+
+ Get["/exception"] = parameters => new Response() {Contents = s =>
+ {
+ var writer = new StreamWriter(s);
+ writer.Write("Content");
+ writer.Flush();
+ throw new Exception("An error occured during content rendering");
+ }};
}
}
}
View
24 src/Nancy.Hosting.Self/NancyHost.cs
@@ -184,12 +184,28 @@ private void GotCallback(IAsyncResult ar)
private void Process(HttpListenerContext ctx)
{
- var nancyRequest =
- ConvertRequestToNancyRequest(ctx.Request);
+ try
+ {
- using (var nancyContext = engine.HandleRequest(nancyRequest))
+ var nancyRequest = ConvertRequestToNancyRequest(ctx.Request);
+ using (var nancyContext = engine.HandleRequest(nancyRequest))
+ {
+
+ try
+ {
+ ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response);
+ }
+ catch (Exception ex)
+ {
+ nancyContext.Trace.TraceLog.WriteLog(s => s.AppendLine(string.Concat("[SelfHost] Exception while rendering response: ", ex)));
+ //TODO - the content of the tracelog is not used in this case
+ }
+ }
+ }
+ catch (Exception)
{
- ConvertNancyResponseToResponse(nancyContext.Response, ctx.Response);
+ //TODO - this swallows the exception so that it doesn't kill the host
+ // pass it to the host process for handling by the caller ?
}
}
}
Something went wrong with that request. Please try again.