Skip to content

Commit

Permalink
Fix potential race condition in IPC message sending function
Browse files Browse the repository at this point in the history
Hopefully this finally fixes the random EC monitor errors...
  • Loading branch information
Sparronator9999 committed Dec 30, 2024
1 parent 1979f21 commit d2e433c
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions YAMDCC.Config/YAMDCC.Config.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>YAMDCC config library</Title>
<VersionPrefix>1.0.0.0</VersionPrefix>
<VersionSuffix>beta.1</VersionSuffix>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugType>none</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.Config/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
".NETFramework,Version=v4.8/win7-x64": {},
".NETFramework,Version=v4.8/win7-x86": {}
}
}
}
4 changes: 2 additions & 2 deletions YAMDCC.ConfigEditor/YAMDCC.ConfigEditor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<TargetFramework>net48</TargetFramework>
<Title>YAMDCC configuration utility</Title>
<UseWindowsForms>true</UseWindowsForms>
<VersionPrefix>1.0.0.0</VersionPrefix>
<VersionSuffix>beta.1</VersionSuffix>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugType>none</DebugType>
Expand Down
8 changes: 4 additions & 4 deletions YAMDCC.ConfigEditor/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )"
}
}
},
Expand All @@ -116,4 +116,4 @@
".NETFramework,Version=v4.8/win7-x64": {},
".NETFramework,Version=v4.8/win7-x86": {}
}
}
}
4 changes: 2 additions & 2 deletions YAMDCC.ECAccess/YAMDCC.ECAccess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>YAMDCC EC access library</Title>
<VersionPrefix>1.0.0.0</VersionPrefix>
<VersionSuffix>beta.1</VersionSuffix>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugType>none</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.ECAccess/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
".NETFramework,Version=v4.8/win7-x64": {},
".NETFramework,Version=v4.8/win7-x86": {}
}
}
}
27 changes: 12 additions & 15 deletions YAMDCC.IPC/NamedPipeConnection.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -58,8 +58,7 @@ public class NamedPipeConnection<TRead, TWrite> : IDisposable

private readonly PipeStreamWrapper<TRead, TWrite> _streamWrapper;

private readonly AutoResetEvent _writeSignal = new(false);
private readonly Queue<TWrite> _writeQueue = new();
private readonly BlockingCollection<TWrite> _writeQueue = new();

private bool _notifiedSucceeded;

Expand All @@ -82,10 +81,9 @@ internal NamedPipeConnection(int id, string name, PipeStream serverStream)
/// <param name="message">
/// The message to write to the named pipe.
/// </param>
public void PushMessage(TWrite message)
public bool PushMessage(TWrite message)
{
_writeQueue.Enqueue(message);
_writeSignal.Set();
return _writeQueue.TryAdd(message);
}

/// <summary>
Expand Down Expand Up @@ -118,7 +116,7 @@ internal void Open()
internal void Close()
{
_streamWrapper.Close();
_writeSignal.Set();
_writeQueue.CompleteAdding();
}

/// <summary>
Expand Down Expand Up @@ -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<TRead, TWrite> e =
new(this, obj);

ReceiveMessage?.Invoke(this, e);
ReceiveMessage?.Invoke(this,
new PipeMessageEventArgs<TRead, TWrite>(this, obj));
}
}

Expand All @@ -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();
}
}
Expand All @@ -197,7 +193,8 @@ private void Dispose(bool disposing)

if (disposing)
{
_writeSignal.Dispose();
Close();
_writeQueue.Dispose();
}

_disposed = true;
Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.IPC/YAMDCC.IPC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>Named Pipe Wrapper library</Title>
<VersionPrefix>2.1.0.0</VersionPrefix>
<VersionPrefix>2.1.1</VersionPrefix>
<VersionSuffix>release</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
4 changes: 2 additions & 2 deletions YAMDCC.Logs/YAMDCC.Logs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>YAMDCC logging library</Title>
<VersionPrefix>1.0.0.0</VersionPrefix>
<VersionSuffix>beta.1</VersionSuffix>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<DebugType>none</DebugType>
Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.Logs/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
".NETFramework,Version=v4.8/win7-x64": {},
".NETFramework,Version=v4.8/win7-x86": {}
}
}
}
4 changes: 2 additions & 2 deletions YAMDCC.Service/YAMDCC.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
<RuntimeIdentifiers>win7-x64;win7-x86;win-x64;win-x86</RuntimeIdentifiers>
<TargetFramework>net48</TargetFramework>
<Title>YAMDCC helper service</Title>
<VersionPrefix>1.0.0.0</VersionPrefix>
<VersionSuffix>beta.1</VersionSuffix>
<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>dev</VersionSuffix>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down
2 changes: 1 addition & 1 deletion YAMDCC.Service/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,4 @@
".NETFramework,Version=v4.8/win7-x64": {},
".NETFramework,Version=v4.8/win7-x86": {}
}
}
}

0 comments on commit d2e433c

Please sign in to comment.