@@ -30,45 +30,49 @@ public async Task Start()
3030 continue ;
3131 }
3232
33- var noLongerTheLeader = new CancellationTokenSource ( ) ;
34- var linkedTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( config . ServiceIsStopping . Token , noLongerTheLeader . Token ) ;
35- try
33+ using ( var noLongerTheLeader = new CancellationTokenSource ( ) )
3634 {
37- var leaseTask = RenewLease ( linkedTokenSource . Token , noLongerTheLeader ) ;
38- var startupTask = config . Startup ( service , linkedTokenSource . Token ) ;
39- var whenAnyTask = await Task . WhenAny ( leaseTask , startupTask ) ;
40-
41- var exceptions = new List < Exception > ( ) ;
42- if ( startupTask . IsFaulted )
35+ using ( var linkedTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( config . ServiceIsStopping . Token , noLongerTheLeader . Token ) )
4336 {
44- config . ServiceIsStopping . Cancel ( ) ;
45- if ( startupTask . Exception != null )
37+ try
4638 {
47- exceptions . Add ( startupTask . Exception . GetBaseException ( ) ) ;
48- }
49- }
39+ var leaseTask = RenewLease ( linkedTokenSource . Token , noLongerTheLeader ) ;
40+ var startupTask = config . Startup ( service , linkedTokenSource . Token ) ;
41+ var whenAnyTask = await Task . WhenAny ( leaseTask , startupTask ) ;
5042
51- if ( leaseTask . IsFaulted )
52- {
53- if ( leaseTask . Exception != null )
43+ var exceptions = new List < Exception > ( ) ;
44+ if ( startupTask . IsFaulted )
45+ {
46+ config . ServiceIsStopping . Cancel ( ) ;
47+ if ( startupTask . Exception != null )
48+ {
49+ exceptions . Add ( startupTask . Exception . GetBaseException ( ) ) ;
50+ }
51+ }
52+
53+ if ( leaseTask . IsFaulted )
54+ {
55+ if ( leaseTask . Exception != null )
56+ {
57+ exceptions . Add ( leaseTask . Exception . GetBaseException ( ) ) ;
58+ }
59+ }
60+
61+ if ( exceptions . Any ( ) )
62+ {
63+ throw new AggregateException ( exceptions ) ;
64+ }
65+
66+ await whenAnyTask ;
67+ }
68+ catch ( TaskCanceledException )
5469 {
55- exceptions . Add ( leaseTask . Exception . GetBaseException ( ) ) ;
70+ }
71+ finally
72+ {
73+ linkedTokenSource . Cancel ( ) ;
5674 }
5775 }
58-
59- if ( exceptions . Any ( ) )
60- {
61- throw new AggregateException ( exceptions ) ;
62- }
63-
64- await whenAnyTask ;
65- }
66- catch ( TaskCanceledException )
67- {
68- }
69- finally
70- {
71- linkedTokenSource . Cancel ( ) ;
7276 }
7377 }
7478 }
0 commit comments