Skip to content

fix: set TcpListener to non-blocking mode before passing to axum-server#9

Merged
josecelano merged 1 commit intomainfrom
fix/issue-8-tokio-blocking-socket
Dec 22, 2025
Merged

fix: set TcpListener to non-blocking mode before passing to axum-server#9
josecelano merged 1 commit intomainfrom
fix/issue-8-tokio-blocking-socket

Conversation

@josecelano
Copy link
Member

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:

cargo run

Fixes #8

References

This fixes the Tokio runtime panic that occurs when running the application
with cargo run. The panic was caused by passing a blocking socket to
axum-server, which internally uses tokio::net::TcpListener::from_std()
that validates the socket is non-blocking.

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

This is the proper fix as opposed to using RUSTFLAGS workaround which
only masks the symptom and can cause performance issues under load.

Fixes #8
@josecelano josecelano self-assigned this Dec 22, 2025
@josecelano
Copy link
Member Author

ACK 8cdb2d9

@josecelano josecelano merged commit 23397cf into main Dec 22, 2025
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix Tokio blocking socket panic by setting TcpListener to non-blocking mode

1 participant