Subject: | Net::ZooKeeper consumes 100% cpu on wait |
Date: | Sun, 12 Sep 2010 17:59:49 -0700 |
To: | bug-Net-ZooKeeper [...] rt.cpan.org |
From: | Syam <shyamx [...] gmail.com> |
Net::ZooKeeper consumes 100% cpu when "wait" is used. At my
initial inspection, it seems to be related to implementation mistake
in pthread_cond_timedwait. The following patch fixes the issue.
Please review and let me know if it looks correct.
To reproduce:
#!/usr/bin/perl
use strict;
use warnings;
use Net::ZooKeeper qw/:errors :node_flags :acls/;
my $zk_node = "localhost:2181";
my $zkh = Net::ZooKeeper::->new($zk_node) or die "$!";
my $watch = $zkh->watch();
my @locks = $zkh->exists('/foo',watch=>$watch);
$watch->wait(timeout=>'10000');
The above script takes 100% cpu.
Proposed patch:
[syam@outercross]~/zookeeper-3.3.1/src/contrib/zkperl% diff -u ZooKeeper.xs
zookeeper-3.3.1/contrib/zkperl/ZooKeeper.xs
--- ZooKeeper.xs 2010-09-12 17:49:02.000000000 -0700
+++ zookeeper-3.3.1/contrib/zkperl/ZooKeeper.xs 2010-05-07
10:15:32.000000000 -0700
@@ -2638,7 +2638,7 @@
gettimeofday(&curr_timeval, NULL);
- wait_timespec.tv_sec = end_timeval.tv_sec;
+ wait_timespec.tv_sec = end_timeval.tv_sec -
curr_timeval.tv_sec;
wait_timespec.tv_nsec =
(end_timeval.tv_usec - curr_timeval.tv_usec) * 1000;
@@ -2652,9 +2652,8 @@
break;
}
- if (pthread_cond_timedwait(&watch->cond, &watch->mutex,
- &wait_timespec) == ETIMEDOUT)
- break;
+ pthread_cond_timedwait(&watch->cond, &watch->mutex,
+ &wait_timespec);
}
done = watch->done;