-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Update naming around TcpListener::from_std so that it's harder to misuse #5597
Draft
Ten0
wants to merge
7
commits into
tokio-rs:master
Choose a base branch
from
Ten0:5595_better_listener_from_std
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4262749
wip
Ten0 05dc864
improve wording
Ten0 5c19066
Merge remote-tracking branch 'upstream/master' into 5595_better_liste…
Ten0 9e99027
comments and naming improvements
Ten0 b80291d
finish for TcpListener
Ten0 37c5af5
actually put the check in from_std_unchecked
Ten0 67078dd
make it explicit with regards to what is unchecked
Ten0 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. | ||
//! | ||
//! These are displayed warnings in debug mode, panics in test mode | ||
//! (so that nothing slips through in the tokio test suite), and no-ops in release mode. | ||
|
||
#[cfg(all(debug_assertions, not(test)))] | ||
/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. | ||
/// | ||
/// These are displayed warnings in debug mode, panics in test mode | ||
/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. | ||
macro_rules! debug_check_non_blocking { | ||
($std_socket: expr, $method: expr, $fallback_method: expr) => {{ | ||
// Make sure the provided item is in non-blocking mode, otherwise warn. | ||
static HAS_WARNED_BLOCKING: std::sync::atomic::AtomicBool = | ||
std::sync::atomic::AtomicBool::new(false); | ||
match socket2::SockRef::from(&$std_socket).nonblocking() { | ||
Ok(true) => {} | ||
Ok(false) => { | ||
if !HAS_WARNED_BLOCKING.swap(true, std::sync::atomic::Ordering::Relaxed) { | ||
println!(concat!( | ||
"WARNING: `", | ||
$method, | ||
"` was called on a socket that is \ | ||
not in non-blocking mode. This is unexpected, and may cause the \ | ||
thread to block indefinitely. Use `", | ||
$fallback_method, | ||
"` instead." | ||
)); | ||
} | ||
} | ||
Err(io_error) => { | ||
if !HAS_WARNED_BLOCKING.swap(true, std::sync::atomic::Ordering::Relaxed) { | ||
println!( | ||
concat!( | ||
"WARNING: `", | ||
$method, | ||
"` was called on a socket which we \ | ||
could not determine whether was in non-blocking mode: {}" | ||
), | ||
io_error | ||
); | ||
} | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
#[cfg(test)] | ||
/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. | ||
/// | ||
/// These are displayed warnings in debug mode, panics in test mode | ||
/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. | ||
macro_rules! debug_check_non_blocking { | ||
($std_socket: expr, $method: expr, $fallback_method: expr) => {{ | ||
match socket2::SockRef::from(&$std_socket).nonblocking() { | ||
Ok(true) => {} | ||
Ok(false) => { | ||
panic!(concat!( | ||
$method, | ||
"` was called on a socket that is \ | ||
not in non-blocking mode. This is unexpected, and may cause the \ | ||
thread to block indefinitely. Use `", | ||
$fallback_method, | ||
"` instead." | ||
)) | ||
} | ||
Err(io_error) => { | ||
panic!( | ||
concat!( | ||
$method, | ||
"` was called on a socket which we \ | ||
could not determine whether was in non-blocking mode: {}" | ||
), | ||
io_error | ||
); | ||
} | ||
} | ||
}}; | ||
} | ||
|
||
#[cfg(not(debug_assertions))] | ||
/// Debug assertions that from_std_assume_nonblocking is not used on blocking sockets. | ||
/// | ||
/// These are displayed warnings in debug mode, panics in test mode | ||
/// (so that nothing slips through in the tokio test suite), and no-ops in release mode. | ||
macro_rules! debug_check_non_blocking { | ||
($($tts:tt)+) => {}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should fix it on all IO types if we're fixing it on one of them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aha the trap ^^
Does the wording seem good now? (Before I update them all following the same pattern.)
There's something that makes me feel a but uneasy however: it seems like the naming doesn't make as much sense as
from_std
did...from_tcp
doesn't feel explanatory enough since there is TcpStream, TcpListener...Now to fix that we could name it
TcpStream::from_tcp_stream
, however it is not as obvious in what it does asTcpFile::from_std
.With pipe there was only File so
Pipe::from_file
is explanatory enough. Also, the fact you're asserting that the file is a pipe is the main thing you're interested in in that context so it makes sense that it's named this way.However with our function the main thing we're interested in about that function is that it goes from std to tokio.
=> How about naming it
TcpFile::from_std_checked
? That's more verbose, but at least it's more self-explanatory, you understand it right away when reading code that uses it, and it makes it pretty clear that it should be renamed tofrom_std
in the next major release.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right that it's a worse name than
from_std
. Your other suggestions could also work, but I don't think that they are clearly better thanfrom_tcp
.