aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

MemoryPoolHttpResponseStreamWriterFactory throws ArgumentNullException

Open bongia opened this issue 10 years ago • 3 comments

Hi, I have an exception which is thrown by AspNet code which I am not able to reproduce sistematically:

> System.ArgumentNullException: Value cannot be null.
Parameter name: stream
   at Microsoft.AspNetCore.Mvc.Internal.MemoryPoolHttpResponseStreamWriterFactory.CreateWriter(Stream stream, Encoding encoding)
   at Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultAsync>d__32.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResultFilterAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAllResultFiltersAsync>d__29.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeResourceFilterAsync>d__23.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

This seems to happen when the client is a browser (it happened using Angular2). With Postman or HttpClient I was not able to reproduce the issue. The same angular2 app, running with Charles (https://www.charlesproxy.com/) active, is not affected by this problem.

This is the request sent without Charles:

> POST@/api/token
    **Connection:keep-alive**
    Content-Length:58
    Content-Type:application/json
    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8,it;q=0.6
    Host:172.28.64.124:45678
    Referer:http://localhost:2020/login;next=true
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
    Origin:http://localhost:2020
    DNT:1

This is the request sent with Charles:

> POST@/api/token
        Content-Length:58
    Content-Type:application/json
    Accept:*/*
    Accept-Encoding:gzip, deflate
    Accept-Language:en-US,en;q=0.8,it;q=0.6
    Host:172.28.64.124:45678
    Referer:http://localhost:2020/login;next=null
    User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36
    Origin:http://localhost:2020
    DNT:1

As you can see the only significant difference is that when it throws exception, the client sends also the Connection:keep-alive header. It might be also a problem related to CORS since we are doing cross domain requests and we never reproduced the error with non-browser clients

I am using the following packages and the application is running self-hosted on windows 10 (64bit)

> {
    "dependencies": {
        "Microsoft.AspnetCore.Diagnostics": "1.0.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
        "Microsoft.AspNetCore.Mvc": "1.0.0",
        "Microsoft.AspnetCore.Server.Kestrel": "1.0.0",
        "Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
        "Microsoft.AspnetCore.StaticFiles": "1.0.0",
        "Microsoft.AspnetCore.Authentication.JwtBearer": "1.0.0",
        "Microsoft.Extensions.Configuration.Json": " 1.0.0",
        "Microsoft.Extensions.Logging": "1.0.0",
        "Microsoft.Extensions.Logging.Console": "1.0.0",
        "Microsoft.Extensions.Logging.Debug": "1.0.0",
        "Microsoft.Extensions.Caching.Abstractions": "1.0.0",
        "Microsoft.Extensions.Caching.Memory": "1.0.0",
        "System.IdentityModel.Tokens.Jwt": "5.0.0",
    },
    "frameworks": {
        "netcoreapp1.0": { }
    },
    "publishOptions": {
        "include": [
            "Properties"
        ]
    },
    "buildOptions": {
        "preserveCompilationContext": true
    }
}

Do you have ideas about the cause of the issue? Thanks in advance

bongia avatar Sep 28 '16 11:09 bongia

Thanks for the thorough report.

Do you have any code that messes with HttpContext.Response.Body? That stream that's null in this case is the Response.Body stream. https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNetCore.Mvc.Formatters.Json/JsonOutputFormatter.cs#L133

@cesarbs @halter73 @Tratcher seen anything as spooky as this before? The connection-keepalive part might be interesting.

rynowak avatar Sep 28 '16 15:09 rynowak

Could be the Kestrel response body reset bug fixed in 1.0.1. https://github.com/aspnet/KestrelHttpServer/issues/1028

Tratcher avatar Sep 28 '16 16:09 Tratcher

Thank you @Tratcher the issue seems to be solved. @rynowak no, the exception was thrown before hitting my code.

I will keep you updated if the problem occurs again. Thank you

bongia avatar Sep 29 '16 11:09 bongia