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

AFD failure now sources underlying I/O error #175

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 58 additions & 8 deletions src/iocp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,19 @@ impl Poller {
pub(super) fn new() -> io::Result<Self> {
// Make sure AFD is able to be used.
if let Err(e) = afd::NtdllImports::force_load() {
return Err(crate::unsupported_error(format!(
"Failed to initialize unstable Windows functions: {}\nThis usually only happens for old Windows or Wine.",
e
)));
return Err(io::Error::new(
io::ErrorKind::Unsupported,
AfdError::new("failed to initialize unstable Windows functions", e),
));
}

// Create and destroy a single AFD to test if we support it.
Afd::<Packet>::new().map_err(|e| crate::unsupported_error(format!(
"Failed to initialize \\Device\\Afd: {}\nThis usually only happens for old Windows or Wine.",
e,
)))?;
Afd::<Packet>::new().map_err(|e| {
io::Error::new(
io::ErrorKind::Unsupported,
AfdError::new("failed to initialize \\Device\\Afd", e),
)
})?;

let port = IoCompletionPort::new(0)?;
tracing::trace!(handle = ?port, "new");
Expand Down Expand Up @@ -1326,6 +1328,54 @@ fn dur2timeout(dur: Duration) -> u32 {
.unwrap_or(INFINITE)
}

/// An error type that wraps around failing to open AFD.
struct AfdError {
/// String description of what happened.
description: &'static str,

/// The underlying system error.
system: io::Error,
}

impl AfdError {
#[inline]
fn new(description: &'static str, system: io::Error) -> Self {
Self {
description,
system,
}
}
}

impl fmt::Debug for AfdError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AfdError")
.field("description", &self.description)
.field("system", &self.system)
.field("note", &"probably caused by old Windows or Wine")
.finish()
}
}

impl fmt::Display for AfdError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {}\nThis error is usually caused by running on old Windows or Wine",
self.description, &self.system
)
}
}

impl std::error::Error for AfdError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.system)
}
}

struct CallOnDrop<F: FnMut()>(F);

impl<F: FnMut()> Drop for CallOnDrop<F> {
Expand Down