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

[OrchardCore.Modules.Cms.Alias] Add thread-safety for YesSql session queries. Fixes #16069 #16095

Closed
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 @@ -31,7 +31,7 @@ public static async Task<string> GetContentItemIdByAliasAsync(this IOrchardHelpe
}

var session = orchardHelper.HttpContext.RequestServices.GetService<ISession>();
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndex(session, alias);
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndexAsync(session, alias);

return aliasPartIndex?.ContentItemId;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Threading;
using System.Threading.Tasks;
using OrchardCore.Alias.Indexes;
using OrchardCore.ContentManagement;
using YesSql;

namespace OrchardCore.Alias.Services
{
internal sealed class AliasPartContentHandleHelper
{
private static readonly SemaphoreSlim _yesSqlLock = new(1, 1);

#pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
internal static async Task<ContentItem> QueryAliasIndexAsync(ISession session, string alias)
{
// NOTE: YesSql/Dapper does not support parallel or concurrent requests.
// Doing so can cause an `InvalidOperationException`, with the connection stuck within a "connecting" state.
await _yesSqlLock.WaitAsync();
try
{
return await session.Query<ContentItem, AliasPartIndex>(x => x.Alias == alias.ToLowerInvariant()).FirstOrDefaultAsync();
}
finally
{
_yesSqlLock?.Release();
}
}
#pragma warning restore CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using OrchardCore.Alias.Indexes;
using OrchardCore.ContentManagement;
using YesSql;

Expand All @@ -23,19 +22,11 @@ public async Task<string> GetContentItemIdAsync(string handle)
{
handle = handle[6..];

var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndex(_session, handle);
var aliasPartIndex = await AliasPartContentHandleHelper.QueryAliasIndexAsync(_session, handle);
return aliasPartIndex?.ContentItemId;
}

return null;
}
}

internal sealed class AliasPartContentHandleHelper
{
#pragma warning disable CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
internal static Task<ContentItem> QueryAliasIndex(ISession session, string alias) =>
session.Query<ContentItem, AliasPartIndex>(x => x.Alias == alias.ToLowerInvariant()).FirstOrDefaultAsync();
#pragma warning restore CA1862 // Use the 'StringComparison' method overloads to perform case-insensitive string comparisons
}
}