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

[w32handle] Fix race condition when creating named mutex/event/semaphore #3560

Merged
merged 1 commit into from Sep 14, 2016
Jump to file or symbol
Failed to load files and symbols.
+179 −97
Diff settings

Always

Just for now

@@ -227,8 +227,7 @@ static gpointer namedevent_create (gboolean manual, gboolean initial, const guni
/* Not an error, but this is how the caller is informed that the event wasn't freshly created */
SetLastError (ERROR_ALREADY_EXISTS);
/* this is used as creating a new handle */
mono_w32handle_ref (handle);
/* mono_w32handle_namespace_search_handle already adds a ref to the handle */
} else {
/* A new named event */
MonoW32HandleNamedEvent namedevent_handle;
@@ -89,6 +89,10 @@ mono_w32handle_namespace_search_handle_callback (gpointer handle, gpointer data,
} else {
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_IO_LAYER, "%s: handle %p matches name and type",
__func__, handle);
/* we do not want the handle to be destroyed before we return it */
mono_w32handle_ref (handle);
search_data->ret = handle;
}
@@ -282,8 +282,7 @@ static gpointer namedmutex_create (gboolean owned, const gunichar2 *name)
/* Not an error, but this is how the caller is informed that the mutex wasn't freshly created */
SetLastError (ERROR_ALREADY_EXISTS);
/* this is used as creating a new handle */
mono_w32handle_ref (handle);
/* mono_w32handle_namespace_search_handle already adds a ref to the handle */
} else {
/* A new named mutex */
MonoW32HandleNamedMutex namedmutex_handle;
@@ -200,8 +200,7 @@ namedsem_create (gint32 initial, gint32 max, const gunichar2 *name)
/* Not an error, but this is how the caller is informed that the semaphore wasn't freshly created */
SetLastError (ERROR_ALREADY_EXISTS);
/* this is used as creating a new handle */
mono_w32handle_ref (handle);
/* mono_w32handle_namespace_search_handle already adds a ref to the handle */
} else {
/* A new named semaphore */
MonoW32HandleNamedSemaphore namedsem_handle;
View
@@ -468,7 +468,8 @@ BASE_TEST_CS_SRC_UNIVERSAL= \
bug-29585.cs \
priority.cs \
abort-cctor.cs \
reference-loader.cs
reference-loader.cs \
namedmutex-destroy-race.cs
if INSTALL_MOBILE_STATIC
BASE_TEST_CS_SRC= \
@@ -0,0 +1,45 @@
/* test for https://bugzilla.xamarin.com/show_bug.cgi?id=41914 */
using System;
using System.Threading;
namespace Crasher
{
class Program
{
public static void Main (string[] args)
{
Thread[] threads = new Thread[100];
DateTime start = DateTime.Now;
for (int i = 0; i < threads.Length; ++i) {
threads [i] = new Thread (() => {
var rnd = new Random();
do {
using (var mutex = new Mutex(false, "Global\\TEST")) {
var owner = false;
try {
owner = mutex.WaitOne(TimeSpan.FromMinutes(1));
} finally {
if (owner)
mutex.ReleaseMutex();
}
}
Thread.Sleep(rnd.Next(100, 1000));
} while ((DateTime.Now - start) < TimeSpan.FromSeconds (30));
});
}
for (int i = 0; i < threads.Length; ++i)
threads [i].Start ();
for (int i = 0; i < threads.Length; ++i)
threads [i].Join ();
}
private static void Crasher(){
}
}
}
Oops, something went wrong.
ProTip! Use n and p to navigate between commits in a pull request.