@@ -24,7 +24,7 @@ public sealed class UsageService : IUsageService, IDisposable
24
24
25
25
readonly IGitHubServiceProvider serviceProvider ;
26
26
readonly IEnvironment environment ;
27
- readonly SemaphoreSlim writeSemaphoreSlim = new SemaphoreSlim ( 1 , 1 ) ;
27
+ readonly SemaphoreSlim semaphoreSlim = new SemaphoreSlim ( 1 , 1 ) ;
28
28
29
29
string storePath ;
30
30
string userStorePath ;
@@ -39,7 +39,7 @@ public UsageService(IGitHubServiceProvider serviceProvider, IEnvironment environ
39
39
40
40
public void Dispose ( )
41
41
{
42
- writeSemaphoreSlim . Dispose ( ) ;
42
+ semaphoreSlim . Dispose ( ) ;
43
43
}
44
44
45
45
public async Task < Guid > GetUserGuid ( )
@@ -146,17 +146,26 @@ async Task Initialize()
146
146
147
147
async Task < string > ReadAllTextAsync ( string path )
148
148
{
149
- using ( var s = File . OpenRead ( path ) )
150
- using ( var r = new StreamReader ( s , Encoding . UTF8 ) )
149
+ // Avoid IOException when metrics updated multiple times in quick succession
150
+ await semaphoreSlim . WaitAsync ( ) ;
151
+ try
152
+ {
153
+ using ( var s = File . OpenRead ( path ) )
154
+ using ( var r = new StreamReader ( s , Encoding . UTF8 ) )
155
+ {
156
+ return await r . ReadToEndAsync ( ) ;
157
+ }
158
+ }
159
+ finally
151
160
{
152
- return await r . ReadToEndAsync ( ) ;
161
+ semaphoreSlim . Release ( ) ;
153
162
}
154
163
}
155
164
156
165
async Task WriteAllTextAsync ( string path , string text )
157
166
{
158
167
// Avoid IOException when metrics updated multiple times in quick succession
159
- await writeSemaphoreSlim . WaitAsync ( ) ;
168
+ await semaphoreSlim . WaitAsync ( ) ;
160
169
try
161
170
{
162
171
using ( var s = new FileStream ( path , FileMode . Create ) )
@@ -167,7 +176,7 @@ async Task WriteAllTextAsync(string path, string text)
167
176
}
168
177
finally
169
178
{
170
- writeSemaphoreSlim . Release ( ) ;
179
+ semaphoreSlim . Release ( ) ;
171
180
}
172
181
}
173
182
0 commit comments