Skip to content

Commit

Permalink
Fix the Threading Issue in GitEventsMonitor (#5019)
Browse files Browse the repository at this point in the history
Fixes #5020
  • Loading branch information
ugras-ergun-sonarsource authored Nov 15, 2023
1 parent af16f6b commit dbf2515
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/Core.UnitTests/GitEventsMonitorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

using System;
using System.IO.Abstractions;
using System.Threading.Tasks;
using System.Windows.Threading;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
Expand All @@ -46,6 +48,8 @@ public void HeadChanged_EventInvoked()

fileSystemWatcher.Raise(w => w.Renamed += null, null, null);

Dispatcher.CurrentDispatcher.Invoke(() => { }); // Force thread dispatcher to process other threads

counter.Should().Be(1);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Core/GitEventsMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ public sealed class GitEventsMonitor : IGitEvents, IDisposable
private const string GitFolder = ".git";
private const string HEADFile = "HEAD";


public GitEventsMonitor(string repoFolder) : this(repoFolder, new FileSystemWatcherFactory())
{

}

internal GitEventsMonitor(string repoFolder, IFileSystemWatcherFactory fileSystemFactory)
Expand All @@ -73,7 +71,10 @@ private void WatchGitEvents(string repoFolder, IFileSystemWatcherFactory fileSys

private void HeadFileChanged(object sender, FileSystemEventArgs e)
{
HeadChanged?.Invoke(this, EventArgs.Empty);
HeadChanged?.BeginInvoke(this, EventArgs.Empty, new AsyncCallback((IAsyncResult ar) =>
{
HeadChanged?.EndInvoke(ar);
}), null);
}

public void Dispose()
Expand Down

0 comments on commit dbf2515

Please sign in to comment.