pocoproject / poco

The POCO C++ Libraries are powerful cross-platform C++ libraries for building network- and internet-based applications that run on desktop, server, mobile, IoT, and embedded systems.
https://pocoproject.org
Other
8.34k stars 2.15k forks source link

Event_POSIX.cpp waitImpl(long) returns immediately, pthread_cond_timedwait err 110 #1896

Open sil-mk opened 7 years ago

sil-mk commented 7 years ago

I have built the latest poco 1.7.9 for Android using the latest ndk for Android r15c with exported standalone toolchain for api 21. The same thing happens with the standalone toolchain for api 23. In my code I am creating a Poco::Data::Session for SQLite and from the SessionImpl.cpp on some android phones I get ConnectionFailedException("Timed out.");. I have debugged this issue to some extent and concluded the following: From the waitImpl(long) in Event_POSIX.cpp the pthread_cond_timedwait returns immediately with value 110. Because of this, if the session for the database does not manage to be created faster than the call towards pthread_cond_timedwait you get exception "Timed out". I have tried changing the timeout but the result is the same. I am using an encrypted databases (with sqlcipher) which take longer to open. On some older android phones like the htc m9 since the CPU is slower 95% of the time it manages to open the database. On newer phones like samsung s7 and s8 99% of the time it fails with "Timed out".

sil-mk commented 7 years ago

Just to clarify. The same code works on linux-x86_64 with poco 1.7.9. Also worth to mention is that I am upgrading my static poco libraries from 1.4.6 to 1.7.9 and on 1.7.9 the SQLite db sessions are being created slower compared to the old libraries (for the same databases).

sil-mk commented 7 years ago

I tried rebuilding the libraries with the older ndk-r10e toolchain but the result is the same. After some debugging I have concluded that the problem is with clock_gettime(CLOCK_MONOTONIC, &abstime); or possibly the initialization of the condition for the MONOTONIC case. If instead of MONOTONIC and clock_gettime we use gettimeofday it works without problem. So I have replaced the Event_POSIX.cpp in Poco 1.7.9 with the one from 1.4.6 where gettimeofday is used for android and the problem is solved.

aleks-f commented 7 years ago

@sil-mk something along those lines was done in #1403 . can you check and see whether/how it relates to your issue?

sil-mk commented 7 years ago

Despite setting the clock to be CLOCK_MONOTONIC with pthread_condattr_setclock, the pthread_cond_timedwait does not work with monotonic time. For some reason it expects time since unix epoch and not time since boot (as it should be with the monotonic). I also tried with the CLOCK_MONOTONIC_RAW the result is the same, it does not wait for the specified timeout.

aleks-f commented 6 years ago

I currently have no time nor platform setup to do something here. @RangelReale can you help with this?

@sil-mk if you'd like to see the fix in 1.8, please find a way to provide fix.