Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit e5552e8

Browse files
authored
Removed Timer object from RetryPolicy which was getting leaked. Instead using Task.Delay (#587)
This timer object was never being disposed and was getting leaked. Instead using Task.Delay to achieve the same. Fix for #535
1 parent 435c259 commit e5552e8

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

src/Microsoft.Azure.ServiceBus/RetryPolicy.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace Microsoft.Azure.ServiceBus
55
{
66
using System;
77
using System.Collections.Generic;
8-
using System.Threading;
98
using System.Threading.Tasks;
109
using Primitives;
1110

@@ -22,14 +21,12 @@ public abstract class RetryPolicy
2221
static readonly TimeSpan DefaultRetryMaxBackoff = TimeSpan.FromSeconds(30);
2322

2423
readonly object serverBusyLock = new object();
25-
Timer serverBusyResetTimer;
2624

2725
// This is a volatile copy of IsServerBusy. IsServerBusy is synchronized with a lock, whereas encounteredServerBusy is kept volatile for performance reasons.
2826
volatile bool encounteredServerBusy;
2927

3028
protected RetryPolicy()
3129
{
32-
this.serverBusyResetTimer = new Timer(OnTimerCallback, this, TimeSpan.FromMilliseconds(-1), TimeSpan.FromMilliseconds(-1));
3330
}
3431

3532
/// <summary>
@@ -164,7 +161,7 @@ internal void SetServerBusy(string exceptionMessage)
164161
this.ServerBusyExceptionMessage = string.IsNullOrWhiteSpace(exceptionMessage) ?
165162
Resources.DefaultServerBusyException : exceptionMessage;
166163
this.IsServerBusy = true;
167-
this.serverBusyResetTimer.Change(RetryPolicy.ServerBusyBaseSleepTime, TimeSpan.FromMilliseconds(-1));
164+
TaskExtensionHelper.Schedule(ScheduleResetServerBusy);
168165
}
169166
}
170167
}
@@ -183,17 +180,16 @@ internal void ResetServerBusy()
183180
this.encounteredServerBusy = false;
184181
this.ServerBusyExceptionMessage = Resources.DefaultServerBusyException;
185182
this.IsServerBusy = false;
186-
this.serverBusyResetTimer.Change(TimeSpan.FromMilliseconds(-1), TimeSpan.FromMilliseconds(-1));
187183
}
188184
}
189185
}
190186

191187
protected abstract bool OnShouldRetry(TimeSpan remainingTime, int currentRetryCount, out TimeSpan retryInterval);
192188

193-
static void OnTimerCallback(object state)
189+
private async Task ScheduleResetServerBusy()
194190
{
195-
var thisPtr = (RetryPolicy)state;
196-
thisPtr.ResetServerBusy();
191+
await Task.Delay(RetryPolicy.ServerBusyBaseSleepTime).ConfigureAwait(false);
192+
ResetServerBusy();
197193
}
198194
}
199195
}

0 commit comments

Comments
 (0)