Skip to content

use accept4 for accepted sockets in Linux - #3656

Closed
songgaoye wants to merge 2 commits into
apache:masterfrom
songgaoye:perf/accept4-accepted-sockets
Closed

songgaoye wants to merge 2 commits into
apache:masterfrom
songgaoye:perf/accept4-accepted-sockets

Conversation

@songgaoye

@songgaoye songgaoye commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: resolve
Problem Summary:

On Linux, each accepted connection currently uses accept() followed by
separate calls to set non-blocking and close-on-exec flags. This adds
unnecessary syscalls on the connection-accept hot path.

What is changed and the side effects?

Changed:

  • Use accept4() with SOCK_NONBLOCK | SOCK_CLOEXEC for accepted sockets on
    Linux.
  • Fall back to accept() when accept4() is unavailable (ENOSYS).
  • Skip redundant flag-setting calls when the accepted fd already has the
    requested flags.
  • Add a Linux-only regression test verifying accepted sockets have
    O_NONBLOCK and FD_CLOEXEC.

Side effects:

  • Performance effects: reduces syscall overhead while accepting connections on
    Linux.
  • Breaking backward compatibility: No. Non-Linux platforms and Linux kernels
    without accept4() retain the existing behavior.

Check List:

  • Please make sure your changes are compilable.
    • cmake --build build --target brpc_socket_unittest -j4
  • When providing us with a new feature, it is best to add related tests.
  • Please follow Contributor Covenant Code of Conduct.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

Static review found no blocking issues in flag propagation, fallback handling, or the regression test; tests were not executed during review.

No open issues 🎉

What changed in this PR

This PR reduces Linux connection-accept overhead by setting socket flags through accept4(), while preserving existing behavior on other platforms and older Linux kernels.

Changes:

  • Use accept4() with non-blocking and close-on-exec flags, falling back to accept() on ENOSYS.
  • Pass known descriptor flags through socket initialization to avoid redundant flag-setting calls.
  • Add a Linux-only regression test for accepted socket flags.
File Description
test/​brpc_socket_unittest.cpp Verify accepted sockets have both required flags on Linux.
src/​brpc/​socket.h Declare flag-aware socket initialization interfaces.
src/​brpc/​socket.cpp Skip flag-setting calls when flags are already set.
src/​brpc/​acceptor.cpp Use accept4() on Linux with an accept() fallback.

Review effort: Balanced

@wwbmmm

wwbmmm commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

I don't think this is necessary. Connection-accept is not hot path in most scenarios.

@songgaoye songgaoye closed this Oct 25, 2026
@songgaoye

Copy link
Copy Markdown
Contributor Author

close it

@songgaoye
songgaoye deleted the perf/accept4-accepted-sockets branch October 25, 2026 10:27
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.

3 participants