Stop using TaskCreationOptions.LongRunning

This commit is contained in:
Jordan Brown
2020-08-17 21:42:09 -04:00
parent 3e9457fb3e
commit bf313e9e28
12 changed files with 62 additions and 51 deletions
@@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Host.Core;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.IO;
using Tgstation.Server.Host.Jobs;
using Tgstation.Server.Host.System;
@@ -302,7 +303,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
Logger.LogTrace("Exiting listening task...");
},
cancellationToken,
TaskCreationOptions.LongRunning,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
await nickCheckCompleteTcs.Task.ConfigureAwait(false);
@@ -430,7 +431,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
}
},
cancellationToken,
TaskCreationOptions.LongRunning,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current)
.ConfigureAwait(false);
await HardDisconnect(cancellationToken).ConfigureAwait(false);
@@ -472,7 +473,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
}
},
cancellationToken,
TaskCreationOptions.None,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
await Task.WhenAny(
@@ -545,7 +546,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
})
.ToList();
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public override Task SendMessage(ulong channelId, string message, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -573,7 +574,7 @@ namespace Tgstation.Server.Host.Components.Chat.Providers
{
Logger.LogWarning(e, "Unable to send to channel {0}!", channelName);
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public override async Task<Func<string, string, Task>> SendUpdateMessage(
@@ -5,6 +5,7 @@ using System;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Host.IO;
using Tgstation.Server.Host.Jobs;
namespace Tgstation.Server.Host.Components.Repository
@@ -46,7 +47,7 @@ namespace Tgstation.Server.Host.Components.Repository
return new LibGit2Sharp.Repository(path);
},
cancellationToken,
TaskCreationOptions.LongRunning,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
}
@@ -63,7 +64,7 @@ namespace Tgstation.Server.Host.Components.Repository
logger.LogTrace(ex, "Suppressing clone cancellation exception");
cancellationToken.ThrowIfCancellationRequested();
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public CredentialsHandler GenerateCredentialsHandler(string username, string password) => (a, b, supportedCredentialTypes) =>
@@ -375,7 +375,7 @@ namespace Tgstation.Server.Host.Components.Repository
}
libGitRepo.RemoveUntrackedFiles();
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
if (result.Status == MergeStatus.Conflicts)
{
@@ -389,7 +389,7 @@ namespace Tgstation.Server.Host.Components.Repository
await Task.Factory.StartNew(() => libGitRepo.Commit(commitMessage, sig, sig, new CommitOptions
{
PrettifyMessage = true
}), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}), cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
}
await eventConsumer.HandleEvent(
@@ -420,7 +420,7 @@ namespace Tgstation.Server.Host.Components.Repository
{
libGitRepo.RemoveUntrackedFiles();
RawCheckout(committish, progressReporter, cancellationToken);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
}
/// <inheritdoc />
@@ -453,7 +453,7 @@ namespace Tgstation.Server.Host.Components.Repository
{
cancellationToken.ThrowIfCancellationRequested();
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
}
/// <summary>
@@ -492,7 +492,7 @@ namespace Tgstation.Server.Host.Components.Repository
{
libGitRepo.Branches.Remove(branch);
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public async Task ResetToOrigin(Action<int> progressReporter, CancellationToken cancellationToken)
@@ -530,7 +530,7 @@ namespace Tgstation.Server.Host.Components.Repository
{
OnCheckoutProgress = CheckoutProgressHandler(progressReporter)
});
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public async Task CopyTo(string path, CancellationToken cancellationToken)
@@ -550,7 +550,7 @@ namespace Tgstation.Server.Host.Components.Repository
cancellationToken.ThrowIfCancellationRequested();
return libGitRepo.Head.TrackedBranch.Tip.Sha;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public async Task<bool?> MergeOrigin(string committerName, string committerEmail, Action<int> progressReporter, CancellationToken cancellationToken)
@@ -601,7 +601,7 @@ namespace Tgstation.Server.Host.Components.Repository
}
libGitRepo.RemoveUntrackedFiles();
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
if (result.Status == MergeStatus.Conflicts)
{
@@ -643,7 +643,7 @@ namespace Tgstation.Server.Host.Components.Repository
libGitRepo.Config.Set("user.name", committerName);
cancellationToken.ThrowIfCancellationRequested();
libGitRepo.Config.Set("user.email", committerEmail);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
try
@@ -668,7 +668,7 @@ namespace Tgstation.Server.Host.Components.Repository
{
OnCheckoutProgress = CheckoutProgressHandler(progress => progressReporter(progress / 10))
});
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
}
void FinalReporter(int progress) => progressReporter((int)(((float)progress) / 100 * 90));
@@ -711,7 +711,7 @@ namespace Tgstation.Server.Host.Components.Repository
logger.LogWarning(e, "Unable to make synchronization push!");
return false;
}
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
}
/// <inheritdoc />
@@ -732,7 +732,7 @@ namespace Tgstation.Server.Host.Components.Repository
if (libGitRepo.Lookup<Commit>(committish) != null)
return true;
return false;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<bool> ShaIsParent(string sha, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -770,6 +770,6 @@ namespace Tgstation.Server.Host.Components.Repository
startSha);
return false;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
@@ -287,7 +287,7 @@ namespace Tgstation.Server.Host.Components.StaticFiles
using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false))
if (systemIdentity == null)
await Task.Factory.StartNew(ReadImpl, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
await Task.Factory.StartNew(ReadImpl, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
else
await systemIdentity.RunImpersonated(ReadImpl, cancellationToken).ConfigureAwait(false);
@@ -418,7 +418,7 @@ namespace Tgstation.Server.Host.Components.StaticFiles
using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false))
if (systemIdentity == null)
await Task.Factory.StartNew(WriteImpl, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
await Task.Factory.StartNew(WriteImpl, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
else
await systemIdentity.RunImpersonated(WriteImpl, cancellationToken).ConfigureAwait(false);
@@ -436,7 +436,7 @@ namespace Tgstation.Server.Host.Components.StaticFiles
using (await SemaphoreSlimContext.Lock(semaphore, cancellationToken).ConfigureAwait(false))
if (systemIdentity == null)
await Task.Factory.StartNew(DoCreate, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current).ConfigureAwait(false);
await Task.Factory.StartNew(DoCreate, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current).ConfigureAwait(false);
else
await systemIdentity.RunImpersonated(DoCreate, cancellationToken).ConfigureAwait(false);
+4 -4
View File
@@ -1,4 +1,4 @@
using System;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -61,7 +61,7 @@ namespace Tgstation.Server.Host.IO
{
CheckAvailable();
global::System.Console.Read();
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<string> ReadLineAsync(bool usePasswordChar, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -97,7 +97,7 @@ namespace Tgstation.Server.Host.IO
cancellationToken.ThrowIfCancellationRequested();
global::System.Console.WriteLine();
return passwordBuilder.ToString();
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task WriteAsync(string text, bool newLine, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -113,6 +113,6 @@ namespace Tgstation.Server.Host.IO
global::System.Console.WriteLine(text);
else
global::System.Console.Write(text);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
@@ -24,6 +24,11 @@ namespace Tgstation.Server.Host.IO
/// </summary>
public const int DefaultBufferSize = 4096;
/// <summary>
/// The <see cref="TaskCreationOptions"/> used to spawn <see cref="Task"/>s for potentially long running, blocking operations.
/// </summary>
public const TaskCreationOptions BlockingTaskCreationOptions = TaskCreationOptions.None;
/// <summary>
/// Recursively empty a directory
/// </summary>
@@ -149,7 +154,7 @@ namespace Tgstation.Server.Host.IO
}
/// <inheritdoc />
public Task CreateDirectory(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => Directory.CreateDirectory(ResolvePath(path)), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
public Task CreateDirectory(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => Directory.CreateDirectory(ResolvePath(path)), cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task DeleteDirectory(string path, CancellationToken cancellationToken)
@@ -162,18 +167,18 @@ namespace Tgstation.Server.Host.IO
return Task.Factory.StartNew(
() => NormalizeAndDelete(di, cancellationToken),
cancellationToken,
TaskCreationOptions.LongRunning,
BlockingTaskCreationOptions,
TaskScheduler.Current);
}
/// <inheritdoc />
public Task DeleteFile(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => File.Delete(ResolvePath(path)), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
public Task DeleteFile(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => File.Delete(ResolvePath(path)), cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<bool> FileExists(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => File.Exists(ResolvePath(path)), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
public Task<bool> FileExists(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => File.Exists(ResolvePath(path)), cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<bool> DirectoryExists(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => Directory.Exists(ResolvePath(path)), cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
public Task<bool> DirectoryExists(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() => Directory.Exists(ResolvePath(path)), cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public string GetDirectoryName(string path) => Path.GetDirectoryName(path ?? throw new ArgumentNullException(nameof(path)));
@@ -201,7 +206,7 @@ namespace Tgstation.Server.Host.IO
}
return results;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task MoveFile(string source, string destination, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -211,7 +216,7 @@ namespace Tgstation.Server.Host.IO
source = ResolvePath(source ?? throw new ArgumentNullException(nameof(source)));
destination = ResolvePath(destination);
File.Move(source, destination);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task MoveDirectory(string source, string destination, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -221,7 +226,7 @@ namespace Tgstation.Server.Host.IO
source = ResolvePath(source ?? throw new ArgumentNullException(nameof(source)));
destination = ResolvePath(destination);
Directory.Move(source, destination);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public async Task<byte[]> ReadAllBytes(string path, CancellationToken cancellationToken)
@@ -261,7 +266,7 @@ namespace Tgstation.Server.Host.IO
}
return (IReadOnlyList<string>)results;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<IReadOnlyList<string>> GetFiles(string path, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -276,7 +281,7 @@ namespace Tgstation.Server.Host.IO
}
return (IReadOnlyList<string>)results;
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public async Task<byte[]> DownloadFile(Uri url, CancellationToken cancellationToken)
@@ -312,7 +317,7 @@ namespace Tgstation.Server.Host.IO
using var ms = new MemoryStream(zipFileBytes);
using var archive = new ZipArchive(ms, ZipArchiveMode.Read);
archive.ExtractToDirectory(path);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public bool PathContainsParentAccess(string path) => path?.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }).Any(x => x == "..") ?? throw new ArgumentNullException(nameof(path));
@@ -323,6 +328,6 @@ namespace Tgstation.Server.Host.IO
path = ResolvePath(path ?? throw new ArgumentNullException(nameof(path)));
var fileInfo = new FileInfo(path);
return new DateTimeOffset(fileInfo.LastWriteTimeUtc);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
@@ -1,4 +1,4 @@
using Mono.Unix;
using Mono.Unix;
using System;
using System.IO;
using System.Threading;
@@ -28,6 +28,6 @@ namespace Tgstation.Server.Host.IO
fsInfo = new UnixDirectoryInfo(targetPath);
cancellationToken.ThrowIfCancellationRequested();
fsInfo.CreateSymbolicLink(linkPath);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
@@ -1,4 +1,4 @@
using BetterWin32Errors;
using BetterWin32Errors;
using System;
using System.IO;
using System.Threading;
@@ -37,6 +37,6 @@ namespace Tgstation.Server.Host.IO
cancellationToken.ThrowIfCancellationRequested();
if (!NativeMethods.CreateSymbolicLink(linkPath, targetPath, flags))
throw new Win32Exception();
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
@@ -1,8 +1,9 @@
using System;
using System;
using System.DirectoryServices.AccountManagement;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Host.IO;
namespace Tgstation.Server.Host.Security
{
@@ -90,6 +91,6 @@ namespace Tgstation.Server.Host.Security
if (identity == null)
throw new InvalidOperationException("Impersonate using a UserPrincipal based WindowsSystemIdentity!");
WindowsIdentity.RunImpersonated(identity.AccessToken, action);
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
}
@@ -5,6 +5,7 @@ using System.DirectoryServices.AccountManagement;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Host.IO;
using Tgstation.Server.Host.Models;
namespace Tgstation.Server.Host.Security
@@ -88,7 +89,7 @@ namespace Tgstation.Server.Host.Security
return null;
return (ISystemIdentity)new WindowsSystemIdentity(principal);
},
cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
/// <inheritdoc />
public Task<ISystemIdentity> CreateSystemIdentity(string username, string password, CancellationToken cancellationToken) => Task.Factory.StartNew(() =>
@@ -114,6 +115,6 @@ namespace Tgstation.Server.Host.Security
using var handle = new SafeAccessTokenHandle(token);
return (ISystemIdentity)new WindowsSystemIdentity(
new WindowsIdentity(handle.DangerousGetHandle())); // https://github.com/dotnet/corefx/blob/6ed61acebe3214fcf79b4274f2bb9b55c0604a4d/src/System.Security.Principal.Windows/src/System/Security/Principal/WindowsIdentity.cs#L271
}, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Current);
}, cancellationToken, DefaultIOManager.BlockingTaskCreationOptions, TaskScheduler.Current);
}
}
+2 -1
View File
@@ -6,6 +6,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Host.Extensions;
using Tgstation.Server.Host.IO;
namespace Tgstation.Server.Host.System
{
@@ -97,7 +98,7 @@ namespace Tgstation.Server.Host.System
}
},
default, // DCT: None available
TaskCreationOptions.LongRunning,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
logger.LogTrace("Created process ID: {0}", Id);
@@ -7,6 +7,7 @@ using System.Management;
using System.Threading;
using System.Threading.Tasks;
using Tgstation.Server.Api.Models;
using Tgstation.Server.Host.IO;
using Tgstation.Server.Host.Jobs;
namespace Tgstation.Server.Host.System
@@ -119,7 +120,7 @@ namespace Tgstation.Server.Host.System
throw new Win32Exception();
},
cancellationToken,
TaskCreationOptions.LongRunning,
DefaultIOManager.BlockingTaskCreationOptions,
TaskScheduler.Current);
}
}