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

[Windows] Unwrap AggregateException when catching exceptions from Task.Wait() method #602

Merged
merged 9 commits into from
Apr 16, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Utils\Synchronization\State.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Synchronization\StatefulMutex.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\Synchronization\StatefulMutexException.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\TaskExtension.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\TimeHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utils\UnhandledExceptionOccurredEventArgs.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using Microsoft.AppCenter.Ingestion.Models;
using Microsoft.AppCenter.Ingestion.Models.Serialization;
using Microsoft.AppCenter.Utils;
using Newtonsoft.Json;
using SQLite;

Expand Down Expand Up @@ -52,7 +53,7 @@ public Storage() : this(DefaultAdapter())
internal Storage(IStorageAdapter adapter)
{
_storageAdapter = adapter;
_queue.Add(new Task(() => InitializeDatabaseAsync().Wait()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like @MatkovIvan mentioned in a conversation we had, just using .GetAwaiter().GetResult() would remove the need for an extension utils, please switch to that and remove the additional extension.

_queue.Add(new Task(() => InitializeDatabaseAsync().RunNotAsync()));
_queueFlushTask = Task.Run(FlushQueueAsync);
}

Expand Down Expand Up @@ -87,7 +88,7 @@ public Task PutLog(string channelName, Log log)
{
var logJsonString = LogSerializer.Serialize(log);
var logEntry = new LogEntry {Channel = channelName, Log = logJsonString};
_storageAdapter.InsertAsync(logEntry).Wait();
_storageAdapter.InsertAsync(logEntry).RunNotAsync();
});
try
{
Expand Down Expand Up @@ -128,7 +129,7 @@ public Task DeleteLogs(string channelName, string batchId)
{
_storageAdapter
.DeleteAsync<LogEntry>(entry => entry.Channel == channelName && entry.Id == id)
.Wait();
.RunNotAsync();
}
}
catch (KeyNotFoundException e)
Expand Down Expand Up @@ -163,7 +164,7 @@ public Task DeleteLogs(string channelName)
$"Deleting all logs from storage for channel '{channelName}'");
ClearPendingLogStateWithoutEnqueue(channelName);
_storageAdapter.DeleteAsync<LogEntry>(entry => entry.Channel == channelName)
.Wait();
.RunNotAsync();
}
catch (KeyNotFoundException e)
{
Expand Down Expand Up @@ -290,7 +291,7 @@ public async Task<string> GetLogsAsync(string channelName, int limit, List<Log>
AppCenterLog.Error(AppCenterLog.LogTag, "Cannot deserialize a log in storage", e);
failedToDeserializeALog = true;
_storageAdapter.DeleteAsync<LogEntry>(row => row.Id == entry.Id)
.Wait();
.RunNotAsync();
}
}
if (failedToDeserializeALog)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Threading.Tasks;

namespace Microsoft.AppCenter.Utils
{
internal static class TaskExtension
{
public static T RunNotAsync<T>(this Task<T> @this)
{
try
{
@this.Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}

return @this.Result;
}

public static void RunNotAsync(this Task @this)
{
try
{
@this.Wait();
}
catch (AggregateException e)
{
throw e.InnerException;
}
}
}
}