Skip to content
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
3 changes: 0 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ dotnet_sort_system_directives_first = true
# VSTHRD002: Avoid problematic synchronous waits
dotnet_diagnostic.VSTHRD002.severity = none

# VSTHRD103: Call async methods when in an async method
dotnet_diagnostic.VSTHRD103.severity = none

# VSTHRD200: Use "Async" suffix for async methods
dotnet_diagnostic.VSTHRD200.severity = none

Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.Core/Utilities/ResourceManagerUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ private async Task<string> DownloadFromUrl(IHostEnvironment env, IChannel ch, st
var timeoutTask = Task.Delay(timeout).ContinueWith(task => default(Exception), TaskScheduler.Default);
ch.Info($"Downloading {fileName} from {url} to {filePath}");
var completedTask = await Task.WhenAny(t, timeoutTask);
if (completedTask != t || completedTask.Result != null)
if (completedTask != t || completedTask.CompletedResult() != null)
{
downloadCancel.Cancel();
deleteNeeded = true;
return t.Result.Message;
return (await t).Message;
}

return CheckValidDownload(ch, filePath, url, ref deleteNeeded);
Expand Down
20 changes: 20 additions & 0 deletions src/Microsoft.ML.Core/Utilities/TaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using Microsoft.ML.Runtime;

namespace Microsoft.ML.Internal.Utilities
{
internal static class TaskExtensions
{
[SuppressMessage("Usage", "VSTHRD002:Avoid problematic synchronous waits", Justification = "The task is completed.")]
public static TResult CompletedResult<TResult>(this Task<TResult> task)
{
Contracts.Check(task.IsCompleted);
return task.Result;
}
}
}
14 changes: 7 additions & 7 deletions test/Microsoft.ML.Sweeper.Tests/TestSweeper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public async Task TestDeterministicSweeperAsyncCancellation()
if (i < args.BatchSize - args.Relaxation)
{
Assert.True(task.IsCompleted);
sweeper.Update(task.Result.Id, new RunResult(task.Result.ParameterSet, random.NextDouble(), true));
sweeper.Update(task.CompletedResult().Id, new RunResult(task.CompletedResult().ParameterSet, random.NextDouble(), true));
numCompleted++;
}
else
Expand All @@ -218,7 +218,7 @@ public async Task TestDeterministicSweeperAsyncCancellation()
await Task.WhenAll(tasks);
foreach (var task in tasks)
{
if (task.Result != null)
if (task.CompletedResult() != null)
numCompleted++;
}
Assert.Equal(args.BatchSize + args.BatchSize, numCompleted);
Expand Down Expand Up @@ -254,9 +254,9 @@ public async Task TestDeterministicSweeperAsync()
{
var task = sweeper.Propose();
Assert.True(task.IsCompleted);
paramSets.Add(task.Result.ParameterSet);
var result = new RunResult(task.Result.ParameterSet, random.NextDouble(), true);
sweeper.Update(task.Result.Id, result);
paramSets.Add(task.CompletedResult().ParameterSet);
var result = new RunResult(task.CompletedResult().ParameterSet, random.NextDouble(), true);
sweeper.Update(task.CompletedResult().Id, result);
}
Assert.Equal(sweeps, paramSets.Count);
CheckAsyncSweeperResult(paramSets);
Expand All @@ -273,9 +273,9 @@ public async Task TestDeterministicSweeperAsync()
var task = sweeper.Propose();
Assert.True(task.IsCompleted);
tasks[i] = task;
if (task.Result == null)
if (task.CompletedResult() == null)
continue;
results.Add(new KeyValuePair<int, IRunResult>(task.Result.Id, new RunResult(task.Result.ParameterSet, 0.42, true)));
results.Add(new KeyValuePair<int, IRunResult>(task.CompletedResult().Id, new RunResult(task.CompletedResult().ParameterSet, 0.42, true)));
}
// Register consumers for the 2nd batch. Those consumers will await until at least one run
// in the previous batch has been posted to the sweeper.
Expand Down