Merge lp:~percona-toolkit-dev/percona-toolkit/pt-heartbeat-crashes-with-sleep-argument-1406390 into lp:~percona-toolkit-dev/percona-toolkit/release-2.2.14

Proposed by Frank Cizmich
Status: Merged
Approved by: Daniel Nichter
Approved revision: 628
Merged at revision: 617
Proposed branch: lp:~percona-toolkit-dev/percona-toolkit/pt-heartbeat-crashes-with-sleep-argument-1406390
Merge into: lp:~percona-toolkit-dev/percona-toolkit/release-2.2.14
Diff against target: 42 lines (+12/-6)
1 file modified
bin/pt-heartbeat (+12/-6)
To merge this branch: bzr merge lp:~percona-toolkit-dev/percona-toolkit/pt-heartbeat-crashes-with-sleep-argument-1406390
Reviewer Review Type Date Requested Status
Daniel Nichter Approve
Review via email: mp+254662@code.launchpad.net

Description of the change

Problem:
pt-heartbeat was reported to fail sometimes when using --sleep option
Although unable to reproduce the code clearly showed a potential race condition where:
1) time function was tested to be over a certain fixed interval
2) a loop was entered waiting for next interval
3) on exiting the loop a sleep is issued with a potentially negative value due to the clock ticks transpiring after the exit of the loop

Solution:
Save the time value in a variable and use that value in the code to guarantee a positive value for the sleep function

To post a comment you must log in.
Revision history for this message
Daniel Nichter (daniel-nichter) wrote :

Code comments are always helpful for the next person, e.g. in case someone runs into this code and wonders why the time is being saved in another var. Can also mention the bug/jira issue URL. Otherwise, code is fine.

review: Approve
629. By Frank Cizmich

pt-heartbeat added comment for issue 1406390 fix

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/pt-heartbeat'
2--- bin/pt-heartbeat 2015-01-23 10:19:56 +0000
3+++ bin/pt-heartbeat 2015-04-06 21:22:41 +0000
4@@ -5286,13 +5286,16 @@
5 ) {
6 eval {
7 my $next_interval = $get_next_interval->();
8- if ( time >= $next_interval ) {
9+ # save current time in variable to avoid race condition
10+ # https://bugs.launchpad.net/percona-toolkit/+bug/1406390
11+ my $time = time;
12+ if ( $time >= $next_interval ) {
13 do { $next_interval = $get_next_interval->() }
14- until $next_interval > time;
15+ until $next_interval > $time;
16 PTDEBUG && _d("Missed last interval; next interval:",
17 ts($next_interval));
18 }
19- sleep $next_interval - time;
20+ sleep $next_interval - $time;
21 PTDEBUG && _d('Woke up at', ts(time));
22
23 # Connect or reconnect if necessary.
24@@ -5419,13 +5422,16 @@
25 PTDEBUG && _d('Checking slave', $dp->as_string($dsn));
26
27 my $next_interval = $get_next_interval->();
28- if ( time >= $next_interval ) {
29+ # save current time in variable to avoid race condition
30+ # https://bugs.launchpad.net/percona-toolkit/+bug/1406390
31+ my $time = time;
32+ if ( $time >= $next_interval ) {
33 do { $next_interval = $get_next_interval->() }
34- until $next_interval > time;
35+ until $next_interval > $time;
36 PTDEBUG && _d("Missed last interval; next interval:",
37 ts($next_interval));
38 }
39- sleep $next_interval - time;
40+ sleep $next_interval - $time;
41 PTDEBUG && _d('Woke up at', ts(time));
42 my ($delay, $hostname, $master_server_id) = $get_delay->($sth);
43

Subscribers

People subscribed via source and target branches

to all changes: