Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch away from thread abort where we can #336

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/ChorusHub/Advertiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Chorus.ChorusHub;

namespace ChorusHub
{
public class Advertiser : IDisposable
{
private Thread _thread;
private CancellationTokenSource _cancellationTokenSource;
private UdpClient _client;
private IPEndPoint _endPoint;
private byte[] _sendBytes;
Expand All @@ -33,30 +35,44 @@ public void Start()
EnableBroadcast = true
};
_endPoint = new IPEndPoint(IPAddress.Parse("255.255.255.255"), Port);
_cancellationTokenSource = new CancellationTokenSource();
_thread = new Thread(Work);
_thread.Start();
}

private void Work()
{
bool cancelled = false;
try
{
while (true)
while (!_cancellationTokenSource.Token.IsCancellationRequested)
{
UpdateAdvertisementBasedOnCurrentIpAddress();
_client.BeginSend(_sendBytes, _sendBytes.Length, _endPoint, SendCallback, _client);
Thread.Sleep(1000);
_client.BeginSend(_sendBytes,
_sendBytes.Length,
_endPoint,
SendCallback,
_client);
Task.Delay(1000).Wait(_cancellationTokenSource.Token);
}
}
catch (OperationCanceledException)
{
cancelled = true;
}
catch(ThreadAbortException)
{
//Progress.WriteVerbose("Advertiser Thread Aborting (that's normal)");
_client.Close();
cancelled = true;
}
catch(Exception)
{
//EventLog.WriteEntry("Application", string.Format("Error in Advertiser: {0}", error.Message), EventLogEntryType.Error);
}

if (_cancellationTokenSource.Token.IsCancellationRequested && cancelled)
{
_client.Close();
}
}

public static void SendCallback(IAsyncResult args)
Expand Down Expand Up @@ -104,7 +120,7 @@ public void Stop()
return;

//EventLog.WriteEntry("Application", "Advertiser Stopping...", EventLogEntryType.Information);
_thread.Abort();
_cancellationTokenSource.Cancel();
_thread.Join(2 * 1000);
_thread = null;
}
Expand Down
56 changes: 21 additions & 35 deletions src/ChorusHub/HgServeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class HgServeRunner : IDisposable
public readonly int Port;
private readonly string _rootFolder;
private Thread _hgServeThread;
private CancellationTokenSource _tokenSource;

public HgServeRunner(string rootFolder, int port)
{
Expand Down Expand Up @@ -76,32 +77,25 @@ public bool Start()
_hgServeProcess.StartInfo.RedirectStandardOutput = true;
_hgServeProcess.Start();
#else
_tokenSource = new CancellationTokenSource();
_hgServeThread = new Thread(() =>
{
var commandLineRunner = new CommandLineRunner();
try
{
var progress = new ConsoleProgress();
commandLineRunner.Start(
Chorus.MercurialLocation.PathToHgExecutable,
arguments,
Encoding.UTF8, _rootFolder, -1,
progress, s => progress.WriteMessage(s));
}
catch (ThreadAbortException)
{
//Progress.WriteVerbose("Hg Serve command Thread Aborting (that's normal when stopping)");
try
{
if (!commandLineRunner.Abort(1))
{
//EventLog.WriteEntry("Application", "Hg Serve might not have closed down.", EventLogEntryType.Information);
}
}
catch { }

}
});
{
var commandLineRunner = new CommandLineRunner();
var progress = new ConsoleProgress();
_tokenSource.Token.Register(() =>
{
progress.CancelRequested = true;
commandLineRunner.Abort(1);
});
commandLineRunner.Start(
Chorus.MercurialLocation.PathToHgExecutable,
arguments,
Encoding.UTF8,
_rootFolder,
-1,
progress,
s => progress.WriteMessage(s));
});
_hgServeThread.Start();
#endif

Expand Down Expand Up @@ -190,16 +184,8 @@ public void Stop()
if(_hgServeThread !=null && _hgServeThread.IsAlive)
{
//Progress.WriteMessage("Hg Server Stopping...");
_hgServeThread.Abort();

if(_hgServeThread.Join(2 * 1000))
{
//Progress.WriteMessage("Hg Server Stopped");
}
else
{
//Progress.WriteError("***Gave up on hg server stopping");
}
_tokenSource.Cancel(false);
_hgServeThread.Join(2 * 1000);
_hgServeThread = null;
}
}
Expand Down
125 changes: 0 additions & 125 deletions src/LibChorus/ProcessStream.cs

This file was deleted.

Loading
Loading