@@ -36,13 +36,18 @@ public abstract class ProtectedFileProvider : IFileProvider, IDisposable
36
36
public const int MaxTries = 30 ;
37
37
static readonly TimeSpan RetryInterval = TimeSpan . FromMilliseconds ( 500 ) ;
38
38
protected Stream _stream ;
39
- object _initializationLock = new object ( ) ;
39
+
40
+ /// <summary>
41
+ /// Use a Mutex to prevent cross-process file I/O
42
+ /// </summary>
43
+ /// <returns></returns>
44
+ private static readonly Mutex _initializationLock = new Mutex ( false , @"Global\AzurePowerShellProtectedFileProviderInit" ) ;
40
45
public string FilePath { get ; set ; }
41
46
42
47
protected IDataStore DataStore { get ; set ; }
43
48
44
49
/// <summary>
45
- ///
50
+ ///
46
51
/// </summary>
47
52
public Stream Stream
48
53
{
@@ -87,18 +92,17 @@ public StreamWriter CreateWriter()
87
92
88
93
protected virtual void InitializeStream ( )
89
94
{
90
- lock ( _initializationLock )
95
+ _initializationLock . WaitOne ( ) ;
96
+ if ( _stream == null )
91
97
{
92
- if ( _stream == null )
98
+ Stream stream ;
99
+ if ( ! TryGetStreamLock ( AcquireLock , FilePath , out stream ) )
93
100
{
94
- Stream stream ;
95
- if ( ! TryGetStreamLock ( AcquireLock , FilePath , out stream ) )
96
- {
97
- throw new UnauthorizedAccessException ( string . Format ( Resources . FileLockFailure , FilePath ) ) ;
98
- }
99
-
100
- _stream = stream ;
101
+ _initializationLock . ReleaseMutex ( ) ;
102
+ throw new UnauthorizedAccessException ( string . Format ( Resources . FileLockFailure , FilePath ) ) ;
101
103
}
104
+ _initializationLock . ReleaseMutex ( ) ;
105
+ _stream = stream ;
102
106
}
103
107
}
104
108
0 commit comments