Skip to content

Commit 6fb60ea

Browse files
mizantrop777MrBlue
authored andcommitted
Add lock to FSWatcher change queue
1 parent b4c2b9a commit 6fb60ea

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

src/Plugins/Watchers/FSWatcher.cs

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ private class QueuedChange
3434

3535
// Changes are buffered briefly to avoid duplicate events
3636
private Dictionary<string, QueuedChange> changeQueue;
37+
private readonly object changeQueueLock = new object();
3738

3839
private Timer timers;
3940

@@ -172,43 +173,47 @@ private void watcher_Changed(object sender, FileSystemEventArgs e)
172173
int length = e.FullPath.Length - watcher.Path.Length - Path.GetExtension(e.Name).Length - 1;
173174
string subPath = e.FullPath.Substring(watcher.Path.Length + 1, length);
174175

175-
if (!changeQueue.TryGetValue(subPath, out QueuedChange change))
176+
QueuedChange change;
177+
lock (changeQueueLock)
176178
{
177-
change = new QueuedChange();
178-
changeQueue[subPath] = change;
179-
}
180-
change.timer?.Destroy();
181-
change.timer = null;
179+
if (!changeQueue.TryGetValue(subPath, out change))
180+
{
181+
change = new QueuedChange();
182+
changeQueue[subPath] = change;
183+
}
184+
change.timer?.Destroy();
185+
change.timer = null;
182186

183-
switch (e.ChangeType)
184-
{
185-
case WatcherChangeTypes.Changed:
186-
if (change.type != WatcherChangeTypes.Created)
187-
{
188-
change.type = WatcherChangeTypes.Changed;
189-
}
190-
break;
187+
switch (e.ChangeType)
188+
{
189+
case WatcherChangeTypes.Changed:
190+
if (change.type != WatcherChangeTypes.Created)
191+
{
192+
change.type = WatcherChangeTypes.Changed;
193+
}
194+
break;
191195

192-
case WatcherChangeTypes.Created:
193-
if (change.type == WatcherChangeTypes.Deleted)
194-
{
195-
change.type = WatcherChangeTypes.Changed;
196-
}
197-
else
198-
{
199-
change.type = WatcherChangeTypes.Created;
200-
}
201-
break;
196+
case WatcherChangeTypes.Created:
197+
if (change.type == WatcherChangeTypes.Deleted)
198+
{
199+
change.type = WatcherChangeTypes.Changed;
200+
}
201+
else
202+
{
203+
change.type = WatcherChangeTypes.Created;
204+
}
205+
break;
202206

203-
case WatcherChangeTypes.Deleted:
204-
if (change.type == WatcherChangeTypes.Created)
205-
{
206-
changeQueue.Remove(subPath);
207-
return;
208-
}
207+
case WatcherChangeTypes.Deleted:
208+
if (change.type == WatcherChangeTypes.Created)
209+
{
210+
changeQueue.Remove(subPath);
211+
return;
212+
}
209213

210-
change.type = WatcherChangeTypes.Deleted;
211-
break;
214+
change.type = WatcherChangeTypes.Deleted;
215+
break;
216+
}
212217
}
213218

214219
Interface.Oxide.NextTick(() =>
@@ -238,8 +243,11 @@ private void watcher_Changed(object sender, FileSystemEventArgs e)
238243
change.timer?.Destroy();
239244
change.timer = timers.Once(.2f, () =>
240245
{
241-
change.timer = null;
242-
changeQueue.Remove(subPath);
246+
lock (changeQueueLock)
247+
{
248+
change.timer = null;
249+
changeQueue.Remove(subPath);
250+
}
243251

244252
if (Regex.Match(subPath, @"include\\", RegexOptions.IgnoreCase).Success)
245253
{

0 commit comments

Comments
 (0)