1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . Linq ;
4
5
using System . Runtime . CompilerServices ;
5
6
using System . Runtime . InteropServices ;
@@ -246,7 +247,7 @@ private class PlacementWorker
246
247
#pragma warning restore IDE0052 // Remove unread private members
247
248
private readonly object _lockObj = new ( ) ;
248
249
private readonly PlacementService _placementService ;
249
- private List < ( Message Message , TaskCompletionSource < bool > Completion ) > _messages = new ( ) ;
250
+ private List < ( Message Message , TaskCompletionSource Completion ) > _messages = new ( ) ;
250
251
251
252
public PlacementWorker ( PlacementService placementService )
252
253
{
@@ -259,7 +260,7 @@ public PlacementWorker(PlacementService placementService)
259
260
260
261
public Task AddressMessage ( Message message )
261
262
{
262
- var completion = new TaskCompletionSource < bool > ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
263
+ var completion = new TaskCompletionSource ( TaskCreationOptions . RunContinuationsAsynchronously ) ;
263
264
264
265
lock ( _lockObj )
265
266
{
@@ -271,7 +272,7 @@ public Task AddressMessage(Message message)
271
272
return completion . Task ;
272
273
}
273
274
274
- private List < ( Message Message , TaskCompletionSource < bool > Completion ) > GetMessages ( )
275
+ private List < ( Message Message , TaskCompletionSource Completion ) > GetMessages ( )
275
276
{
276
277
lock ( _lockObj )
277
278
{
@@ -347,47 +348,32 @@ private void AddressWaitingMessages(GrainPlacementWorkItem completedWorkItem)
347
348
var resultTask = completedWorkItem . Result ;
348
349
var messages = completedWorkItem . Messages ;
349
350
350
- if ( resultTask . IsCompletedSuccessfully )
351
+ try
351
352
{
353
+ var siloAddress = resultTask . Result ;
352
354
foreach ( var message in messages )
353
355
{
354
- var siloAddress = resultTask . Result ;
355
356
_placementService . SetMessageTargetPlacement ( message . Message , siloAddress ) ;
356
- message . Completion . TrySetResult ( true ) ;
357
+ message . Completion . TrySetResult ( ) ;
357
358
}
358
359
}
359
- else if ( resultTask . IsCanceled )
360
+ catch ( Exception exception )
360
361
{
361
- foreach ( var message in messages )
362
+ var originalException = exception switch
362
363
{
363
- message . Completion . TrySetCanceled ( ) ;
364
- }
365
- }
366
- else if ( resultTask . IsFaulted )
367
- {
364
+ AggregateException ae when ae . InnerExceptions . Count == 1 => ae . InnerException ,
365
+ _ => exception ,
366
+ } ;
367
+
368
368
foreach ( var message in messages )
369
369
{
370
- message . Completion . TrySetException ( OriginalException ( resultTask . Exception ) ) ;
370
+ message . Completion . TrySetException ( originalException ) ;
371
371
}
372
372
}
373
373
374
374
messages . Clear ( ) ;
375
375
}
376
376
377
- private static Exception OriginalException ( AggregateException exception )
378
- {
379
- if ( exception is null )
380
- {
381
- // Due to race conditions, it is possible to observe IsFaulted = true, but still Exception = null.
382
- // This is because the state transition to Faulted might have occurred just after the Exception
383
- // property check, but before the internal exception was retrieved.
384
-
385
- return new Exception ( "Task faulted without an exception." ) ;
386
- }
387
-
388
- return exception . InnerExceptions . Count == 1 ? exception . InnerException : exception ;
389
- }
390
-
391
377
private async Task < SiloAddress > GetOrPlaceActivationAsync ( Message firstMessage )
392
378
{
393
379
await Task . Yield ( ) ;
@@ -421,7 +407,7 @@ private async Task<SiloAddress> GetOrPlaceActivationAsync(Message firstMessage)
421
407
422
408
private class GrainPlacementWorkItem
423
409
{
424
- public List < ( Message Message , TaskCompletionSource < bool > Completion ) > Messages { get ; } = new ( ) ;
410
+ public List < ( Message Message , TaskCompletionSource Completion ) > Messages { get ; } = new ( ) ;
425
411
426
412
public Task < SiloAddress > Result { get ; set ; }
427
413
}
0 commit comments