Skip to content
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

util: add track_caller to public APIs #4785

Merged
merged 2 commits into from Jun 27, 2022
Merged

Conversation

hds
Copy link
Contributor

@hds hds commented Jun 22, 2022

Motivation

When a user of tokio calls a function that panics when misused (e.g. calling
DelayQueue::insert_at with a an instant 100 years in the future) then the user
currently sees the line number of the panic call inside tokio. It would be more
informative for the user to see the place where they called the panicking function.

It is still possible for the user to see the full stack trace by setting the
environment variable RUST_BACKLOG=1, so no useful information is
hidden.

This change is the second step in closing #4413 (after #4772), this change
is for the tokio-util crate.

Note that there was an earlier PR (#4445) to work on this issue for the tokio-util
crate, but it hasn't seen much work recently and after looking into it, I found that
these change in the main crate are necessary prerequisites.

Solution

Functions that may panic can be annotated with #[track_caller] so that
in the event of a panic, the function where the user called the
panicking function is shown instead of the file and line within Tokio
source.

This change adds #[track_caller] to all the non-unstable public APIs in
tokio-util where the documentation describes how the function may panic
due to incorrect context or inputs.

In one place, an assert was added where the described behavior appeared
not to be implemented. The documentation for DelayQueue::reserve
states that the function will panic if the new capacity exceeds the
maximum number of entries the queue can contain. However, the function
didn't panic until a higher number caused by an allocation failure. This
is inconsistent with DelayQueue::insert_at which will panic if the
number of entries were to go over MAX_ENTRIES.

Tests are included to cover each potentially panicking function.

Refs: #4413

@github-actions github-actions bot added the R-loom Run loom tests on this PR label Jun 22, 2022
@hds hds marked this pull request as draft June 22, 2022 10:22
@hds hds force-pushed the track-caller-util branch 3 times, most recently from 79ac466 to 5b39b1f Compare June 22, 2022 12:19
Functions that may panic can be annotated with `#[track_caller]` so that
in the event of a panic, the function where the user called the
panicking function is shown instead of the file and line within Tokio
source.

This change adds `#[track_caller]` to all the non-unstable public APIs in
tokio-util where the documentation describes how the function may panic
due to incorrect context or inputs.

In one place, an assert was added where the described behavior appeared
not to be implemented. The documentation for `DelayQueue::reserve`
states that the function will panic if the new capacity exceeds the
maximum number of entries the queue can contain. However, the function
didn't panic until a higher number caused by an allocation failure. This
is inconsistent with `DelayQueue::insert_at` which will panic if the
number of entries were to go over MAX_ENTRIES.

Tests are included to cover each potentially panicking function.

Refs: tokio-rs#4413
@Darksonn Darksonn added the A-tokio-util Area: The tokio-util crate label Jun 22, 2022
@Darksonn
Copy link
Contributor

This PR is marked as a draft. Please let me know when you would like it to be reviewed, and if you have any other questions.

Some tests were failing on FreeBSD 32-bit because the "times too far in
the future" for DelayQueue were also too far in the future for the OS.

Fixed by copying the MAX_DURATION value from where it's defined and
using it to create a duration that is just 1 more than the maximum. This
will start to break once we get close (within 2 and a bit years) of the
Epochalypse (19 Jan, 2038) - but a lot of other things are going to be
breaking on FreeBSD 32-bit by then anyway.
@hds hds marked this pull request as ready for review June 22, 2022 15:26
@hds
Copy link
Contributor Author

hds commented Jun 22, 2022

@Darksonn It's ready now. I realized that I had some broken tests on FreeBSD 32-bit and marked the PR a draft until I could fix them as I wasn't sure how complicated it was going to be.

Thanks for your help!

Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

LGTM

@Darksonn Darksonn merged commit ad412a9 into tokio-rs:master Jun 27, 2022
@hds hds deleted the track-caller-util branch July 21, 2022 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate R-loom Run loom tests on this PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants