Skip to content

Commit 02b4afb

Browse files
authored
catching exceptions due to race with worker process exit (#8917)
1 parent c8e6c9d commit 02b4afb

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/WebJobs.Script/Workers/ProcessManagement/WorkerProcess.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
using Microsoft.Azure.WebJobs.Logging;
1212
using Microsoft.Azure.WebJobs.Script.Diagnostics;
1313
using Microsoft.Azure.WebJobs.Script.Eventing;
14-
using Microsoft.Azure.WebJobs.Script.Workers.Rpc;
1514
using Microsoft.Extensions.DependencyInjection;
1615
using Microsoft.Extensions.Logging;
1716

@@ -140,12 +139,13 @@ private void LogError(string msg)
140139

141140
private void OnProcessExited(object sender, EventArgs e)
142141
{
142+
_workerProcessLogger.LogDebug("Process {processId} has exited with code {exitCode}.", Process?.Id, Process?.ExitCode);
143+
143144
if (Disposing)
144145
{
145146
// No action needed
146147
return;
147148
}
148-
string exceptionMessage = string.Join(",", _processStdErrDataQueue.Where(s => !string.IsNullOrEmpty(s)));
149149

150150
try
151151
{
@@ -160,6 +160,7 @@ private void OnProcessExited(object sender, EventArgs e)
160160
}
161161
else
162162
{
163+
string exceptionMessage = string.Join(",", _processStdErrDataQueue.Where(s => !string.IsNullOrEmpty(s)));
163164
var processExitEx = new WorkerProcessExitException($"{Process.StartInfo.FileName} exited with code {Process.ExitCode} (0x{Process.ExitCode.ToString("X")})", new Exception(exceptionMessage));
164165
processExitEx.ExitCode = Process.ExitCode;
165166
processExitEx.Pid = Process.Id;
@@ -168,7 +169,7 @@ private void OnProcessExited(object sender, EventArgs e)
168169
}
169170
catch (Exception exc)
170171
{
171-
_workerProcessLogger?.LogDebug(exc, "Exception on worker process exit.");
172+
_workerProcessLogger?.LogDebug(exc, "Exception on worker process exit. Process id: {processId}", Process?.Id);
172173
// ignore process is already disposed
173174
}
174175
finally
@@ -208,7 +209,17 @@ internal void BuildAndLogConsoleLog(string msg, LogLevel level)
208209

209210
public void WaitForProcessExitInMilliSeconds(int waitTime)
210211
{
211-
Process.WaitForExit(waitTime);
212+
try
213+
{
214+
if (!Process.HasExited)
215+
{
216+
Process.WaitForExit(waitTime);
217+
}
218+
}
219+
catch (Exception ex)
220+
{
221+
_workerProcessLogger.LogDebug(ex, "An exception was thrown while waiting for process {processId} to exit. It is possible that the process had already exited and this can be ignored.", Process?.Id);
222+
}
212223
}
213224

214225
public void Dispose()
@@ -229,7 +240,7 @@ public void Dispose()
229240
Process.Kill();
230241
if (!Process.WaitForExit(processExitTimeoutInMilliseconds))
231242
{
232-
_workerProcessLogger.LogWarning($"Worker process has not exited despite waiting for {processExitTimeoutInMilliseconds} ms");
243+
_workerProcessLogger.LogWarning("Worker process {processId} has not exited despite waiting for {processExitTimeoutInMilliseconds} ms", Process?.Id, processExitTimeoutInMilliseconds);
233244
}
234245
}
235246
Process.Dispose();

0 commit comments

Comments
 (0)