Skip to content

Commit 294f8db

Browse files
committed
Use "Local" mutex; put ReleaseMutex() in finally
1 parent 7156b7f commit 294f8db

File tree

2 files changed

+57
-34
lines changed

2 files changed

+57
-34
lines changed

src/Accounts/Authentication.ResourceManager/ProtectedFileProvider.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public abstract class ProtectedFileProvider : IFileProvider, IDisposable
4141
/// Use a Mutex to prevent cross-process file I/O
4242
/// </summary>
4343
/// <returns></returns>
44-
private static readonly Mutex _initializationLock = new Mutex(false, @"Global\AzurePowerShellProtectedFileProviderInit");
44+
private static readonly Mutex _initializationLock = new Mutex(false, @"Local\AzurePowerShellProtectedFileProviderInit");
4545
public string FilePath { get; set; }
4646

4747
protected IDataStore DataStore { get; set; }
@@ -93,16 +93,21 @@ public StreamWriter CreateWriter()
9393
protected virtual void InitializeStream()
9494
{
9595
_initializationLock.WaitOne();
96-
if (_stream == null)
96+
try
9797
{
98-
Stream stream;
99-
if (!TryGetStreamLock(AcquireLock, FilePath, out stream))
98+
if (_stream == null)
10099
{
101-
_initializationLock.ReleaseMutex();
102-
throw new UnauthorizedAccessException(string.Format(Resources.FileLockFailure, FilePath));
100+
Stream stream;
101+
if (!TryGetStreamLock(AcquireLock, FilePath, out stream))
102+
{
103+
throw new UnauthorizedAccessException(string.Format(Resources.FileLockFailure, FilePath));
104+
}
105+
_stream = stream;
103106
}
107+
}
108+
finally
109+
{
104110
_initializationLock.ReleaseMutex();
105-
_stream = stream;
106111
}
107112
}
108113

src/Accounts/Authentication/Authentication/ProtectedFileTokenCache.cs

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class ProtectedFileTokenCache : TokenCache, IAzureTokenCache
4646
/// <summary>
4747
/// A mutex to prevent IO to token cache file across threads / processes.
4848
/// </summary>
49-
private static readonly Mutex fileLock = new Mutex(false, @"Global\AzurePowerShellAdalTokenCacheFile");
49+
private static readonly Mutex fileLock = new Mutex(false, @"Local\AzurePowerShellAdalTokenCacheFile");
5050

5151
private static readonly Lazy<ProtectedFileTokenCache> instance = new Lazy<ProtectedFileTokenCache>(() => new ProtectedFileTokenCache());
5252

@@ -134,26 +134,32 @@ private void ReadFileIntoCache(string cacheFileName = null)
134134
}
135135

136136
fileLock.WaitOne();
137-
if (_store.FileExists(cacheFileName))
137+
try
138138
{
139-
var existingData = _store.ReadFileAsBytes(cacheFileName);
140-
if (existingData != null)
139+
if (_store.FileExists(cacheFileName))
141140
{
142-
#if !NETSTANDARD
143-
try
144-
{
145-
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
146-
}
147-
catch (CryptographicException)
141+
var existingData = _store.ReadFileAsBytes(cacheFileName);
142+
if (existingData != null)
148143
{
149-
_store.DeleteFile(cacheFileName);
150-
}
144+
#if !NETSTANDARD
145+
try
146+
{
147+
Deserialize(ProtectedData.Unprotect(existingData, null, DataProtectionScope.CurrentUser));
148+
}
149+
catch (CryptographicException)
150+
{
151+
_store.DeleteFile(cacheFileName);
152+
}
151153
#else
152-
Deserialize(existingData);
154+
Deserialize(existingData);
153155
#endif
156+
}
154157
}
155158
}
156-
fileLock.ReleaseMutex();
159+
finally
160+
{
161+
fileLock.ReleaseMutex();
162+
}
157163
}
158164

159165
private void WriteCacheIntoFile(string cacheFileName = null)
@@ -170,22 +176,30 @@ private void WriteCacheIntoFile(string cacheFileName = null)
170176
#endif
171177

172178
fileLock.WaitOne();
173-
if (HasStateChanged)
179+
try
174180
{
175-
_store.WriteFile(cacheFileName, dataToWrite);
176-
HasStateChanged = false;
181+
if (HasStateChanged)
182+
{
183+
_store.WriteFile(cacheFileName, dataToWrite);
184+
HasStateChanged = false;
185+
}
186+
}
187+
finally
188+
{
189+
fileLock.ReleaseMutex();
177190
}
178-
fileLock.ReleaseMutex();
179191
}
180192

181193
private void EnsureCacheFile(string cacheFileName = null)
182194
{
183195
fileLock.WaitOne();
184-
if (_store.FileExists(cacheFileName))
196+
try
185197
{
186-
var existingData = _store.ReadFileAsBytes(cacheFileName);
187-
if (existingData != null)
198+
if (_store.FileExists(cacheFileName))
188199
{
200+
var existingData = _store.ReadFileAsBytes(cacheFileName);
201+
if (existingData != null)
202+
{
189203
#if !NETSTANDARD
190204
try
191205
{
@@ -196,19 +210,23 @@ private void EnsureCacheFile(string cacheFileName = null)
196210
_store.DeleteFile(cacheFileName);
197211
}
198212
#else
199-
Deserialize(existingData);
213+
Deserialize(existingData);
200214
#endif
215+
}
201216
}
202-
}
203217

204-
// Eagerly create cache file.
218+
// Eagerly create cache file.
205219
#if !NETSTANDARD
206220
var dataToWrite = ProtectedData.Protect(Serialize(), null, DataProtectionScope.CurrentUser);
207221
#else
208-
var dataToWrite = Serialize();
222+
var dataToWrite = Serialize();
209223
#endif
210-
_store.WriteFile(cacheFileName, dataToWrite);
211-
fileLock.ReleaseMutex();
224+
_store.WriteFile(cacheFileName, dataToWrite);
225+
}
226+
finally
227+
{
228+
fileLock.ReleaseMutex();
229+
}
212230
}
213231
}
214232
}

0 commit comments

Comments
 (0)