Skip to content

tcp_acceptor::accept() should support returning the peer socket #227

@sgerbino

Description

@sgerbino

Current API

corosio::tcp_socket peer(ioc);
auto [ec] = co_await acc.accept(peer);

The caller must pre-construct a tcp_socket and pass it to accept().

Proposed addition

Add an overload that returns the peer socket directly:

auto [ec, peer] = co_await acc.accept();

The existing accept(tcp_socket&) overload is retained for callers that want to manage socket lifetime themselves (e.g., pre-allocated or recycled sockets).

Motivation

The current interface requires the caller to construct a socket before accepting. This has a design issue: the caller can pass a socket associated with a different io_context than the acceptor's, creating a failure mode that doesn't exist at the POSIX level (where accept() simply returns a new file descriptor).

The acceptor already knows its io_context, and the underlying accept(2) returns a new fd — it doesn't write into a pre-existing one. A returning overload:

  1. Eliminates a misuse vector — no way to pass a socket bound to the wrong io_context
  2. Matches POSIX semanticsaccept(2) returns a new fd
  3. Simplifies common usage — one line instead of two

Retaining the existing overload gives callers who pre-allocate or recycle sockets a way to continue doing so.

Affected code

  • include/boost/corosio/tcp_acceptor.hpp — add returning overload and corresponding awaitable
  • Backend implementations (epoll, select, kqueue, iocp) — may need a new virtual or the returning overload can be built on top of the existing one

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions