Skip to content

net: debug_assert on creating a tokio socket from a blocking one#7166

Merged
Noah-Kennedy merged 23 commits into
masterfrom
noah/warnings
Mar 5, 2025
Merged

net: debug_assert on creating a tokio socket from a blocking one#7166
Noah-Kennedy merged 23 commits into
masterfrom
noah/warnings

Conversation

@Noah-Kennedy
Copy link
Copy Markdown
Contributor

@Noah-Kennedy Noah-Kennedy commented Feb 20, 2025

See #5595 and #7172.

This adds a debug assertion that checks that a supplied underlying std socket is set to nonblocking mode when constructing a tokio socket object from such an object.

This only works on unix.

@Noah-Kennedy
Copy link
Copy Markdown
Contributor Author

Windows does not support this, so it only works on unix for now.

@Noah-Kennedy Noah-Kennedy changed the title net: warn on creating a tokio socket from a nonblocking one. net: warn on creating a tokio socket from a nonblocking one Feb 20, 2025
@Darksonn Darksonn requested a review from carllerche February 20, 2025 08:00
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-net Module: tokio/net labels Feb 20, 2025
@Noah-Kennedy Noah-Kennedy changed the title net: warn on creating a tokio socket from a nonblocking one net: warn on creating a tokio socket from a blocking one Feb 20, 2025
Comment thread tokio/src/util/blocking_check.rs Outdated
Copy link
Copy Markdown

@Ten0 Ten0 left a comment

Choose a reason for hiding this comment

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

Thanks for getting this to move forward!

Is from_std_set_nonblocking/from_std_assume_nonblocking still the long term plan?
image
Or will it instead be something where tokio sets it to nonblocking, like axum_server currently does?
Or will the panic be considered to be enough?

@Noah-Kennedy
Copy link
Copy Markdown
Contributor Author

Thanks for getting this to move forward!

Is from_std_set_nonblocking/from_std_assume_nonblocking still the long term plan? image Or will it instead be something where tokio sets it to nonblocking, like axum_server currently does? Or will the panic be considered to be enough?

I think there's a bit that needs figured out still about what we do going forward. The trouble with the prior suggestion is that we actually still have the issue of TryFrom to figure out - we can't exactly deprecate that, and we need to figure out whether what it's behavior will be.

I think our two long-term options here are going to be to either panic, or set the socket option silently, and there's good arguments to be made for both. There should also be something along the lines of from_std_unchecked or from_std_assume_nonblocking, of course.

Given that we either need to leave the footgun in TryFrom or make a behavioral change anyways, I don't think leaving from_std as is makes much sense.

@Noah-Kennedy Noah-Kennedy enabled auto-merge (squash) February 20, 2025 21:51
Copy link
Copy Markdown
Member

@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.

This also needs review from Carl before it can be merged.

Comment thread tokio/src/util/mod.rs Outdated
Comment thread tokio/src/util/blocking_check.rs Outdated
Comment thread tokio/src/net/tcp/listener.rs Outdated
Comment thread tokio/src/util/blocking_check.rs
Comment thread tokio/src/net/tcp/listener.rs Outdated
use crate::net::{to_socket_addrs, ToSocketAddrs};
}

use crate::util::blocking_check::check_socket_for_blocking;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Very minor and not a needed change to merge, but IMO this check makes more sense in the net module.

Comment thread tokio/src/util/blocking_check.rs Outdated
@Noah-Kennedy
Copy link
Copy Markdown
Contributor Author

@carllerche @Darksonn I have updated the PR and addressed the feedback. This is ready for re-review.

Noah-Kennedy and others added 8 commits March 1, 2025 10:39
See #5595.

This adds a warning when constructing a tokio socket object from an underlying std socket that is not set to nonblocking mode.

This warning is likely to incrementally turn into a hard error in the future.
@Noah-Kennedy Noah-Kennedy requested a review from carllerche March 1, 2025 16:40
@Noah-Kennedy Noah-Kennedy changed the title net: warn on creating a tokio socket from a blocking one net: debug_assert on creating a tokio socket from a blocking one Mar 3, 2025
@Noah-Kennedy
Copy link
Copy Markdown
Contributor Author

