Skip to content

Commit

Permalink
Merge pull request #6452 from peppy/fix-multiple-instances
Browse files Browse the repository at this point in the history
Fix multiple instances of a game being allowed to start concurrently
  • Loading branch information
bdach authored Dec 12, 2024
2 parents f5900ab + 39ddcfa commit 69a5a39
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ public void SetUpSteps()
});
}

// Fails once in a blue moon due to loose (but maybe-not-loose-enough) timing requirements. If we break things, it will fail every time so this is fine.
[TestCase(false)]
[TestCase(true)]
[FlakyTest]
public void TestManyChildren(bool instant)
{
AddStep("create children", () =>
Expand Down
19 changes: 17 additions & 2 deletions osu.Framework/Platform/NamedPipeIpcProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class NamedPipeIpcProvider : IDisposable

private NamedPipeServerStream? pipe;

private Mutex? mutex;

/// <summary>
/// Create a new provider.
/// </summary>
Expand All @@ -57,7 +59,17 @@ public bool Bind()

try
{
pipe = new NamedPipeServerStream($"osu-framework-{pipeName}", PipeDirection.InOut);
string name = $"osu-framework-{pipeName}";

// Named pipes from different processes are allowed to coexist, but we don't want this for our purposes.
// Using a system global mutex allows ensuring that only one osu!framework project using the same pipe name
// will be able to bind.
mutex = new Mutex(false, $"Global\\{name}", out bool createdNew);

if (!createdNew)
return false;

pipe = new NamedPipeServerStream(name, PipeDirection.InOut, 1);

listenTask = listen(pipe);

Expand Down Expand Up @@ -110,7 +122,8 @@ private async Task listen(NamedPipeServerStream pipe)
{
try
{
pipe.Disconnect();
if (pipe.IsConnected)
pipe.Disconnect();
}
catch
{
Expand Down Expand Up @@ -202,6 +215,8 @@ public void Dispose()

cancellationSource.Cancel();

mutex?.Dispose();

if (listenTask != null)
{
try
Expand Down

0 comments on commit 69a5a39

Please sign in to comment.