-
Notifications
You must be signed in to change notification settings - Fork 272
/
OutOfProcMiddleware.cs
710 lines (623 loc) · 33.1 KB
/
OutOfProcMiddleware.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#nullable enable
#if FUNCTIONS_V3_OR_GREATER
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using DurableTask.Core;
using DurableTask.Core.Entities;
using DurableTask.Core.Entities.OperationFormat;
using DurableTask.Core.Exceptions;
using DurableTask.Core.History;
using DurableTask.Core.Middleware;
using Microsoft.Azure.WebJobs.Host.Executors;
using P = Microsoft.DurableTask.Protobuf;
namespace Microsoft.Azure.WebJobs.Extensions.DurableTask
{
internal class OutOfProcMiddleware
{
private readonly DurableTaskExtension extension;
public OutOfProcMiddleware(DurableTaskExtension extension)
{
this.extension = extension;
}
// The below private properties are just passthroughs to properties or methods defined in the extension class.
// They exist simply to make it easier to copy/paste logic from the old middleware defined there to this file.
private DurableTaskOptions Options => this.extension.Options;
private EndToEndTraceHelper TraceHelper => this.extension.TraceHelper;
private ILifeCycleNotificationHelper LifeCycleNotificationHelper => this.extension.LifeCycleNotificationHelper;
private IApplicationLifetimeWrapper HostLifetimeService => this.extension.HostLifetimeService;
/// <summary>
/// Durable Task Framework orchestration middleware that invokes an out-of-process orchestrator function.
/// </summary>
/// <param name="dispatchContext">This middleware context provided by the framework that contains information about the orchestration.</param>
/// <param name="next">The next middleware handler in the pipeline.</param>
/// <exception cref="InvalidOperationException">Thrown if there's an internal failure in the middleware.</exception>
/// <exception cref="SessionAbortedException">Thrown if there is a recoverable error in the Functions runtime that's expected to be handled gracefully.</exception>
public async Task CallOrchestratorAsync(DispatchMiddlewareContext dispatchContext, Func<Task> next)
{
OrchestrationRuntimeState? runtimeState = dispatchContext.GetProperty<OrchestrationRuntimeState>();
if (runtimeState == null)
{
// This should never happen, but it's almost certainly non-retryable if it does.
dispatchContext.SetProperty(OrchestratorExecutionResult.ForFailure(
message: "Orchestration runtime state was missing!",
details: null));
return;
}
OrchestrationInstance? instance = dispatchContext.GetProperty<OrchestrationInstance>();
if (instance == null)
{
// This should never happen, but it's almost certainly non-retryable if it does.
dispatchContext.SetProperty(OrchestratorExecutionResult.ForFailure(
message: "Instance ID metadata was missing!",
details: null));
return;
}
FunctionName functionName = new FunctionName(runtimeState.Name);
RegisteredFunctionInfo? function = this.extension.GetOrchestratorInfo(functionName);
if (function == null)
{
// Fail the orchestration with an error explaining that the function name is invalid.
string errorMessage = this.extension.GetInvalidOrchestratorFunctionMessage(functionName.Name);
dispatchContext.SetProperty(OrchestratorExecutionResult.ForFailure(errorMessage, details: null));
return;
}
ExecutionStartedEvent? startEvent = runtimeState.ExecutionStartedEvent;
if (startEvent == null)
{
// This should never happen, but it's almost certainly non-retriable if it does.
dispatchContext.SetProperty(OrchestratorExecutionResult.ForFailure(
message: "ExecutionStartedEvent was missing from runtime state!",
details: null));
return;
}
TaskOrchestrationEntityParameters? entityParameters = dispatchContext.GetProperty<TaskOrchestrationEntityParameters>();
bool isReplaying = runtimeState.PastEvents.Any();
this.TraceHelper.FunctionStarting(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
isReplaying ? "(replay)" : this.extension.GetIntputOutputTrace(startEvent.Input),
FunctionType.Orchestrator,
isReplaying);
// One-time logging/notifications for when the orchestration first starts.
if (!isReplaying)
{
DurableTaskExtension.TagActivityWithOrchestrationStatus(OrchestrationRuntimeStatus.Running, instance.InstanceId);
await this.LifeCycleNotificationHelper.OrchestratorStartingAsync(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
isReplay: false);
}
var context = new RemoteOrchestratorContext(runtimeState, entityParameters);
var input = new TriggeredFunctionData
{
TriggerValue = context,
#pragma warning disable CS0618 // Type or member is obsolete (not intended for general public use)
InvokeHandler = async functionInvoker =>
{
// Invoke the function and look for a return value. Trigger return values are an undocumented feature that we depend on.
Task invokeTask = functionInvoker();
if (invokeTask is not Task<object> invokeTaskWithResult)
{
// This should never happen
throw new InvalidOperationException("The internal function invoker returned a task that does not support return values!");
}
// The return value is expected to be a JSON string of a well-known schema.
string? triggerReturnValue = (await invokeTaskWithResult) as string;
if (string.IsNullOrEmpty(triggerReturnValue))
{
throw new InvalidOperationException(
"The function invocation resulted in a null response. This means that either the orchestrator function was implemented " +
"incorrectly, the Durable Task language SDK was implemented incorrectly, or that the destination language worker is not " +
"sending the function result back to the host.");
}
byte[] triggerReturnValueBytes = Convert.FromBase64String(triggerReturnValue);
P.OrchestratorResponse response = P.OrchestratorResponse.Parser.ParseFrom(triggerReturnValueBytes);
context.SetResult(
response.Actions.Select(ProtobufUtils.ToOrchestratorAction),
response.CustomStatus);
context.ThrowIfFailed();
},
#pragma warning restore CS0618 // Type or member is obsolete (not intended for general public use)
};
FunctionResult functionResult;
try
{
functionResult = await function.Executor.TryExecuteAsync(
input,
cancellationToken: this.HostLifetimeService.OnStopping);
if (!functionResult.Succeeded)
{
// Shutdown can surface as a completed invocation in a failed state.
// Re-throw so we can abort this invocation.
this.HostLifetimeService.OnStopping.ThrowIfCancellationRequested();
}
}
catch (Exception hostRuntimeException)
{
string reason = this.HostLifetimeService.OnStopping.IsCancellationRequested ?
"The Functions/WebJobs runtime is shutting down!" :
$"Unhandled exception in the Functions/WebJobs runtime: {hostRuntimeException}";
this.TraceHelper.FunctionAborted(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
reason,
functionType: FunctionType.Orchestrator);
// This will abort the current execution and force an durable retry
throw new SessionAbortedException(reason);
}
OrchestratorExecutionResult orchestratorResult;
if (functionResult.Succeeded)
{
orchestratorResult = context.GetResult();
if (context.OrchestratorCompleted)
{
this.TraceHelper.FunctionCompleted(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
this.extension.GetIntputOutputTrace(context.SerializedOutput),
context.ContinuedAsNew,
FunctionType.Orchestrator,
isReplay: false);
DurableTaskExtension.TagActivityWithOrchestrationStatus(
OrchestrationRuntimeStatus.Completed,
instance.InstanceId);
await this.LifeCycleNotificationHelper.OrchestratorCompletedAsync(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
context.ContinuedAsNew,
isReplay: false);
}
else
{
this.TraceHelper.FunctionAwaited(
this.Options.HubName,
functionName.Name,
FunctionType.Orchestrator,
instance.InstanceId,
isReplay: false);
}
}
else if (context.TryGetOrchestrationErrorDetails(out string details))
{
// the function failed because the orchestrator failed.
orchestratorResult = context.GetResult();
this.TraceHelper.FunctionFailed(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
details,
FunctionType.Orchestrator,
isReplay: false);
await this.LifeCycleNotificationHelper.OrchestratorFailedAsync(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
details,
isReplay: false);
}
else
{
// the function failed for some other reason
string exceptionDetails = functionResult.Exception.ToString();
this.TraceHelper.FunctionFailed(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
exceptionDetails,
FunctionType.Orchestrator,
isReplay: false);
await this.LifeCycleNotificationHelper.OrchestratorFailedAsync(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
exceptionDetails,
isReplay: false);
orchestratorResult = OrchestratorExecutionResult.ForFailure(
message: $"Function '{functionName}' failed with an unhandled exception.",
functionResult.Exception);
}
// Send the result of the orchestrator function to the DTFx dispatch pipeline.
// This allows us to bypass the default, in-process execution and process the given results immediately.
dispatchContext.SetProperty(orchestratorResult);
}
/// <summary>
/// Durable Task Framework entity middleware that invokes an out-of-process orchestrator function.
/// </summary>
/// <param name="dispatchContext">This middleware context provided by the framework that contains information about the entity.</param>
/// <param name="next">The next middleware handler in the pipeline.</param>
/// <exception cref="SessionAbortedException">Thrown if there is a recoverable error in the Functions runtime that's expected to be handled gracefully.</exception>
public async Task CallEntityAsync(DispatchMiddlewareContext dispatchContext, Func<Task> next)
{
EntityBatchRequest? batchRequest = dispatchContext.GetProperty<EntityBatchRequest>();
if (batchRequest == null)
{
// This should never happen, and there's no good response we can return if it does.
throw new InvalidOperationException($"An entity was scheduled but no {nameof(EntityBatchRequest)} was found!");
}
if (batchRequest.InstanceId == null)
{
// This should never happen, and there's no good response we can return if it does.
throw new InvalidOperationException($"An entity was scheduled but InstanceId is null!");
}
EntityId entityId = EntityId.GetEntityIdFromSchedulerId(batchRequest.InstanceId);
FunctionName functionName = new FunctionName(entityId.EntityName);
RegisteredFunctionInfo functionInfo = this.extension.GetEntityInfo(functionName);
void SetErrorResult(FailureDetails failureDetails)
{
// Returns a result with no operation results and no state change,
// and with failure details that explain what error was encountered.
dispatchContext.SetProperty(new EntityBatchResult()
{
Actions = new List<OperationAction>(),
Results = new List<OperationResult>(),
EntityState = batchRequest!.EntityState,
FailureDetails = failureDetails,
});
}
if (functionInfo == null)
{
SetErrorResult(new FailureDetails(
errorType: "EntityFunctionNotFound",
errorMessage: this.extension.GetInvalidEntityFunctionMessage(functionName.Name),
stackTrace: null,
innerFailure: null,
isNonRetriable: true));
return;
}
this.TraceHelper.FunctionStarting(
this.Options.HubName,
functionName.Name,
batchRequest.InstanceId,
this.extension.GetIntputOutputTrace(batchRequest.EntityState),
functionType: FunctionType.Entity,
isReplay: false);
var context = new RemoteEntityContext(batchRequest);
var input = new TriggeredFunctionData
{
TriggerValue = context,
#pragma warning disable CS0618 // Type or member is obsolete (not intended for general public use)
InvokeHandler = async functionInvoker =>
{
// Invoke the function and look for a return value. Trigger return values are an undocumented feature that we depend on.
Task invokeTask = functionInvoker();
if (invokeTask is not Task<object> invokeTaskWithResult)
{
// This should never happen
throw new InvalidOperationException("The internal function invoker returned a task that does not support return values!");
}
// The return value is expected to be a base64 string containing the protobuf-encoding of the batch result.
string? triggerReturnValue = (await invokeTaskWithResult) as string;
if (string.IsNullOrEmpty(triggerReturnValue))
{
throw new InvalidOperationException(
"The function invocation resulted in a null response. This means that either the entity function was implemented " +
"incorrectly, the Durable Task language SDK was implemented incorrectly, or that the destination language worker is not " +
"sending the function result back to the host.");
}
byte[] triggerReturnValueBytes = Convert.FromBase64String(triggerReturnValue);
P.EntityBatchResult response = P.EntityBatchResult.Parser.ParseFrom(triggerReturnValueBytes);
context.Result = response.ToEntityBatchResult();
context.ThrowIfFailed();
#pragma warning restore CS0618 // Type or member is obsolete (not intended for general public use)
},
};
FunctionResult functionResult;
try
{
functionResult = await functionInfo.Executor.TryExecuteAsync(
input,
cancellationToken: this.HostLifetimeService.OnStopping);
if (!functionResult.Succeeded)
{
// Shutdown can surface as a completed invocation in a failed state.
// Re-throw so we can abort this invocation.
this.HostLifetimeService.OnStopping.ThrowIfCancellationRequested();
}
}
catch (Exception hostRuntimeException)
{
string reason = this.HostLifetimeService.OnStopping.IsCancellationRequested ?
"The Functions/WebJobs runtime is shutting down!" :
$"Unhandled exception in the Functions/WebJobs runtime: {hostRuntimeException}";
this.TraceHelper.FunctionAborted(
this.Options.HubName,
functionName.Name,
batchRequest.InstanceId,
reason,
functionType: FunctionType.Entity);
// This will abort the current execution and force an durable retry
throw new SessionAbortedException(reason);
}
if (!functionResult.Succeeded)
{
this.TraceHelper.FunctionFailed(
this.Options.HubName,
functionName.Name,
batchRequest.InstanceId,
functionResult.Exception.ToString(),
FunctionType.Entity,
isReplay: false);
if (context.Result != null)
{
// Send the results of the entity batch execution back to the DTFx dispatch pipeline.
// This is important so we can propagate the individual failure details of each failed operation back to the
// calling orchestrator. Also, even though the function execution was reported as a failure,
// it may not be a "total failure", i.e. some of the operations in the batch may have succeeded and updated
// the entity state.
dispatchContext.SetProperty(context.Result);
}
else
{
SetErrorResult(new FailureDetails(
errorType: "FunctionInvocationFailed",
errorMessage: $"Invocation of function '{functionName}' failed with an exception.",
stackTrace: null,
innerFailure: new FailureDetails(functionResult.Exception),
isNonRetriable: true));
}
return;
}
EntityBatchResult batchResult = context.Result
?? throw new InvalidOperationException($"The entity function executed successfully but {nameof(context.Result)} is still null!");
this.TraceHelper.FunctionCompleted(
this.Options.HubName,
functionName.Name,
batchRequest.InstanceId,
this.extension.GetIntputOutputTrace(batchRequest.EntityState),
batchResult.EntityState != null,
FunctionType.Entity,
isReplay: false);
// Send the results of the entity batch execution back to the DTFx dispatch pipeline.
dispatchContext.SetProperty(batchResult);
}
/// <summary>
/// Durable Task Framework activity middleware that invokes an out-of-process orchestrator function.
/// </summary>
/// <param name="dispatchContext">This middleware context provided by the framework that contains information about the activity.</param>
/// <param name="next">The next middleware handler in the pipeline.</param>
/// <exception cref="SessionAbortedException">Thrown if there is a recoverable error in the Functions runtime that's expected to be handled gracefully.</exception>
public async Task CallActivityAsync(DispatchMiddlewareContext dispatchContext, Func<Task> next)
{
TaskScheduledEvent? scheduledEvent = dispatchContext.GetProperty<TaskScheduledEvent>();
if (scheduledEvent == null)
{
// This should never happen, and there's no good response we can return if it does.
throw new InvalidOperationException($"An activity was scheduled but no {nameof(TaskScheduledEvent)} was found!");
}
if (scheduledEvent.Name?.StartsWith("BuiltIn::", StringComparison.OrdinalIgnoreCase) ?? false)
{
await next();
return;
}
FunctionName functionName = new FunctionName(scheduledEvent.Name);
OrchestrationInstance? instance = dispatchContext.GetProperty<OrchestrationInstance>();
if (instance == null)
{
// This should never happen, but it's almost certainly non-retriable if it does.
dispatchContext.SetProperty(new ActivityExecutionResult
{
ResponseEvent = new TaskFailedEvent(
eventId: -1,
taskScheduledId: scheduledEvent.EventId,
reason: $"Function {functionName} could not execute because instance ID metadata was missing!",
details: null),
});
return;
}
if (!this.extension.TryGetActivityInfo(functionName, out RegisteredFunctionInfo? function))
{
// Fail the activity call with an error explaining that the function name is invalid.
string errorMessage = this.extension.GetInvalidActivityFunctionMessage(functionName.Name);
dispatchContext.SetProperty(new ActivityExecutionResult
{
ResponseEvent = new TaskFailedEvent(
eventId: -1,
taskScheduledId: scheduledEvent.EventId,
reason: errorMessage,
details: null),
});
return;
}
string? rawInput = scheduledEvent.Input;
this.TraceHelper.FunctionStarting(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
this.extension.GetIntputOutputTrace(rawInput),
functionType: FunctionType.Activity,
isReplay: false,
taskEventId: scheduledEvent.EventId);
var inputContext = new DurableActivityContext(this.extension, instance.InstanceId, rawInput, functionName.Name);
var triggerInput = new TriggeredFunctionData { TriggerValue = inputContext };
FunctionResult result;
try
{
result = await function.Executor.TryExecuteAsync(
triggerInput,
cancellationToken: this.HostLifetimeService.OnStopping);
if (!result.Succeeded)
{
// Shutdown can surface as a completed invocation in a failed state.
// Re-throw so we can abort this invocation.
this.HostLifetimeService.OnStopping.ThrowIfCancellationRequested();
}
}
catch (Exception hostRuntimeException)
{
string reason = this.HostLifetimeService.OnStopping.IsCancellationRequested ?
"The Functions/WebJobs runtime is shutting down!" :
$"Unhandled exception in the Functions/WebJobs runtime: {hostRuntimeException}";
this.TraceHelper.FunctionAborted(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
reason,
functionType: FunctionType.Activity);
// This will abort the current execution and force an durable retry
throw new SessionAbortedException(reason);
}
ActivityExecutionResult activityResult;
if (result.Succeeded)
{
string? serializedOutput = inputContext.GetSerializedOutput();
this.TraceHelper.FunctionCompleted(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
this.extension.GetIntputOutputTrace(serializedOutput),
continuedAsNew: false,
FunctionType.Activity,
isReplay: false,
scheduledEvent.EventId);
activityResult = new ActivityExecutionResult
{
ResponseEvent = new TaskCompletedEvent(
eventId: -1,
taskScheduledId: scheduledEvent.EventId,
result: serializedOutput),
};
}
else
{
this.TraceHelper.FunctionFailed(
this.Options.HubName,
functionName.Name,
instance.InstanceId,
result.Exception.ToString(),
FunctionType.Activity,
isReplay: false,
scheduledEvent.EventId);
activityResult = new ActivityExecutionResult
{
ResponseEvent = new TaskFailedEvent(
eventId: -1,
taskScheduledId: scheduledEvent.EventId,
reason: $"Function '{functionName}' failed with an unhandled exception.",
details: null,
GetFailureDetails(result.Exception)),
};
}
// Send the result of the activity function to the DTFx dispatch pipeline.
// This allows us to bypass the default, in-process execution and process the given results immediately.
dispatchContext.SetProperty(activityResult);
}
private static FailureDetails GetFailureDetails(Exception e)
{
if (e.InnerException != null && e.InnerException.Message.StartsWith("Result:"))
{
Exception rpcException = e.InnerException;
if (TryGetRpcExceptionFields(rpcException.Message, out string? exception, out string? stackTrace))
{
if (TrySplitExceptionTypeFromMessage(exception, out string? exceptionType, out string? exceptionMessage))
{
return new FailureDetails(exceptionType, exceptionMessage, stackTrace, innerFailure: null, isNonRetriable: false);
}
return new FailureDetails("(unknown)", exception, stackTrace, innerFailure: null, isNonRetriable: false);
}
else
{
// Don't recognize this message format - return the failure as-is
return new FailureDetails(rpcException);
}
}
// Don't recognize this exception - return it as-is
return new FailureDetails(e);
}
private static bool TryGetRpcExceptionFields(
string rpcExceptionMessage,
[NotNullWhen(true)] out string? exception,
out string? stackTrace)
{
exception = null;
stackTrace = null;
// The message string is formatted as follows:
// Result: {result}\nException: {message}\nStack: {stack}
const string ExceptionDelimeter = "\nException: ";
const string StackDelimeter = "\nStack: ";
ReadOnlySpan<char> messageSpan = rpcExceptionMessage.AsSpan();
int startException = messageSpan.IndexOf(ExceptionDelimeter.AsSpan());
if (startException == -1)
{
// Couldn't find the exception payload - give up
return false;
}
int startExceptionPayload = startException + ExceptionDelimeter.Length;
ReadOnlySpan<char> exceptionPayloadStartSpan = messageSpan[startExceptionPayload..];
int endException = exceptionPayloadStartSpan.LastIndexOf(StackDelimeter.AsSpan());
if (endException >= 0)
{
exception = new string(exceptionPayloadStartSpan[..endException].ToArray());
// Start looking for the stack trace payload immediately after the exception payload
int startStack = endException + StackDelimeter.Length;
stackTrace = new string(exceptionPayloadStartSpan[startStack..].ToArray());
}
else
{
// [Not expected] Couldn't find the stack trace for whatever reason. Just take the rest of the payload as the stack trace.
exception = new string(exceptionPayloadStartSpan.ToArray());
}
return true;
}
private static bool TrySplitExceptionTypeFromMessage(
string exception,
[NotNullWhen(true)] out string? exceptionType,
[NotNullWhen(true)] out string? exceptionMessage)
{
// In certain situations, like when the .NET Isolated worker is configured with
// WorkerOptions.EnableUserCodeException = true, the exception message we get from the .NET Isolated
// worker looks like this:
// "Exception of type 'ExceptionSerialization.Function+UnknownException' was thrown."
const string startMarker = "Exception of type '";
const string endMarker = "' was thrown.";
if (exception.StartsWith(startMarker) && exception.EndsWith(endMarker))
{
exceptionType = exception[startMarker.Length..^endMarker.Length];
exceptionMessage = string.Empty;
return true;
}
// The following are the more common cases that we expect to see, which will be common across a
// variety of language workers:
// .NET : System.ApplicationException: Kah-BOOOOM!!
// Java : SQLServerException: Invalid column name 'status'.
// Python : ResourceNotFoundError: The specified blob does not exist. RequestId:8d5a2c9b-b01e-006f-33df-3f7a2e000000 Time:2022-03-25T00:31:24.2003327Z ErrorCode:BlobNotFound Error:None
// Node : SyntaxError: Unexpected token N in JSON at position 12768
// From the above examples, they all follow the same pattern of {ExceptionType}: {Message}.
// However, some exception types override the ToString() method and do something custom, in which
// case the message may not be in the expected format. In such cases we won't be able to distinguish
// the exception type.
string delimeter = ": ";
int endExceptionType = exception.IndexOf(delimeter);
if (endExceptionType < 0)
{
exceptionType = null;
exceptionMessage = exception;
return false;
}
exceptionType = exception[..endExceptionType].TrimEnd();
// The .NET Isolated language worker strangely includes the stack trace in the exception message.
// To avoid bloating the payload with redundant information, we only consider the first line.
int startMessage = endExceptionType + delimeter.Length;
int endMessage = exception.IndexOf('\n', startMessage);
if (endMessage < 0)
{
exceptionMessage = exception[startMessage..].TrimEnd();
}
else
{
exceptionMessage = exception[startMessage..endMessage].TrimEnd();
}
return true;
}
}
}
#endif