Skip to content

Commit

Permalink
#407 - Fix freezing on "Authorizing" screen.
Browse files Browse the repository at this point in the history
The raised exception from profile query caused exception
on awaited task for getting the authentication token.

Second problem was with tasks in middlewares that weren't
properly cleared on exception.
  • Loading branch information
maraf committed Aug 2, 2021
1 parent e63d110 commit 6b7bd9e
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/Money.Blazor.Host/Layouts/UserInfo.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Money.Services;
using Neptuo.Events;
using Neptuo.Events.Handlers;
using Neptuo.Logging;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
Expand All @@ -33,6 +34,9 @@ public partial class UserInfo : IDisposable, IEventHandler<UserSignedIn>, IEvent
[Inject]
public AuthenticationStateProvider AuthenticationState { get; set; }

[Inject]
public ILog<UserInfo> Log { get; set; }

[Parameter]
public string ListCssClass { get; set; }

Expand Down Expand Up @@ -85,6 +89,8 @@ private void UnBindEvents()

async Task IEventHandler<UserSignedIn>.HandleAsync(UserSignedIn payload)
{
Log.Debug("User signed in.");

await LoadProfileAsync();
StateHasChanged();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ private async Task ChangeTokenAsync(string value, bool isValidationRequired = tr
return;
}
}
else
{
EnsurePrincipal(null);
}

log.Debug("Clearing token.");

Expand Down Expand Up @@ -173,6 +177,7 @@ public async override Task<AuthenticationState> GetAuthenticationStateAsync()
if (principal == null)
EnsurePrincipal(await GetTokenAsync());

log.Debug($"AuthenticationState '{principal?.Identity}'.");
return new AuthenticationState(principal);
}

Expand Down
10 changes: 8 additions & 2 deletions src/Money.Blazor.Host/Services/CategoryMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,14 @@ private async Task EnsureListAsync(HttpQueryDispatcher dispatcher, HttpQueryDisp

log.Debug($"Awating task.");

await listAllTask;
listAllTask = null;
try
{
await listAllTask;
}
finally
{
listAllTask = null;
}

log.Debug($"Awaiting done.");
}
Expand Down
10 changes: 8 additions & 2 deletions src/Money.Blazor.Host/Services/CurrencyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ private async Task EnsureListAsync(HttpQueryDispatcher dispatcher, HttpQueryDisp
if (listAllTask == null)
listAllTask = LoadAllAsync(dispatcher, next, listAll);

await listAllTask;
listAllTask = null;
try
{
await listAllTask;
}
finally
{
listAllTask = null;
}
}
}

Expand Down
25 changes: 22 additions & 3 deletions src/Money.Blazor.Host/Services/UserMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Money.Models.Queries;
using Neptuo;
using Neptuo.Events.Handlers;
using Neptuo.Logging;
using Neptuo.Queries;
using System;
using System.Collections.Generic;
Expand All @@ -18,16 +19,19 @@ internal class UserMiddleware : HttpQueryDispatcher.IMiddleware,
{
private readonly ServerConnectionState serverConnection;
private readonly ProfileStorage localStorage;
private readonly ILog log;

private ProfileModel profile;
private Task getProfileTask;

public UserMiddleware(ServerConnectionState serverConnection, ProfileStorage localStorage)
public UserMiddleware(ServerConnectionState serverConnection, ProfileStorage localStorage, ILogFactory logFactory)
{
Ensure.NotNull(serverConnection, "serverConnection");
Ensure.NotNull(localStorage, "localStorage");
Ensure.NotNull(logFactory, "logFactory");
this.serverConnection = serverConnection;
this.localStorage = localStorage;
this.log = logFactory.Scope("UserMiddleware");
}

public async Task<object> ExecuteAsync(object query, HttpQueryDispatcher dispatcher, HttpQueryDispatcher.Next next)
Expand All @@ -36,13 +40,27 @@ public async Task<object> ExecuteAsync(object query, HttpQueryDispatcher dispatc
{
if (profile == null)
{
log.Debug("Profile is null.");

if (getProfileTask == null)
{
log.Debug("Profile task is null.");
getProfileTask = LoadProfileAsync(getProfile, next);
}

await getProfileTask;
getProfileTask = null;
try
{
log.Debug("Awating profile task.");
await getProfileTask;
}
finally
{
log.Debug("Clearing profile task.");
getProfileTask = null;
}
}

log.Debug("Returning profile.");
return profile;
}

Expand All @@ -58,6 +76,7 @@ private async Task LoadProfileAsync(GetProfile query, HttpQueryDispatcher.Next n
return;
}

log.Debug("Get profile over the wire.");
profile = (ProfileModel)await next(query);
await localStorage.SaveAsync(profile);
}
Expand Down
10 changes: 8 additions & 2 deletions src/Money.Blazor.Host/Services/UserPropertyMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ private async Task EnsureListAsync(HttpQueryDispatcher dispatcher, HttpQueryDisp
if (listAllTask == null)
listAllTask = LoadAllAsync(dispatcher, next, listAll);

await listAllTask;
listAllTask = null;
try
{
await listAllTask;
}
finally
{
listAllTask = null;
}
}
}

Expand Down

0 comments on commit 6b7bd9e

Please sign in to comment.