@@ -412,6 +412,24 @@ public async Task<ReadTableReponse> ReadAndWriteTableAsync(bool isShuttingDown,
412
412
try
413
413
{
414
414
await this . partitionTable . ReplaceEntityAsync ( partition , etag , forcefulShutdownToken ) ;
415
+
416
+ // Ensure worker is listening to the control queue iff either:
417
+ // 1) worker just claimed the lease,
418
+ // 2) worker was already the owner in the partitions table and is not actively draining the queue.
419
+ // Note that during draining, we renew the lease but do not want to listen to new messages.
420
+ // Otherwise, we'll never finish draining our in-memory messages.
421
+ // When drain completes, and the worker may decide to release the lease. In that moment,
422
+ // IsDrainingPartition can still be true but renewedLease is false — without checking
423
+ // !releasedLease, the worker could incorrectly resume listening just before releasing the lease.
424
+ bool isRenewingToDrainQueue = renewedLease && response . IsDrainingPartition && ! releasedLease ;
425
+ if ( claimedLease || ! isRenewingToDrainQueue )
426
+ {
427
+ // Notify the orchestration session manager that we acquired a lease for one of the partitions.
428
+ // This will cause it to start reading control queue messages for that partition.
429
+ await this . service . OnTableLeaseAcquiredAsync ( partition ) ;
430
+ }
431
+
432
+ this . LogHelper ( partition , claimedLease , stoleLease , renewedLease , drainedLease , releasedLease , previousOwner ) ;
415
433
}
416
434
catch ( DurableTaskStorageException ex ) when ( ex . HttpStatusCode == ( int ) HttpStatusCode . PreconditionFailed )
417
435
{
@@ -423,19 +441,6 @@ public async Task<ReadTableReponse> ReadAndWriteTableAsync(bool isShuttingDown,
423
441
$ "Failed to update table entry due to an Etag mismatch. Failed ETag value: '{ etag } '.") ;
424
442
throw ;
425
443
}
426
-
427
- // Ensure worker is listening to the control queue iff either:
428
- // 1) worker just claimed the lease,
429
- // 2) worker was already the owner in the partitions table and is not actively draining the queue. Note that during draining, we renew the lease but do not want to listen to new messages. Otherwise, we'll never finish draining our in-memory messages.
430
- bool isRenewingToDrainQueue = renewedLease & response . IsDrainingPartition ;
431
- if ( claimedLease || ! isRenewingToDrainQueue )
432
- {
433
- // Notify the orchestration session manager that we acquired a lease for one of the partitions.
434
- // This will cause it to start reading control queue messages for that partition.
435
- await this . service . OnTableLeaseAcquiredAsync ( partition ) ;
436
- }
437
-
438
- this . LogHelper ( partition , claimedLease , stoleLease , renewedLease , drainedLease , releasedLease , previousOwner ) ;
439
444
}
440
445
}
441
446
@@ -505,7 +510,8 @@ void RenewOrReleaseMyLease(
505
510
partition ,
506
511
ref releasedLease ,
507
512
ref renewedLease ,
508
- ref drainedLease ) ;
513
+ ref drainedLease ,
514
+ CloseReason . LeaseLost ) ;
509
515
}
510
516
}
511
517
@@ -583,7 +589,8 @@ void TryDrainAndReleaseAllPartitions(
583
589
partition ,
584
590
ref releasedLease ,
585
591
ref renewedLease ,
586
- ref drainedLease ) ;
592
+ ref drainedLease ,
593
+ CloseReason . Shutdown ) ;
587
594
588
595
if ( releasedLease )
589
596
{
@@ -661,7 +668,7 @@ await this.partitionTable.ReplaceEntityAsync(
661
668
partition ,
662
669
etag ,
663
670
forceShutdownToken ) ;
664
-
671
+
665
672
this . settings . Logger . LeaseStealingSucceeded (
666
673
this . storageAccountName ,
667
674
this . settings . TaskHubName ,
@@ -815,7 +822,8 @@ void CheckDrainTask(
815
822
TablePartitionLease partition ,
816
823
ref bool releasedLease ,
817
824
ref bool renewedLease ,
818
- ref bool drainedLease )
825
+ ref bool drainedLease ,
826
+ CloseReason reason )
819
827
{
820
828
// Check if drain process has started.
821
829
if ( this . backgroundDrainTasks . TryGetValue ( partition . RowKey ! , out Task ? drainTask ) )
@@ -844,7 +852,7 @@ void CheckDrainTask(
844
852
}
845
853
else // If drain task hasn't been started yet, start it and keep renewing the lease to prevent it from expiring.
846
854
{
847
- this . DrainPartition ( partition , CloseReason . Shutdown ) ;
855
+ this . DrainPartition ( partition , reason ) ;
848
856
this . RenewLease ( partition ) ;
849
857
renewedLease = true ;
850
858
drainedLease = true ;
0 commit comments