Notification DeQueue Timeout crashes when the system time changes

General discussion regarding the development of POCO for contributors.
akhilpaleri
Posts: 1
Joined: 10 Aug 2016, 14:15

Notification DeQueue Timeout crashes when the system time changes

Postby akhilpaleri » 10 Aug 2016, 14:38

Issue

It has been noticed that the below API call hangs if the system time is set backwards. A typical use case for backward time rollover is to update the timezone from IST to PST.

waitDequeueNotification Notification * waitDequeueNotification( long milliseconds );

Library: Foundation
Package: Notifications
Header: Poco/NotificationQueue.h

POCO Version: 1.4.7-all
OS: Linux
Distribution: Ubuntu 14.04 LTS

Analysis

The root cause has been tracked down to the usage of gettimeofday(&tv, NULL); function.

Proposed Resolution

Please find a possible fix that can resolve this issue. Could any one of you please comment on this issue and the patch.

========================================================================================================================
Index: packages/poco-1.4.7-all/Foundation/src/Event_POSIX.cpp
===================================================================
--- packages.orig/poco-1.4.7-all/Foundation/src/Event_POSIX.cpp 2014-10-31 14:07:58.000000000 +0530
+++ packages/poco-1.4.7-all/Foundation/src/Event_POSIX.cpp 2016-08-10 12:24:44.895235977 +0530
@@ -35,6 +35,7 @@


#include "Poco/Event_POSIX.h"
+#include <stdio.h>
#if defined(POCO_VXWORKS)
#include <timers.h>
#include <cstring>
@@ -57,8 +58,18 @@
#endif
if (pthread_mutex_init(&_mutex, NULL))
throw SystemException("cannot create event (mutex)");
- if (pthread_cond_init(&_cond, NULL))
+ if (pthread_condattr_init(&attr)) {
+ printf("pthread_condattr_init failed");
+ throw SystemException("cannot create event (condition attr)");
+ }
+ if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) {
+ printf("pthread_condattr_setclock failed");
+ throw SystemException("cannot set (condition attr clock)");
+ }
+ if (pthread_cond_init(&_cond, &attr)) {
+ printf("pthread_cond_init failed");
throw SystemException("cannot create event (condition)");
+ }
}


@@ -107,10 +118,13 @@
abstime.tv_sec++;
}
#else
- struct timeval tv;
- gettimeofday(&tv, NULL);
- abstime.tv_sec = tv.tv_sec + milliseconds / 1000;
- abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
+ //struct timeval tv;
+ //gettimeofday(&tv, NULL);
+ //abstime.tv_sec = tv.tv_sec + milliseconds / 1000;
+ //abstime.tv_nsec = tv.tv_usec*1000 + (milliseconds % 1000)*1000000;
+ clock_gettime(CLOCK_MONOTONIC, &abstime);
+ abstime.tv_sec += milliseconds / 1000;
+ abstime.tv_nsec += (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
Index: packages/poco-1.4.7-all/Foundation/include/Poco/Event_POSIX.h
===================================================================
--- packages.orig/poco-1.4.7-all/Foundation/include/Poco/Event_POSIX.h 2014-10-31 14:07:57.000000000 +0530
+++ packages/poco-1.4.7-all/Foundation/include/Poco/Event_POSIX.h 2016-08-10 11:18:45.639448147 +0530
@@ -64,6 +64,7 @@
volatile bool _state;
pthread_mutex_t _mutex;
pthread_cond_t _cond;
+ pthread_condattr_t attr;
};

==========================================================================================================================




Thanks

Akhil

Return to “Contributors”

Who is online

Users browsing this forum: No registered users and 1 guest