Is this thread safe? RetryPolicy<StorageTransientErrorDetectionStrategy>

Topics: Data Access Application Block, Enterprise Library Core, Exception Handling Application Block
Apr 23, 2012 at 8:59 PM


I was wondering if the Incremental and RetryPolicy<> classes are thread safe.  Can I declare them statically like in the below example, and use them throughout all of my data layer methods?




using Microsoft.Practices.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.SqlAzure;
using Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.AzureStorage;

namespace MyNamespace
    public static class MyDataLayer
        private static Incremental retryStrategy = new Incremental(10, TimeSpan.FromSeconds(.5),TimeSpan.FromSeconds(1));
        private static RetryPolicy<StorageTransientErrorDetectionStrategy> retryPolicy = new RetryPolicy<StorageTransientErrorDetectionStrategy>(retryStrategy);

Apr 23, 2012 at 10:44 PM
Edited Apr 25, 2012 at 3:45 AM

The Incremental class is thread safe.

However, RetryPolicy<T> is not thread safe.  The reason is that the retrying delegate is publicly exposed:

public event EventHandler<RetryingEventArgs> Retrying;

So it is possible to have a race condition when/if multiple threads are modifying the delegate.  Some good reading on that topic is Eric Lippert's Events and Races.  Another consideration would be what the delegate code is doing and whether or not that is thread safe.

It appears it should be possible to use RetryPolicy in a thread safe manner but that is not guaranteed and will depend on the specific implementation.

Randy Levy
Enterprise Library support engineer 

Aug 24, 2012 at 10:32 AM

In here (thread safety section) it states that Any public static  members of RetryPolicy are thread safe. Any instance members are not guaranteed to be thread safe.