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

Fix multiple instances of a game being allowed to start concurrently #6452

Merged
merged 3 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading