Make Framework.DelayTicks() deadlock-safe before the game has started

This commit is contained in:
goaaats 2026-01-27 23:43:51 +01:00
parent 470267a185
commit 5c250c1725

View file

@ -121,9 +121,9 @@ internal sealed class Framework : IInternalDisposableService, IFramework
/// <inheritdoc/> /// <inheritdoc/>
public Task DelayTicks(long numTicks, CancellationToken cancellationToken = default) public Task DelayTicks(long numTicks, CancellationToken cancellationToken = default)
{ {
if (this.frameworkDestroy.IsCancellationRequested) if (this.frameworkDestroy.IsCancellationRequested) // Going away
return Task.FromCanceled(this.frameworkDestroy.Token); return Task.FromCanceled(this.frameworkDestroy.Token);
if (numTicks <= 0) if (numTicks <= 0 || this.frameworkThreadTaskScheduler.BoundThread == null) // Nonsense or before first tick
return Task.CompletedTask; return Task.CompletedTask;
var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); var tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);