Skip to content

Commit

Permalink
Ensure FileBufferingReadStream created by formatters are always dispo…
Browse files Browse the repository at this point in the history
…sed (#22746)
  • Loading branch information
pranavkm committed Jun 11, 2020
1 parent 5a9a985 commit 75b2775
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
}

readStream = new FileBufferingReadStream(request.Body, memoryThreshold);
// Ensure the file buffer stream is always disposed at the end of a request.
request.HttpContext.Response.RegisterForDispose(readStream);

await readStream.DrainAsync(CancellationToken.None);
readStream.Seek(0L, SeekOrigin.Begin);
Expand Down
2 changes: 2 additions & 0 deletions src/Mvc/Mvc.Formatters.Xml/src/XmlSerializerInputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
}

readStream = new FileBufferingReadStream(request.Body, memoryThreshold);
// Ensure the file buffer stream is always disposed at the end of a request.
request.HttpContext.Response.RegisterForDispose(readStream);

await readStream.DrainAsync(CancellationToken.None);
readStream.Seek(0L, SeekOrigin.Begin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
}

readStream = new FileBufferingReadStream(request.Body, memoryThreshold);
// Ensure the file buffer stream is always disposed at the end of a request.
request.HttpContext.Response.RegisterForDispose(readStream);

await readStream.DrainAsync(CancellationToken.None);
readStream.Seek(0L, SeekOrigin.Begin);
Expand Down Expand Up @@ -278,7 +280,7 @@ void ErrorHandler(object sender, Newtonsoft.Json.Serialization.ErrorEventArgs ev

/// <summary>
/// Called during deserialization to get the <see cref="JsonSerializer"/>. The formatter context
/// that is passed gives an ability to create serializer specific to the context.
/// that is passed gives an ability to create serializer specific to the context.
/// </summary>
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>
/// <remarks>
Expand All @@ -297,7 +299,7 @@ protected virtual JsonSerializer CreateJsonSerializer()

/// <summary>
/// Called during deserialization to get the <see cref="JsonSerializer"/>. The formatter context
/// that is passed gives an ability to create serializer specific to the context.
/// that is passed gives an ability to create serializer specific to the context.
/// </summary>
/// <param name="context">A context object used by an input formatter for deserializing the request body into an object.</param>
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>
Expand Down

0 comments on commit 75b2775

Please sign in to comment.