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 ()
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)

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

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

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report for Bug 53078 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

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?