Bug#261237: NPTL: pthread_cond_timedwait with timespec in the past destroys cond variable

17 views
Skip to first unread message

Hubert Schmid

unread,
Jul 24, 2004, 9:10:09 AM7/24/04
to
Package: libc6
Version: 2.3.2.ds1-13
Severity: normal

pthread_cond_timedwait with a timespec in the past sometimes destroys
the pthread_cond_t variable. The following pthread_cond_(timed)wait
calls ignore all signals on the condition variable and block.

There is no Workaround, because I cannot guarantee that the timespec is
really in the future (because the thread may be interrupted between
gettimeofday and pthread_cond_timedwait).

The problem only occurs with NPTL (i.e. I can't reproduce the problem
with LD_ASSUME_KERNEL=2.4.19).

The following program deadlocks after some iterations (10-1000).

*** /tmp/current/bug.c
// gcc -pthread -Wall -O2 -std=c99 bug.c -o bug

#include <stdio.h>
#include <pthread.h>

pthread_mutex_t mutex;
pthread_cond_t empty;
pthread_cond_t nonempty;
unsigned count = 0;

void* run(void* env)
{
pthread_mutex_lock(&mutex);
struct timespec ts = { 0, 0 };
for (;;) {
count = 0;
pthread_cond_signal(&empty);
pthread_cond_timedwait(&nonempty, &mutex, &ts);
count = 0;
pthread_cond_signal(&empty);
pthread_cond_wait(&nonempty, &mutex);
}
}

int main()
{
pthread_mutex_init(&mutex, 0);
pthread_cond_init(&empty, 0);
pthread_cond_init(&nonempty, 0);

pthread_t thread;
pthread_create(&thread, 0, &run, 0);

pthread_mutex_lock(&mutex);
for (unsigned i = 0;; ++i) {
while (count > 0) {
pthread_cond_wait(&empty, &mutex);
}
++count;
pthread_cond_signal(&nonempty);
printf("%u\n", i);
}
}

-- System Information:
Debian Release: testing/unstable
APT prefers unstable
APT policy: (500, 'unstable')
Architecture: i386 (i686)
Kernel: Linux 2.6.7-1-686-smp
Locale: LANG=C, LC_CTYPE=de_DE@euro

Versions of packages libc6 depends on:
ii libdb1-compat 2.1.3-7 The Berkeley database routines [gl

-- no debconf information

--
To UNSUBSCRIBE, email to debian-bugs-...@lists.debian.org
with a subject of "unsubscribe". Trouble? Contact listm...@lists.debian.org

GOTO Masanori

unread,
Jul 25, 2004, 11:30:09 AM7/25/04
to
tags 261237 confirmed fixed-upstream
thanks

At Sat, 24 Jul 2004 13:59:32 +0200,


Hubert Schmid wrote:
> pthread_cond_timedwait with a timespec in the past sometimes destroys
> the pthread_cond_t variable. The following pthread_cond_(timed)wait
> calls ignore all signals on the condition variable and block.
>
> There is no Workaround, because I cannot guarantee that the timespec is
> really in the future (because the thread may be interrupted between
> gettimeofday and pthread_cond_timedwait).
>
> The problem only occurs with NPTL (i.e. I can't reproduce the problem
> with LD_ASSUME_KERNEL=2.4.19).
>
> The following program deadlocks after some iterations (10-1000).

I tested and I confirmed your test code goes sometimes deadlock. But
the latest nptl are not blocked. I tagged it as fixed-upstream.

Regards,
-- gotom

Reply all
Reply to author
Forward
0 new messages