from offline discussion with @Darksonn: need to wrap the lines on the docs to not be super long

this is good to merge after that is done

@Noah-Kennedy Noah-Kennedy disabled auto-merge March 4, 2025 01:44
@Noah-Kennedy Noah-Kennedy enabled auto-merge (squash) March 4, 2025 01:45
@Noah-Kennedy Noah-Kennedy disabled auto-merge March 4, 2025 01:45
@Noah-Kennedy Noah-Kennedy enabled auto-merge (squash) March 4, 2025 01:45
Copy link
Copy Markdown
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

Small proposal for the panic message.

Comment thread tokio/src/util/blocking_check.rs Outdated
@Noah-Kennedy Noah-Kennedy disabled auto-merge March 5, 2025 02:30
@Noah-Kennedy Noah-Kennedy enabled auto-merge (squash) March 5, 2025 02:31
Copy link
Copy Markdown
Member

@carllerche carllerche left a comment

Choose a reason for hiding this comment

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

LGTM thanks

@Noah-Kennedy Noah-Kennedy merged commit 042433c into master Mar 5, 2025
@Noah-Kennedy Noah-Kennedy deleted the noah/warnings branch March 5, 2025 18:10
prabirshrestha added a commit to prabirshrestha/salvo that referenced this pull request Mar 9, 2025
required due to latest version of tokio panicking if not set.
tokio-rs/tokio#7166
chrislearn pushed a commit to salvo-rs/salvo that referenced this pull request Mar 9, 2025
required due to latest version of tokio panicking if not set.
tokio-rs/tokio#7166
josecelano added a commit to torrust/torrust-hash2torrent that referenced this pull request Dec 22, 2025
… axum-server

8cdb2d9 fix: set TcpListener to non-blocking mode before passing to axum-server (Jose Celano)

Pull request description:

  ## Description

  This PR fixes the Tokio runtime panic that occurs when running the application with `cargo run`.

  ## Problem

  The application was panicking with:
  ```
  Registering a blocking socket with the tokio runtime is unsupported. If you wish to do anyways, please add `--cfg tokio_allow_from_blocking_fd` to your RUSTFLAGS.
  ```

  ## Root Cause

  - Tokio 1.44.0 introduced stricter validation to prevent blocking file descriptors from being registered with the async runtime
  - `std::net::TcpListener::bind()` creates blocking sockets by default
  - axum-server 0.8.0 internally uses `tokio::net::TcpListener::from_std()` which panics when it detects a blocking socket

  ## Solution

  Set the `TcpListener` to non-blocking mode **before** passing it to axum-server by calling `socket.set_nonblocking(true)` immediately after binding.

  ## Changes

  - ✅ Added `socket.set_nonblocking(true)` in `src/api/mod.rs` after binding the TcpListener
  - ✅ Removed the incorrect RUSTFLAGS workaround from README.md troubleshooting section
  - ✅ Deleted temporary TOKIO_BLOCKING_SOCKET_FIX.md file

  ## Benefits

  - Proper fix instead of masking the symptom with RUSTFLAGS
  - Prevents potential performance issues under load
  - Application now runs correctly with just `cargo run`

  ## Testing

  Verified that the application starts successfully without any panic:
  ```bash
  cargo run
  ```

  Fixes #8

  ## References

  - [Tokio Issue #7172](tokio-rs/tokio#7172)
  - [Tokio PR #7166](tokio-rs/tokio#7166)

ACKs for top commit:
  josecelano:
    ACK 8cdb2d9

Tree-SHA512: 12653e0a924f03fb6e43d9eb381aec6e593227f2a095ba681b952c3851ed09575a2a5cbca2b0fd6d0647d9c777d4a28418bc51e768568f618457614c34b02b15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-tokio Area: The main tokio crate M-net Module: tokio/net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants