Bug 53078 - WaitHandle.WaitAny consumes multiple AutoResetEvents on Xamarin.Android and Xamarin.iOS
Summary: WaitHandle.WaitAny consumes multiple AutoResetEvents on Xamarin.Android and X...
Status: NEW
Alias: None
Product: Runtime
Classification: Mono
Component: Interop (show other bugs)
Version: unspecified
Hardware: Other Other
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2017-03-06 16:14 UTC by adrian.eggenberger
Modified: 2017-05-05 16:58 UTC (History)
6 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

patch (554 bytes, patch)
2017-03-31 18:38 UTC, Martin Frydl
Details | Diff

Description adrian.eggenberger 2017-03-06 16:14:13 UTC
Hi all
I've a strange behavior on my Android App that I couldn't explain. Let me show it with some example code:

  AutoResetEvent a1 = new AutoResetEvent(false);
  AutoResetEvent a2 = new AutoResetEvent(false);

  WaitHandle[] allHandles = new WaitHandle[] {a1, a2};


  int i = WaitHandle.WaitAny(allHandles);
  Console.WriteLine("Handle {0} signaled", i);

  int j = WaitHandle.WaitAny(allHandles);
  Console.WriteLine("Handle {0} signaled", j);

On Windows this code runs through and creates the output that I would expect:
Handle 0 signaled
Handle 1 signaled

On Android and iOS I get only the first line an the execution stops at the second WaitAny. It seams that the AutoResetEvent a2 is consumed by the first WaitHandle.WaitAny call. In my understanding this shouldn't happen.

Is this a bug or did I misunderstand the AutoResetEvent?

I've also posted the bug to the Xamarin Forum, but without any response (https://forums.xamarin.com/discussion/89895/waithandle-waitany-consumes-multiple-autoresetevents-on-android).

Best regards
Comment 1 jaromir.flidr 2017-03-28 12:24:59 UTC
I also experienced this bug when running on Linux. Causing Oracle managed driver to get stuck on retriving new connection when connection pool is empty. See similar sample code bellow. Also as far as I can see this bug was introduces in mono 4.8, since 4.6. is running fine. Also 4.8. on windows is ok.

My code
var sem1 = new Semaphore(1, 2);
var sem2 = new Semaphore(1, 2);
var handles = new WaitHandle[2];
handles[0] = sem1;
handles[1] = sem2;

var result1 = WaitHandle.WaitAny(handles, -1);
Console.WriteLine("Got {0}", result1);
var result2 = WaitHandle.WaitAny(handles, -1);
Console.WriteLine("Got {0}", result2);
Comment 2 Martin Frydl 2017-03-31 18:38:48 UTC
Created attachment 21115 [details]

It seems like the problem is mono_w32handle_wait_multiple() where all signaled handles are acquired, not just the first one. The attached patch fixes both test cases.
Comment 3 adrian.eggenberger 2017-04-01 11:58:54 UTC
This sounds like the right explanation for the behavior. So there is hope it will be fixed in upcoming releases, right?

Note You need to log in before you can comment on or make changes to this bug.