Skip to content

Commit a3d53ec

Browse files
committed
fix: use DataDir hash in IPC pipe name to isolate instances with different data dirs
1 parent 6dab78b commit a3d53ec

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

‎src/Models/IpcChannel.cs‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.IO;
33
using System.IO.Pipes;
4+
using System.Security.Cryptography;
5+
using System.Text;
46
using System.Threading;
57
using System.Threading.Tasks;
68

@@ -19,7 +21,7 @@ public IpcChannel()
1921
_singletonLock = File.Open(Path.Combine(Native.OS.DataDir, "process.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
2022
IsFirstInstance = true;
2123
_server = new NamedPipeServerStream(
22-
"SourceGitIPCChannel" + Environment.UserName,
24+
GetPipeName(),
2325
PipeDirection.In,
2426
-1,
2527
PipeTransmissionMode.Byte,
@@ -37,7 +39,7 @@ public void SendToFirstInstance(string cmd)
3739
{
3840
try
3941
{
40-
using (var client = new NamedPipeClientStream(".", "SourceGitIPCChannel" + Environment.UserName, PipeDirection.Out, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly))
42+
using (var client = new NamedPipeClientStream(".", GetPipeName(), PipeDirection.Out, PipeOptions.Asynchronous | PipeOptions.CurrentUserOnly))
4143
{
4244
client.Connect(1000);
4345
if (!client.IsConnected)
@@ -67,6 +69,14 @@ public void Dispose()
6769
_singletonLock?.Dispose();
6870
}
6971

72+
private static string GetPipeName()
73+
{
74+
var dataDir = Native.OS.DataDir.Replace('\\', '/').TrimEnd('/');
75+
var hash = SHA256.HashData(Encoding.UTF8.GetBytes(dataDir));
76+
var hashStr = Convert.ToHexString(hash)[..16];
77+
return $"SourceGitIPCChannel{Environment.UserName}_{hashStr}";
78+
}
79+
7080
private async void StartServer()
7181
{
7282
using var reader = new StreamReader(_server);

0 commit comments

Comments
 (0)