From 2f5b13f4c064b10845a7e9fd122aba67e70139ec Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Mon, 3 Jun 2024 02:18:46 +0200 Subject: [PATCH] [0% tested] fix handling of map render/build timeouts --- .../BuildRunners/LocalBuildService.cs | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/SS14.MapServer/BuildRunners/LocalBuildService.cs b/SS14.MapServer/BuildRunners/LocalBuildService.cs index c025254..727e2b5 100644 --- a/SS14.MapServer/BuildRunners/LocalBuildService.cs +++ b/SS14.MapServer/BuildRunners/LocalBuildService.cs @@ -53,13 +53,17 @@ public async Task Build(string directory, CancellationToken cancellationToken = _log.Information("Started building {ProjectName}", _configuration.MapRendererProjectName); process.Start(); - process.BeginOutputReadLine(); - await process.WaitForExitAsync(cancellationToken).WaitAsync(TimeSpan.FromMinutes(_configuration.ProcessTimeoutMinutes), cancellationToken); - process.CancelOutputRead(); - if (!process.HasExited) + try { - process.Kill(); + process.BeginOutputReadLine(); + await process.WaitForExitAsync(cancellationToken).WaitAsync(TimeSpan.FromMinutes(_configuration.ProcessTimeoutMinutes), cancellationToken); + process.CancelOutputRead(); + } + catch (OperationCanceledException) + { + if (!process.HasExited) + process.Kill(); throw new BuildException($"Building timed out {_configuration.MapRendererProjectName}"); } @@ -87,20 +91,25 @@ public async Task Run(string directory, string command, List arg _log.Information("Running: {Command} {Arguments}", command, string.Join(' ', arguments)); - await Task.Run(() => process.Start(), cancellationToken).WaitAsync(TimeSpan.FromMinutes(1), cancellationToken); + process.Start(); if (process.HasExited) throw new BuildException($"Run timed out {_configuration.MapRendererProjectName}"); - process.BeginErrorReadLine(); - process.BeginOutputReadLine(); - await process.WaitForExitAsync(cancellationToken).WaitAsync(TimeSpan.FromMinutes(_configuration.ProcessTimeoutMinutes), cancellationToken); - process.CancelErrorRead(); - process.CancelOutputRead(); - - if (!process.HasExited) + try { - process.Kill(); + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + await process.WaitForExitAsync(cancellationToken) + .WaitAsync(TimeSpan.FromMinutes(_configuration.ProcessTimeoutMinutes), cancellationToken); + process.CancelErrorRead(); + process.CancelOutputRead(); + } + catch (OperationCanceledException) + { + if (!process.HasExited) + process.Kill(); + throw new BuildException($"Run timed out {_configuration.MapRendererProjectName}"); }