Skip to content

Conversation

alexs-sh
Copy link
Contributor

Define the default listen backlog close to the standard library.

This helps avoid hardcoded unsynchronized values and allows better control of default values depending on the target. This is the initial version of the change that addresses issue #1872

Selecting a “valid” default value can be tricky due to:

  • It often serves only as a hint and may be rounded, trimmed, or ignored by the OS.
  • It is sometimes provided as a "magic" value, for example, -1. This value is undocumented and not standard, but it is often used to represent the largest possible backlog size. This happens due to signed/unsigned conversion and rounding to the upper bound performed by the OS.
  • Default values vary depending on the OS and its version. Common defaults include: -1, 128, 1024, and 4096

As the topic could affect many targets, reviews, comments, and reports are very welcome.

Thank you

Define the `listen` backlog parameters as in the standard library.

On one hand, this helps avoid hardcoded or unsynchronized values. On the
other hand, it allows better control of default values depending on the
target.

This is an initial version which deals with issue tokio-rs#1872
src/sys/mod.rs Outdated
Comment on lines 113 to 123
#[allow(dead_code)]
#[cfg(any(
// Silently capped to `/proc/sys/net/core/somaxconn`.
target_os = "linux",
// Silently capped to `kern.ipc.soacceptqueue`.
target_os = "freebsd",
// Silently capped to `kern.somaxconn sysctl`.
target_os = "openbsd",
// Silently capped to the default 128.
target_vendor = "apple",
))]
Copy link
Contributor

Choose a reason for hiding this comment

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

Formatting is broken here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I’ll double-check. I already ran cargo fmt before submitting the PR

src/sys/mod.rs Outdated
/// include: -1, 128, 1024, and 4096.
///
#[allow(dead_code)]
pub(crate) const fn get_listen_backlog_size() -> i32 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be a constant (not function)?

Also it shouldn't need allow(dead_code)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thx.

  1. Yes, it's possible to have constant. I started with this approach, but I felt that the function better combines logically related code into one piece. Anyway, I’ll prepare a version using a constant and send it for review.
  2. Removing allow(dead_code) causes fails if net feature is not enabled.
 cargo build
   Compiling log v0.4.22
   Compiling mio v1.0.4 (/mnt/public-storage/projects/mio)
error: function `get_listen_backlog_size` is never used
   --> src/sys/mod.rs:101:21
    |
101 | pub(crate) const fn get_listen_backlog_size() -> i32 {
    |                     ^^^^^^^^^^^^^^^^^^^^^^^
    |
note: the lint level is defined here
   --> src/lib.rs:6:5
    |
6   |     dead_code
    |     ^^^^^^^^^

error: could not compile `mio` (lib) due to 1 previous error

target_os = "espidf",
target_os = "horizon"
))]
pub(crate) const LISTEN_BACKLOG_SIZE: i32 = 128;
Copy link
Collaborator

Choose a reason for hiding this comment

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

cfg_select! once stable would be handy here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. It’s pretty wordy now

@Thomasdezeeuw
Copy link
Collaborator

CI found one actual error:

  error: unused import: `std::convert::TryInto`
    --> src\sys\windows\tcp.rs:50:9
     |
  50 |     use std::convert::TryInto;

I think the rest can be ignored, a rebase should fix them.

- backlog size is now a constant, not a function
- removed unused import (windows)
- improved formatting
@alexs-sh alexs-sh force-pushed the detect-listen-backlog-as-standard-library branch from a789532 to b0a951b Compare August 28, 2025 11:26
@Thomasdezeeuw
Copy link
Collaborator

The 3 remaining failures seem legit, libc::SOMAXCONN is not defined for those platforms.

@alexs-sh
Copy link
Contributor Author

The 3 remaining failures seem legit, libc::SOMAXCONN is not defined for those platforms.

Good to know. Should I also add some fallback or special values for those targets? Following the STD is good, but not if it’s breaking things that were previously buildable

@Thomasdezeeuw
Copy link
Collaborator

Thomasdezeeuw commented Aug 28, 2025

Good to know. Should I also add some fallback or special values for those targets? Following the STD is good, but not if it’s breaking things that were previously buildable

Ideally we can add SOMAXCONN to libc for those targets, but since that will likely take some time I would also be ok to defaulting 1024 (the old value) for those.

@alexs-sh alexs-sh force-pushed the detect-listen-backlog-as-standard-library branch from 63b12cd to 11380e8 Compare August 28, 2025 12:42
@Thomasdezeeuw Thomasdezeeuw merged commit 8f7b87c into tokio-rs:master Aug 28, 2025
56 checks passed
@Thomasdezeeuw
Copy link
Collaborator

Thanks @alexs-sh

@stepancheg
Copy link

stepancheg commented Sep 11, 2025

Define the default listen backlog close to the standard library.

FWIW code you referenced is for unix socket.

For TCP socket default is 128.

https://github.com/rust-lang/rust/blob/76c5ed2847cdb26ef2822a3a165d710f6b772217/library/std/src/sys/net/connection/socket/mod.rs#L540-L552

The change seems good anyway.

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.

4 participants