Skip to content

Commit 75b2775

Browse files
authored
Ensure FileBufferingReadStream created by formatters are always disposed (#22746)
1 parent 5a9a985 commit 75b2775

File tree

3 files changed

+8
-2
lines changed

3 files changed

+8
-2
lines changed

src/Mvc/Mvc.Formatters.Xml/src/XmlDataContractSerializerInputFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
143143
}
144144

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

147149
await readStream.DrainAsync(CancellationToken.None);
148150
readStream.Seek(0L, SeekOrigin.Begin);

src/Mvc/Mvc.Formatters.Xml/src/XmlSerializerInputFormatter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
124124
}
125125

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

128130
await readStream.DrainAsync(CancellationToken.None);
129131
readStream.Seek(0L, SeekOrigin.Begin);

src/Mvc/Mvc.NewtonsoftJson/src/NewtonsoftJsonInputFormatter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(
153153
}
154154

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

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

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

298300
/// <summary>
299301
/// Called during deserialization to get the <see cref="JsonSerializer"/>. The formatter context
300-
/// that is passed gives an ability to create serializer specific to the context.
302+
/// that is passed gives an ability to create serializer specific to the context.
301303
/// </summary>
302304
/// <param name="context">A context object used by an input formatter for deserializing the request body into an object.</param>
303305
/// <returns>The <see cref="JsonSerializer"/> used during deserialization.</returns>

0 commit comments

Comments
 (0)