Skip to content

Commit

Permalink
Dispose FileReader asynchronously (#14862)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Dec 9, 2023
1 parent 97ad5ca commit 4f5a01d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
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();
}
}
}

0 comments on commit 4f5a01d

Please sign in to comment.