Re: [Mono-dev] Race condition in named mutex

2016-06-16 Thread Miguel de Icaza
Hello Mikhail,

Please file a bug report so we can properly track this issue.

http://bugzilla.xamarin.com

Miguel

On Thu, Jun 16, 2016 at 5:04 AM, Mikhail Filippov 
wrote:

> I found problem in Mono in named mutex I have repro program:
> using System;
> using System.Threading;
>
> namespace Crasher
> {
> internal class Program
> {
> public static void Main (string[] args)
> {
> var a = "";
> for (var i = 0; i < 100; i++)
> {
> new Thread(Crasher).Start();
> }
> Console.WriteLine(a);
> Console.ReadLine();
> }
>
> private static void Crasher(){
> var rnd = new Random();
> while (true)
> {
> Thread.Sleep(rnd.Next(100, 1));
> using (var mutex = new Mutex(false,
> "Global\\TEST"))
> {
> var owner = false;
> try
> {
> owner =
> mutex.WaitOne(TimeSpan.FromMinutes(1));
> }
> finally
> {
> if (owner)
> {
>
> mutex.ReleaseMutex();
> }
> }
> Console.WriteLine ("PING");
> }
> Thread.Sleep(rnd.Next(100, 1));
> }
> }
> }
> }
>
> It program crached mono after few seconds.
> I see namedmutex_create and namedmutex_release use different locks.
> First time I found it problem when run Nuget client on mono.
> I verify repro on mono 4.2, 4.4, and master.
> Anybody can help me resolve this issue?
> On 4.4.0 native crash:
> namedmutex_create: error creating mutex handle
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> PING
> _wapi_handle_unref_full: Attempting to unref unused handle 0x4e0
> PING
> PING
> namedmutex_create: error creating mutex handle
> PING
> PING
> PING
> PING
> PING
> PING
> _wapi_handle_ref: Attempting to ref unused handle 0x4e3
> * Assertion at ../../mono/utils/mono-os-mutex.h:135, condition `res !=
> EINVAL' not met
>
> Stacktrace:
>
>   at  <0x>
>   at (wrapper managed-to-native)
> System.Threading.WaitHandle.WaitOne_internal
> (System.Threading.WaitHandle,intptr,int,bool) <0x00073>
>   at System.Threading.WaitHandle.WaitOne (System.TimeSpan,bool) <0x0009b>
>   at System.Threading.WaitHandle.WaitOne (System.TimeSpan) <0x0001d>
>   at Crasher.Program.Crasher () <0x000f0>
>   at System.Threading.ThreadHelper.ThreadStart_Context (object) <0x0009a>
>   at System.Threading.ExecutionContext.RunInternal
> (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool)
> <0x001c6>
>   at System.Threading.ExecutionContext.Run
> (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool)
> <0x00020>
>   at System.Threading.ExecutionContext.Run
> (System.Threading.ExecutionContext,System.Threading.ContextCallback,object)
> <0x00059>
>   at System.Threading.ThreadHelper.ThreadStart () <0x0002e>
>   at (wrapper runtime-invoke) object.runtime_invoke_void__this__
> (object,intptr,intptr,intptr) <0x000e0>
>
> Native stacktrace:
>
> 0   mono0x000103f9b0ca
> mono_handle_native_sigsegv + 271
> 1   libsystem_platform.dylib0x7fff854c252a
> _sigtramp + 26
> 2   mono0x0001042036a4 tmp_dir
> + 5316
> 3   libsystem_c.dylib   0x7fff935536e7 abort +
> 129
> 4   mono0x00010410d1f0
> monoeg_g_log + 0
> 5   mono0x00010410d175
> monoeg_g_logv + 83
> 6   mono0x00010410d31a
> monoeg_assertion_message + 143
> 7   mono0x0001040e453d
> _wapi_handle_timedwait_signal_handle + 1153
> 8   mono0x0001040f4aec
> wapi_WaitForSingleObjectEx + 606
> 9   mono0x00010406c01c
> mono_wait_uninterrupted + 130
> 10  mono0x00010406c1ff
> ves_icall_System_Threading_WaitHandle_WaitOne_internal + 73
> 11  ??? 0x000108130b54 0x0 +
> 

[Mono-dev] Race condition in named mutex

2016-06-16 Thread Mikhail Filippov
I found problem in Mono in named mutex I have repro program:
using System;
using System.Threading;

namespace Crasher
{
internal class Program
{
public static void Main (string[] args)
{
var a = "";
for (var i = 0; i < 100; i++)
{   
new Thread(Crasher).Start();
}
Console.WriteLine(a);
Console.ReadLine();
}

private static void Crasher(){
var rnd = new Random();
while (true)
{
Thread.Sleep(rnd.Next(100, 1));
using (var mutex = new Mutex(false, 
"Global\\TEST"))
{
var owner = false;
try
{
owner = 
mutex.WaitOne(TimeSpan.FromMinutes(1));
}
finally
{
if (owner)
{
mutex.ReleaseMutex();
}
}
Console.WriteLine ("PING");
}
Thread.Sleep(rnd.Next(100, 1));
}
}
}
}

It program crached mono after few seconds.
I see namedmutex_create and namedmutex_release use different locks.
First time I found it problem when run Nuget client on mono.
I verify repro on mono 4.2, 4.4, and master.
Anybody can help me resolve this issue?
On 4.4.0 native crash:
namedmutex_create: error creating mutex handle
PING
PING
PING
PING
PING
PING
PING
PING
PING
PING
PING
PING
PING
PING
_wapi_handle_unref_full: Attempting to unref unused handle 0x4e0
PING
PING
namedmutex_create: error creating mutex handle
PING
PING
PING
PING
PING
PING
_wapi_handle_ref: Attempting to ref unused handle 0x4e3
* Assertion at ../../mono/utils/mono-os-mutex.h:135, condition `res != EINVAL' 
not met

