Skip to content

std: Fix sub-second Condvar::wait_timeout_ms #27373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2015

Conversation

alexcrichton
Copy link
Member

The API we're calling requires us to pass an absolute point in time as an
argument (pthread_cond_timedwait) so we call gettimeofday ahead of time to
then add the specified duration to. Unfortuantely the current "add the duration"
logic forgot to take into account the current time's sub-second precision (e.g.
the tv_usec field was ignored), causing sub-second duration waits to return
spuriously.

@rust-highfive
Copy link
Contributor

r? @nikomatsakis

(rust_highfive has picked a reviewer for you, use r? to override)

@alexcrichton
Copy link
Member Author

cc @carllerche

r? @brson

Also nominating for beta as this is a somewhat serious bug

@rust-highfive rust-highfive assigned brson and unassigned nikomatsakis Jul 29, 2015
@alexcrichton alexcrichton added beta-nominated Nominated for backporting to the compiler in the beta channel. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jul 29, 2015
@alexcrichton
Copy link
Member Author

cc @sfackler

let seconds = dur.secs() as libc::time_t;
let timeout = match sys_now.tv_sec.checked_add(seconds) {

let timeout = match (sys_now.tv_sec + extra).checked_add(seconds) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one checked_add here, not 2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured that gettimeofday won't return a value that is 1 away from overflowing, but we don't control seconds + sys_now.tv_sec so that still needs to be checked.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will if you set your system clock that way :P

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @sfackler. The bad input is possible and should be accounted for.

@brson brson added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jul 29, 2015
The API we're calling requires us to pass an absolute point in time as an
argument (`pthread_cond_timedwait`) so we call `gettimeofday` ahead of time to
then add the specified duration to. Unfortuantely the current "add the duration"
logic forgot to take into account the current time's sub-second precision (e.g.
the `tv_usec` field was ignored), causing sub-second duration waits to return
spuriously.
@alexcrichton alexcrichton force-pushed the fix-wait-timeout-ms branch from 9803374 to 43b2c47 Compare July 29, 2015 17:25
@alexcrichton
Copy link
Member Author

Updated to use checked_add on both additions

@brson
Copy link
Contributor

brson commented Jul 29, 2015

@bors: r+

@bors
Copy link
Collaborator

bors commented Jul 29, 2015

📌 Commit 43b2c47 has been approved by brson

Manishearth added a commit to Manishearth/rust that referenced this pull request Jul 29, 2015
…=brson

The API we're calling requires us to pass an absolute point in time as an
argument (`pthread_cond_timedwait`) so we call `gettimeofday` ahead of time to
then add the specified duration to. Unfortuantely the current "add the duration"
logic forgot to take into account the current time's sub-second precision (e.g.
the `tv_usec` field was ignored), causing sub-second duration waits to return
spuriously.
@brson brson mentioned this pull request Jul 29, 2015
@alexcrichton
Copy link
Member Author

@bors: p=1 (merging to beta)

bors added a commit that referenced this pull request Jul 29, 2015
@bors bors merged commit 43b2c47 into rust-lang:master Jul 29, 2015
@brson brson added beta-accepted Accepted for backporting to the compiler in the beta channel. and removed beta-accepted Accepted for backporting to the compiler in the beta channel. beta-nominated Nominated for backporting to the compiler in the beta channel. labels Jul 30, 2015
@alexcrichton alexcrichton deleted the fix-wait-timeout-ms branch August 17, 2015 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants