Skip to content

Commit 633a06c

Browse files
fix(security): replace 2 generic catches in AgentExecutorService.cs and Worker.cs
- AgentExecutorService ExecuteAsync: added InvalidOperationException, TimeoutException handlers - Worker SendHeartbeatAsync: added HttpRequestException, TaskCanceledException, InvalidOperationException handlers Refs: E7-T1 (38/39 catches fixed)
1 parent f0a4ff2 commit 633a06c

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/Node.Runtime/Services/AgentExecutorService.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,50 @@ public async Task<AgentExecutionResult> ExecuteAsync(
268268
"Agent execution timed out after {DurationMs}ms",
269269
stopwatch.ElapsedMilliseconds);
270270
}
271+
catch (InvalidOperationException ex)
272+
{
273+
stopwatch.Stop();
274+
result.Success = false;
275+
result.Error = ex.Message;
276+
result.Duration = stopwatch.Elapsed;
277+
278+
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
279+
activity?.AddEvent(new ActivityEvent("exception",
280+
tags: new ActivityTagsCollection
281+
{
282+
{ "exception.type", ex.GetType().FullName },
283+
{ "exception.message", ex.Message }
284+
}));
285+
activity?.SetTag("error.type", "invalid_operation");
286+
287+
TelemetryConfig.AgentExecutionErrorsCounter.Add(1,
288+
new KeyValuePair<string, object?>("agent.id", spec.AgentId),
289+
new KeyValuePair<string, object?>("error.type", "invalid_operation"));
290+
291+
_logger.LogError(ex, "Invalid operation during agent execution after {DurationMs}ms", stopwatch.ElapsedMilliseconds);
292+
}
293+
catch (TimeoutException ex)
294+
{
295+
stopwatch.Stop();
296+
result.Success = false;
297+
result.Error = ex.Message;
298+
result.Duration = stopwatch.Elapsed;
299+
300+
activity?.SetStatus(ActivityStatusCode.Error, ex.Message);
301+
activity?.AddEvent(new ActivityEvent("exception",
302+
tags: new ActivityTagsCollection
303+
{
304+
{ "exception.type", ex.GetType().FullName },
305+
{ "exception.message", ex.Message }
306+
}));
307+
activity?.SetTag("error.type", "timeout");
308+
309+
TelemetryConfig.AgentExecutionErrorsCounter.Add(1,
310+
new KeyValuePair<string, object?>("agent.id", spec.AgentId),
311+
new KeyValuePair<string, object?>("error.type", "timeout"));
312+
313+
_logger.LogError(ex, "Agent execution timeout after {DurationMs}ms", stopwatch.ElapsedMilliseconds);
314+
}
271315
catch (Exception ex)
272316
{
273317
stopwatch.Stop();
@@ -288,7 +332,7 @@ public async Task<AgentExecutionResult> ExecuteAsync(
288332
new KeyValuePair<string, object?>("agent.id", spec.AgentId),
289333
new KeyValuePair<string, object?>("error.type", ex.GetType().Name));
290334

291-
_logger.LogError(ex, "Agent execution failed after {DurationMs}ms", stopwatch.ElapsedMilliseconds);
335+
_logger.LogError(ex, "Unexpected agent execution failure after {DurationMs}ms", stopwatch.ElapsedMilliseconds);
292336
}
293337

294338
return result;

src/Node.Runtime/Worker.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,21 @@ private async Task SendHeartbeatAsync()
9696

9797
await _registrationService.SendHeartbeatAsync(activeRuns, availableSlots);
9898
}
99+
catch (HttpRequestException ex)
100+
{
101+
_logger.LogWarning(ex, "HTTP error sending heartbeat for node {NodeId}", _options.NodeId);
102+
}
103+
catch (TaskCanceledException ex)
104+
{
105+
_logger.LogWarning(ex, "Timeout sending heartbeat for node {NodeId}", _options.NodeId);
106+
}
107+
catch (InvalidOperationException ex)
108+
{
109+
_logger.LogError(ex, "Invalid operation sending heartbeat for node {NodeId}", _options.NodeId);
110+
}
99111
catch (Exception ex)
100112
{
101-
_logger.LogError(ex, "Error sending heartbeat for node {NodeId}", _options.NodeId);
113+
_logger.LogError(ex, "Unexpected error sending heartbeat for node {NodeId}", _options.NodeId);
102114
}
103115
}
104116
}

0 commit comments

Comments
 (0)