Assuming I have the following class:
public class Foo
{
public string Bar { get; set; }
}
And a controller with this code:
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult(new Foo
{
Bar = "<!DOCTYPE html><html><body><p>This!</p><p visible-if=\"correct\">Not this!</p><code>var x = <br></code><p>Cool!</p></body></html>"
});
}
For some reason, this is throwing an ArgumentException with the message "The buffer is not associated with this pool and may not be returned to it."
What's interesting is, that this exception occurs only for very specific mutations of that test string. If I remove some parts of it, the JSON ist returned just fine. e.g try removing "Not this!" from the string
I've created a very simple project to reproduce this issue: https://github.com/flagbug/JsonTest
just start the server and call http://localhost:5000/api/values
Also note that this worked just fine in RC1-final!
Here is also the full stacktrace:
> System.Buffers.dll!System.Buffers.DefaultArrayPoolBucket<char>.Return(char[] buffer) Unknown
System.Buffers.dll!System.Buffers.DefaultArrayPool<char>.Return(char[] buffer, bool clearArray) Unknown
Microsoft.AspNetCore.Mvc.Formatters.Json.dll!Microsoft.AspNetCore.Mvc.Formatters.Json.Internal.JsonArrayPool<char>.Return(char[] array) Unknown
Newtonsoft.Json.dll!Newtonsoft.Json.JsonTextWriter.Close() Unknown
Newtonsoft.Json.dll!Newtonsoft.Json.JsonWriter.Dispose(bool disposing) Unknown
Newtonsoft.Json.dll!Newtonsoft.Json.JsonWriter.System.IDisposable.Dispose() Unknown
Microsoft.AspNetCore.Mvc.Formatters.Json.dll!Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(System.IO.TextWriter writer, object value) Unknown
Microsoft.AspNetCore.Mvc.Formatters.Json.dll!Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteResponseBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context, System.Text.Encoding selectedEncoding) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__12>(ref Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__12 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Formatters.Json.dll!Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteResponseBodyAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context, System.Text.Encoding selectedEncoding) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Formatters.TextOutputFormatter.WriteAsync(Microsoft.AspNetCore.Mvc.Formatters.OutputFormatterWriteContext context) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.ObjectResultExecutor.ExecuteAsync(Microsoft.AspNetCore.Mvc.ActionContext context, Microsoft.AspNetCore.Mvc.ObjectResult result) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.ObjectResult.ExecuteResultAsync(Microsoft.AspNetCore.Mvc.ActionContext context) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultAsync(Microsoft.AspNetCore.Mvc.IActionResult result) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultAsync>d__44>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultAsync>d__44 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultAsync(Microsoft.AspNetCore.Mvc.IActionResult result) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultFilterAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultFilterAsync>d__43>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultFilterAsync>d__43 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultFilterAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultFilterAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultFilterAsync>d__43>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResultFilterAsync>d__43 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResultFilterAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeAllResultFiltersAsync(Microsoft.AspNetCore.Mvc.IActionResult result) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAllResultFiltersAsync>d__42>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAllResultFiltersAsync>d__42 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeAllResultFiltersAsync(Microsoft.AspNetCore.Mvc.IActionResult result) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResourceFilterAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResourceFilterAsync>d__37>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResourceFilterAsync>d__37 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResourceFilterAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResourceFilterAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResourceFilterAsync>d__37>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeResourceFilterAsync>d__37 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeResourceFilterAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeAllResourceFiltersAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAsync>d__32>(ref Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.<InvokeAsync>d__32 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.FilterActionInvoker.InvokeAsync() Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.InvokeActionAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor actionDescriptor) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.<InvokeActionAsync>d__8>(ref Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.<InvokeActionAsync>d__8 stateMachine) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.InvokeActionAsync(Microsoft.AspNetCore.Http.HttpContext httpContext, Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor actionDescriptor) Unknown
Microsoft.AspNetCore.Mvc.Core.dll!Microsoft.AspNetCore.Mvc.Internal.MvcRouteHandler.RouteAsync.AnonymousMethod__0(Microsoft.AspNetCore.Http.HttpContext c) Unknown
Microsoft.AspNetCore.Routing.dll!Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4>(ref Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4 stateMachine) Unknown
Microsoft.AspNetCore.Routing.dll!Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
Microsoft.AspNetCore.IISPlatformHandler.dll!Microsoft.AspNetCore.IISPlatformHandler.IISPlatformHandlerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.IISPlatformHandler.IISPlatformHandlerMiddleware.<Invoke>d__7>(ref Microsoft.AspNetCore.IISPlatformHandler.IISPlatformHandlerMiddleware.<Invoke>d__7 stateMachine) Unknown
Microsoft.AspNetCore.IISPlatformHandler.dll!Microsoft.AspNetCore.IISPlatformHandler.IISPlatformHandlerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3>(ref Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.<Invoke>d__3 stateMachine) Unknown
Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.Internal.RequestServicesContainerMiddleware.Invoke(Microsoft.AspNetCore.Http.HttpContext httpContext) Unknown
Microsoft.AspNetCore.Hosting.dll!Microsoft.AspNetCore.Hosting.Internal.HostingApplication.ProcessRequestAsync(Microsoft.AspNetCore.Hosting.Internal.HostingApplication.Context context) Unknown
Microsoft.AspNetCore.Server.Kestrel.dll!Microsoft.AspNetCore.Server.Kestrel.Http.Frame<Microsoft.AspNetCore.Hosting.Internal.HostingApplication.Context>.RequestProcessingAsync() Unknown
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<Microsoft.AspNetCore.Server.Kestrel.Http.Frame<Microsoft.AspNetCore.Hosting.Internal.HostingApplication.Context>.<RequestProcessingAsync>d__2>(ref Microsoft.AspNetCore.Server.Kestrel.Http.Frame<Microsoft.AspNetCore.Hosting.Internal.HostingApplication.Context>.<RequestProcessingAsync>d__2 stateMachine) Unknown
Microsoft.AspNetCore.Server.Kestrel.dll!Microsoft.AspNetCore.Server.Kestrel.Http.Frame<Microsoft.AspNetCore.Hosting.Internal.HostingApplication.Context>.RequestProcessingAsync() Unknown
Microsoft.AspNetCore.Server.Kestrel.dll!Microsoft.AspNetCore.Server.Kestrel.Http.Frame.Start.AnonymousMethod__108_0(object o) Unknown
mscorlib.dll!System.Threading.Tasks.Task<System.Threading.Tasks.Task>.InnerInvoke() Unknown
mscorlib.dll!System.Threading.Tasks.Task.Execute() Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Unknown
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Unknown
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Unknown
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Unknown
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Unknown
/cc @JamesNK @sokket
@JamesNK how is JSON.NET using ArrayPool; is it using the Shared pool or creating its own instance?
It is up to the framework/user. For MVC it is the shared pool.
@ajaybhargavb can you take a look?
Same issue using DNX 1.0.0-rc2-16595 on Windows.
Same problem using DNX 1.0.0-rc2-16551 and 1.0.0-rc2-16595 running on mono 4.3.3.97 in docker (Kestrel, Mvc and Mvc.Dnx package 1.0.0-rc2-20096). I'm returning JSON objects as well.
For me, it works on Windows though, using the default CLR (i.e. not core).
So, I tried different things:
52000)443 -> 52000)4430 -> 52000)The first two options would result in the exception with no further output. However connecting through nginx, while still resulting in the exceptions, would return a perfectly valid result.
I can also confirm the behavior that for some responses the exception would occur, while it would _not_ occur for others. I thought it might be size related, but that doesn't seem to be the case.
@sokket @stephentoub - https://github.com/dotnet/corefx/issues/5949
This change breaks some fairly innocent looking code. Look at this unit test: https://github.com/dotnet/corefx/commit/a0d122b80dfb6975c2f0dc4325460d8890562a3b#diff-c4e5a90442584ce8373354cb1fde4e68R382 It doesn't pass the smell test once the 15 is a computed value.
You need to know now whether or not your buffer returned from Rent was really rented, which breaks the usability of this API.
This is not an MVC issue. Some changes to System.Buffer broke JSON.Net. Sit tight, and we'll figure out the right fix for this. https://github.com/dotnet/corefx/issues/5949
@JamesNK @sokket - it looks like there's a possibility for JSON.Net to allocate the write buffer using new here: https://github.com/JamesNK/Newtonsoft.Json/blob/ce2a08dba228341742d96e8c8edfede6748d5bdc/Src/Newtonsoft.Json/Utilities/JavaScriptUtils.cs#L289
Fixed - https://github.com/JamesNK/Newtonsoft.Json/commit/5ddbc508f0b9f19243076821c825a7f5056de4b1
FYI next planned release will be when RC2 and netstandard is available.
Nice, we can use it in Half-Life 3 then.
@JamesNK, @rynowak may be you can post a fix before final RC2?
+1 for a fix before final RC2.
If that's not possible, what would be the recommended workaround ?
Downgrade to an older version of System.Buffers in application code before the exception was introduced. That would work around the issue until there was a new Json.NET available.
@davidfowl That would be 4.0.0-rc2-23728?
Thanks @davidfowl. We tried to include the System.Buffers dependency "System.Buffers": "4.0.0-rc2-23623" in our application projects (we tried multiple olders versions). But during startup it results in the following exception : System.IO.FileLoadException: The located assembly's manifest definition does not match the assembly reference in Microsoft.Extensions.DependencyInjection.MvcCoreServiceCollectionExtensions.AddMvcCoreServices(IServiceCollection services)
Is this the proper method to "downgrade" the version of System.Buffers ?
@IsaacSee / @davidfowl So, I tried with System.Buffers fixed to 4.0.0-rc2-23728 in the main server project and that seems to work.
I'm trying to use System.Buffers 4.0.0-rc2-23728 but dependency resolves to 4.0.0-rc2-23819...
project.json:
{
...
"dependencies": {
...
"System.Buffers": "4.0.0-rc2-23728",
...
},
...
}
Result:

