Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dispose stream reader async when possible #14862

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
}
finally
{
stream?.Dispose();
await stream.DisposeAsync();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Extensions.FileProviders;

namespace OrchardCore.Modules.FileProviders
{
public static class FileInfoExtensions
{
public static IEnumerable<string> ReadAllLines(this IFileInfo fileInfo)
=> ReadAllLinesAsync(fileInfo).GetAwaiter().GetResult();

public static async Task<IEnumerable<string>> ReadAllLinesAsync(this IFileInfo fileInfo)
{
var lines = new List<string>();

if (fileInfo?.Exists ?? false)
{
using var reader = fileInfo.CreateReadStream();
await using var reader = fileInfo.CreateReadStream();
using var sr = new StreamReader(reader);

string line;
while ((line = sr.ReadLine()) != null)
while ((line = await sr.ReadLineAsync()) != null)
{
lines.Add(line);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,15 @@ public static Task<LiquidViewTemplate> ParseAsync(LiquidViewParser parser, strin
entry.ExpirationTokens.Add(fileProvider.Watch(path));
}

using var stream = fileInfo.CreateReadStream();
await using var stream = fileInfo.CreateReadStream();
using var sr = new StreamReader(stream);

if (parser.TryParse(await sr.ReadToEndAsync(), out var template, out var errors))
{
return new LiquidViewTemplate(template);
}
else
{
throw new Exception($"Failed to parse liquid file {path}: {string.Join(System.Environment.NewLine, errors)}");
}

throw new Exception($"Failed to parse liquid file {path}: {string.Join(System.Environment.NewLine, errors)}");
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public async Task<string> ExecuteAsync(string executionId, RecipeDescriptor reci

var result = new RecipeResult { ExecutionId = executionId };

using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream())
await using (var stream = recipeDescriptor.RecipeFileInfo.CreateReadStream())
{
using var file = new StreamReader(stream);
using var reader = new JsonTextReader(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public async Task<RecipeDescriptor> GetRecipeDescriptorAsync(string recipeBasePa
{
// TODO: Try to optimize by only reading the required metadata instead of the whole file.

using var stream = recipeFileInfo.CreateReadStream();
await using var stream = recipeFileInfo.CreateReadStream();
using var reader = new StreamReader(stream);
using var jsonReader = new JsonTextReader(reader);

Expand Down
9 changes: 7 additions & 2 deletions test/OrchardCore.Tests/Utilities/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ namespace OrchardCore.Tests.Utilities
public static class FileInfoExtensions
{
public static string ReadToEnd(this IFileInfo fileInfo)
=> ReadToEndAsync(fileInfo).GetAwaiter().GetResult();

public static async Task<string> ReadToEndAsync(this IFileInfo fileInfo)
{
using var stream = fileInfo.CreateReadStream();
await using var stream = fileInfo.CreateReadStream();

using var streamReader = new StreamReader(stream);
return streamReader.ReadToEnd();

return await streamReader.ReadToEndAsync();
}
}
}