From d2e433c5d7d5a489124750ce20ed372d2ad40483 Mon Sep 17 00:00:00 2001 From: Sparronator9999 <86388887+Sparronator9999@users.noreply.github.com> Date: Mon, 30 Dec 2024 20:00:26 +1100 Subject: [PATCH] Fix potential race condition in IPC message sending function Hopefully this finally fixes the random EC monitor errors... --- YAMDCC.Config/YAMDCC.Config.csproj | 4 +-- YAMDCC.Config/packages.lock.json | 2 +- .../YAMDCC.ConfigEditor.csproj | 4 +-- YAMDCC.ConfigEditor/packages.lock.json | 8 +++--- YAMDCC.ECAccess/YAMDCC.ECAccess.csproj | 4 +-- YAMDCC.ECAccess/packages.lock.json | 2 +- YAMDCC.IPC/NamedPipeConnection.cs | 27 +++++++++---------- YAMDCC.IPC/YAMDCC.IPC.csproj | 2 +- YAMDCC.Logs/YAMDCC.Logs.csproj | 4 +-- YAMDCC.Logs/packages.lock.json | 2 +- YAMDCC.Service/YAMDCC.Service.csproj | 4 +-- YAMDCC.Service/packages.lock.json | 2 +- 12 files changed, 31 insertions(+), 34 deletions(-) diff --git a/YAMDCC.Config/YAMDCC.Config.csproj b/YAMDCC.Config/YAMDCC.Config.csproj index cbdc7c9..dc458b6 100644 --- a/YAMDCC.Config/YAMDCC.Config.csproj +++ b/YAMDCC.Config/YAMDCC.Config.csproj @@ -12,8 +12,8 @@ win7-x64;win7-x86;win-x64;win-x86 net48 YAMDCC config library - 1.0.0.0 - beta.1 + 1.0.0 + dev none diff --git a/YAMDCC.Config/packages.lock.json b/YAMDCC.Config/packages.lock.json index 4e8173e..9cb623c 100644 --- a/YAMDCC.Config/packages.lock.json +++ b/YAMDCC.Config/packages.lock.json @@ -7,4 +7,4 @@ ".NETFramework,Version=v4.8/win7-x64": {}, ".NETFramework,Version=v4.8/win7-x86": {} } -} +} \ No newline at end of file diff --git a/YAMDCC.ConfigEditor/YAMDCC.ConfigEditor.csproj b/YAMDCC.ConfigEditor/YAMDCC.ConfigEditor.csproj index 7ce1fb3..69fd444 100644 --- a/YAMDCC.ConfigEditor/YAMDCC.ConfigEditor.csproj +++ b/YAMDCC.ConfigEditor/YAMDCC.ConfigEditor.csproj @@ -20,8 +20,8 @@ net48 YAMDCC configuration utility true - 1.0.0.0 - beta.1 + 1.0.0 + dev none diff --git a/YAMDCC.ConfigEditor/packages.lock.json b/YAMDCC.ConfigEditor/packages.lock.json index 93cc11d..8f97aee 100644 --- a/YAMDCC.ConfigEditor/packages.lock.json +++ b/YAMDCC.ConfigEditor/packages.lock.json @@ -104,10 +104,10 @@ "yamdccsvc": { "type": "Project", "dependencies": { - "YAMDCC.Config": "[1.0.0-beta.1, )", - "YAMDCC.ECAccess": "[1.0.0-beta.1, )", + "YAMDCC.Config": "[1.0.0-dev, )", + "YAMDCC.ECAccess": "[1.0.0-dev, )", "YAMDCC.IPC": "[2.1.0-release, )", - "YAMDCC.Logs": "[1.0.0-beta.1, )" + "YAMDCC.Logs": "[1.0.0-dev, )" } } }, @@ -116,4 +116,4 @@ ".NETFramework,Version=v4.8/win7-x64": {}, ".NETFramework,Version=v4.8/win7-x86": {} } -} +} \ No newline at end of file diff --git a/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj b/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj index d23ce47..d4e0fb0 100644 --- a/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj +++ b/YAMDCC.ECAccess/YAMDCC.ECAccess.csproj @@ -13,8 +13,8 @@ win7-x64;win7-x86;win-x64;win-x86 net48 YAMDCC EC access library - 1.0.0.0 - beta.1 + 1.0.0 + dev none diff --git a/YAMDCC.ECAccess/packages.lock.json b/YAMDCC.ECAccess/packages.lock.json index 4e8173e..9cb623c 100644 --- a/YAMDCC.ECAccess/packages.lock.json +++ b/YAMDCC.ECAccess/packages.lock.json @@ -7,4 +7,4 @@ ".NETFramework,Version=v4.8/win7-x64": {}, ".NETFramework,Version=v4.8/win7-x86": {} } -} +} \ No newline at end of file diff --git a/YAMDCC.IPC/NamedPipeConnection.cs b/YAMDCC.IPC/NamedPipeConnection.cs index b8f8389..03961e0 100644 --- a/YAMDCC.IPC/NamedPipeConnection.cs +++ b/YAMDCC.IPC/NamedPipeConnection.cs @@ -1,5 +1,5 @@ using System; -using System.Collections.Generic; +using System.Collections.Concurrent; using System.IO.Pipes; using System.Runtime.InteropServices; using System.Threading; @@ -58,8 +58,7 @@ public class NamedPipeConnection : IDisposable private readonly PipeStreamWrapper _streamWrapper; - private readonly AutoResetEvent _writeSignal = new(false); - private readonly Queue _writeQueue = new(); + private readonly BlockingCollection _writeQueue = new(); private bool _notifiedSucceeded; @@ -82,10 +81,9 @@ internal NamedPipeConnection(int id, string name, PipeStream serverStream) /// /// The message to write to the named pipe. /// - public void PushMessage(TWrite message) + public bool PushMessage(TWrite message) { - _writeQueue.Enqueue(message); - _writeSignal.Set(); + return _writeQueue.TryAdd(message); } /// @@ -118,7 +116,7 @@ internal void Open() internal void Close() { _streamWrapper.Close(); - _writeSignal.Set(); + _writeQueue.CompleteAdding(); } /// @@ -154,15 +152,14 @@ private void ReadPipe() while (IsConnected && _streamWrapper.CanRead) { TRead obj = _streamWrapper.ReadObject(); - if (obj == null) + if (obj is null) { Close(); return; } - PipeMessageEventArgs e = - new(this, obj); - ReceiveMessage?.Invoke(this, e); + ReceiveMessage?.Invoke(this, + new PipeMessageEventArgs(this, obj)); } } @@ -173,10 +170,9 @@ private void WritePipe() { while (IsConnected && _streamWrapper.CanWrite) { - _writeSignal.WaitOne(); - while (_writeQueue.Count > 0) + if (_writeQueue.TryTake(out TWrite obj)) { - _streamWrapper.WriteObject(_writeQueue.Dequeue()); + _streamWrapper.WriteObject(obj); _streamWrapper.WaitForPipeDrain(); } } @@ -197,7 +193,8 @@ private void Dispose(bool disposing) if (disposing) { - _writeSignal.Dispose(); + Close(); + _writeQueue.Dispose(); } _disposed = true; diff --git a/YAMDCC.IPC/YAMDCC.IPC.csproj b/YAMDCC.IPC/YAMDCC.IPC.csproj index f1f3814..c237d43 100644 --- a/YAMDCC.IPC/YAMDCC.IPC.csproj +++ b/YAMDCC.IPC/YAMDCC.IPC.csproj @@ -12,7 +12,7 @@ win7-x64;win7-x86;win-x64;win-x86 net48 Named Pipe Wrapper library - 2.1.0.0 + 2.1.1 release diff --git a/YAMDCC.Logs/YAMDCC.Logs.csproj b/YAMDCC.Logs/YAMDCC.Logs.csproj index 4f9f1e6..d4ae98a 100644 --- a/YAMDCC.Logs/YAMDCC.Logs.csproj +++ b/YAMDCC.Logs/YAMDCC.Logs.csproj @@ -12,8 +12,8 @@ win7-x64;win7-x86;win-x64;win-x86 net48 YAMDCC logging library - 1.0.0.0 - beta.1 + 1.0.0 + dev none diff --git a/YAMDCC.Logs/packages.lock.json b/YAMDCC.Logs/packages.lock.json index 4e8173e..9cb623c 100644 --- a/YAMDCC.Logs/packages.lock.json +++ b/YAMDCC.Logs/packages.lock.json @@ -7,4 +7,4 @@ ".NETFramework,Version=v4.8/win7-x64": {}, ".NETFramework,Version=v4.8/win7-x86": {} } -} +} \ No newline at end of file diff --git a/YAMDCC.Service/YAMDCC.Service.csproj b/YAMDCC.Service/YAMDCC.Service.csproj index ed7ab84..4827ab8 100644 --- a/YAMDCC.Service/YAMDCC.Service.csproj +++ b/YAMDCC.Service/YAMDCC.Service.csproj @@ -14,8 +14,8 @@ win7-x64;win7-x86;win-x64;win-x86 net48 YAMDCC helper service - 1.0.0.0 - beta.1 + 1.0.0 + dev app.manifest diff --git a/YAMDCC.Service/packages.lock.json b/YAMDCC.Service/packages.lock.json index 829b6b9..abf62a4 100644 --- a/YAMDCC.Service/packages.lock.json +++ b/YAMDCC.Service/packages.lock.json @@ -107,4 +107,4 @@ ".NETFramework,Version=v4.8/win7-x64": {}, ".NETFramework,Version=v4.8/win7-x86": {} } -} +} \ No newline at end of file