@anfomin Just had a look, 4.0.0-rc2-23728 seems to be available on the aspnetcidev, but not on aspnetvnext feed.
@davidfowl - would it break things if we do the downgrade in MVC?
After downgrade to System.Buffers 4.0.0-rc2-23728 (DNX 1.0.0-rc2-16595) the "buffer x pool" exception is not thrown anymore but I have this new one throwing for some json responses :
fail: Microsoft.AspNetCore.Server.Kestrel[13]
An unhandled exception was thrown by the application.
System.ArgumentException: Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
脿 System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)
脿 Microsoft.AspNetCore.WebUtilities.HttpResponseStreamWriter.CopyToCharBuffer(Char[] values, Int32& index, Int32& count)
脿 Microsoft.AspNetCore.WebUtilities.HttpResponseStreamWriter.Write(Char[] values, Int32 index, Int32 count)
脿 Newtonsoft.Json.Utilities.JavaScriptUtils.WriteEscapedJavaScriptString(TextWriter writer, String s, Char delimiter, Boolean appendDelimiters, Boolean[] charEscapeFlags, StringEscapeHandling stringEscapeHandling, IArrayPool`1 bufferPool, Char[]& writeBuffer)
脿 Newtonsoft.Json.JsonTextWriter.WriteEscapedString(String value, Boolean quote)
脿 Newtonsoft.Json.JsonTextWriter.WriteValue(String value)
脿 Newtonsoft.Json.JsonWriter.WriteValue(JsonWriter writer, PrimitiveTypeCode typeCode, Object value)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializePrimitive(JsonWriter writer, Object value, JsonPrimitiveContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
脿 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
脿 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
脿 Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.WriteObject(TextWriter writer, Object value)
脿 Microsoft.AspNetCore.Mvc.Formatters.JsonOutputFormatter.<WriteResponseBodyAsync>d__12.MoveNext()
@IsaacSee - do you have a sample JSON output I can look at to investigate the issue you're seeing?
@rynowak - lasts tests were on dynamic data but I'll try to export a static sample output generating this exception
@rynowak - it may take more time than i thought since the exception seems to only trigger after something breaks a state (ie. I need to make few requests before reproducing the issue, even for static responses). Once this state is broken, some static requests always reproduce the issue.
That might actually be then that you're hitting the original root cause of https://github.com/dotnet/corefx/issues/5949
The issue described there is that the buffer pool will allow return of "off-size" arrays that don't exactly match a bucket. For instance, if you ask for a 13-element array, the pool will give you a 16-element array because it uses bucketing to encourage greater reuse. When you return that 16-element array, it goes back into the bucket.
However, if you construct a 13-element array with new T[13] and pass it back in, the pool will accept it and store it in the 16-element bucket. Then when you ask for a 15-element array, it gives you one from the 16-element bucket. Probably the 13-element array you returned earlier.... and BOOM.
The change in dotnet/corefx#5949 is to throw an exception when this happens, so you can avoid the bug. I think we'll have to do a tactical fix for this in MVC and revert it once a JSON.Net build is available with the full fix.
@rynowak can/should we disable buffer pooling completely in MVC right now?
That's one option, the other is to to add some tracking to our pool -> JSON.Net adapter. Fixing the issue in the adapter is less churn for us and will perturb our performance measurements less.
I'm trying to follow along... do all of these concerns arise because we're throwing when an invalid buffer is incorrectly returned back to the pool rather than simply dropping the buffer silently?
@stephentoub yes. JSON.Net is doing exactly what the exception is trying to prevent. The problem is that the exception was added after 8.0.2 shipped, so no one knew we had an issue until the System.Buffers with the fix for dotnet/corefx#5949 hit users.
@ajaybhargavb can you implement @rynowak 's suggestion in the meantime?
And I logged https://github.com/aspnet/Mvc/issues/4262 to undo the workaround that we do.
Since there is no date on RC2 I just created a Json.NET release with the fix - https://github.com/JamesNK/Newtonsoft.Json/releases/tag/8.0.3
Thanks @JamesNK !
I confirm that I can not reproduce the issue(s) this last Json.Net bits (along with last System.Buffers bits). Thanks @JamesNK.
@ajaybhargavb OK the action item here will be to depend on the fixed version rather than to put a workaround in our code.
Updated all repos to use 8.0.3.
Most helpful comment
Since there is no date on RC2 I just created a Json.NET release with the fix - https://github.com/JamesNK/Newtonsoft.Json/releases/tag/8.0.3