Stacktrace:

  at  <0x>
  at (wrapper managed-to-native) System.Threading.WaitHandle.WaitOne_internal 
(System.Threading.WaitHandle,intptr,int,bool) <0x00073>
  at System.Threading.WaitHandle.WaitOne (System.TimeSpan,bool) <0x0009b>
  at System.Threading.WaitHandle.WaitOne (System.TimeSpan) <0x0001d>
  at Crasher.Program.Crasher () <0x000f0>
  at System.Threading.ThreadHelper.ThreadStart_Context (object) <0x0009a>
  at System.Threading.ExecutionContext.RunInternal 
(System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool)
 <0x001c6>
  at System.Threading.ExecutionContext.Run 
(System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool)
 <0x00020>
  at System.Threading.ExecutionContext.Run 
(System.Threading.ExecutionContext,System.Threading.ContextCallback,object) 
<0x00059>
  at System.Threading.ThreadHelper.ThreadStart () <0x0002e>
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ 
(object,intptr,intptr,intptr) <0x000e0>

Native stacktrace:

0   mono0x000103f9b0ca 
mono_handle_native_sigsegv + 271
1   libsystem_platform.dylib0x7fff854c252a _sigtramp + 
26
2   mono0x0001042036a4 tmp_dir + 
5316
3   libsystem_c.dylib   0x7fff935536e7 abort + 129
4   mono0x00010410d1f0 monoeg_g_log 
+ 0
5   mono0x00010410d175 
monoeg_g_logv + 83
6   mono0x00010410d31a 
monoeg_assertion_message + 143
7   mono0x0001040e453d 
_wapi_handle_timedwait_signal_handle + 1153
8   mono0x0001040f4aec 
wapi_WaitForSingleObjectEx + 606
9   mono0x00010406c01c 
mono_wait_uninterrupted + 130
10  mono0x00010406c1ff 
ves_icall_System_Threading_WaitHandle_WaitOne_internal + 73
11  ??? 0x000108130b54 0x0 + 
4430433108
12  mscorlib.dll.dylib  0x0001062ed7ae 
System_Threading_WaitHandle_WaitOne_System_TimeSpan + 30
13  mscorlib.dll.dylib  0x000106140e5b 
System_Threading_ThreadHelper_ThreadStart_Context_object + 155
14  mscorlib.dll.dylib  0x00010613f331