[Mono-dev] EventWaitHandle.WaitOne #2

Mirko Wischer Mirko.Wischer at sma.de
Tue Apr 10 08:58:04 UTC 2012


in january I wrote to the mailing list about a problem

in EventWaitHandle.WaitOne because WaitForSingleObjectEx

does not use a monotonic clock.

I also wrote a bug report:


Until a week ago I thought this only happens on named events.

But now I realized the problem also exists using unnamed events

under some circumstances (One thread changes time just after another

thread called WaitOne. I think my patch should also cover this error

but I did not test it yet. First I would like to know if my patch seems

to be okay or if I missed some possible race conditions.

Regards, Mirko

My old posting:

Hi everyone,


we hit a problem concerning EventWaitHandle.WaitOne(timeout) in
conjunction with

changing system time during long timeouts.

The problem only exists using named events. After a short analysis I
think I found the main problem:


In the io-layer  the WaitForSingleObjectEx method uses
_wapi_calc_timeout (from misc.c) to compute

an absolute time from the given timeout, but the absolute timeout is
computed using “gettimeofday”.

That will cause problems if the system clock changes during checking
(named events spin through fake timeouts

until the absolute timeout seems to be reached).

                Unnamed events also use the _wapi_calc_timeout method to
compute the absolute timeout. This timeout is

then used in mono_cond_timedwait (but this only wraps “normal” pthread

So I prepared a patch (see attached files) using
clock_gettime(CLOCK_MONOTONIC,..) instead of gettimeofday, but this also

a change for unnamed events, because the pthreads condition needs to
know what kind of clock provides the

absolute timeout.


This seems to fix the issue for us.  Is this an appropriate way to fix
this? What do you think.

_wapi_calc_timeout is also used in some other timeout relevant
functions, but I just found

the “faketimeout” way of checking the absolute timeout and the

Or do I miss some occurrences/cases checking the absolute timeout.


Thanks for looking at this.


CU Mirko


SMA Solar Technology AG
Aufsichtsrat: Guenther Cramer (Vorsitzender)
Vorstand: Juergen Dolle, Roland Grebe, Pierre-Pascal Urbon, Marko Werner
Handelsregister: Amtsgericht Kassel HRB 3972
Sitz der Gesellschaft: 34266 Niestetal
USt-ID-Nr. DE 113 08 59 54
WEEE-Reg.-Nr. DE 95881150

More information about the Mono-devel-list mailing list