Skip to content

Commit d9f95a3

Browse files
authored
Tweak timeoutExceptionTask (#435)
* Tweak timeoutExceptionTask * ReleaseNotes.md * non async cancel * #if NET8_0_OR_GREATER * Formatting Markdown --------- Co-authored-by: Tom Longhurst <thomhurst@users.noreply.github.com>
1 parent f553036 commit d9f95a3

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ Define your pipeline in .NET! Strong types, intellisense, parallelisation, and t
6363
| ModularPipelines.WinGet | Helpers for interacting with the Windows Package Manager. | [![nuget](https://img.shields.io/nuget/v/ModularPipelines.WinGet.svg)](https://www.nuget.org/packages/ModularPipelines.WinGet/) |
6464
| ModularPipelines.Yarn | Helpers for interacting with Yarn CLI. | [![nuget](https://img.shields.io/nuget/v/ModularPipelines.Yarn.svg)](https://www.nuget.org/packages/ModularPipelines.Yarn/) |
6565

66-
6766
## Getting Started
6867

6968
If you want to see how to get started, or want to know more about ModularPipelines, [read the Documentation here](https://thomhurst.github.io/ModularPipelines)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Tweak logic for throwing Timeout Exceptions

src/ModularPipelines/Modules/Module.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +320,15 @@ private void SetResult(ModuleResult<T> result)
320320
ModuleCancellationTokenSource.CancelAfter(Timeout);
321321

322322
var timeoutCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(ModuleCancellationTokenSource.Token);
323-
_ = executeAsyncTask.ContinueWith(async t =>
324-
{
325-
await Task.Delay(TimeSpan.FromSeconds(5), CancellationToken.None);
326-
timeoutCancellationTokenSource.Cancel();
327-
timeoutCancellationTokenSource.Dispose();
328-
}, CancellationToken.None);
329323

330324
var timeoutExceptionTask = Task.Delay(Timeout, timeoutCancellationTokenSource.Token)
331325
.ContinueWith(t =>
332326
{
327+
if (executeAsyncTask.IsCompleted)
328+
{
329+
return;
330+
}
331+
333332
if (ModuleRunType == ModuleRunType.OnSuccessfulDependencies)
334333
{
335334
Context.EngineCancellationToken.Token.ThrowIfCancellationRequested();
@@ -338,8 +337,17 @@ private void SetResult(ModuleResult<T> result)
338337
throw new ModuleTimeoutException(this);
339338
}, CancellationToken.None);
340339

340+
var finishedTask = await Task.WhenAny(timeoutExceptionTask, executeAsyncTask);
341+
342+
#if NET8_0_OR_GREATER
343+
await timeoutCancellationTokenSource.CancelAsync();
344+
#else
345+
timeoutCancellationTokenSource.Cancel();
346+
#endif
347+
timeoutCancellationTokenSource.Dispose();
348+
341349
// Will throw a timeout exception if configured and timeout is reached
342-
await await Task.WhenAny(timeoutExceptionTask, executeAsyncTask);
350+
await finishedTask;
343351

344352
return await executeAsyncTask;
345353
}

0 commit comments

Comments
 (0)