Skip to content

std.socket: Add accept overload that returns peer address#10941

Merged
thewilsonator merged 1 commit intodlang:masterfrom
CyberShadow:std-socket-accept-address
Jan 27, 2026
Merged

std.socket: Add accept overload that returns peer address#10941
thewilsonator merged 1 commit intodlang:masterfrom
CyberShadow:std-socket-accept-address

Conversation

@CyberShadow
Copy link
Member

Add a new accept(out Address peerAddress) method to Socket that accepts an incoming connection while also retrieving the connecting peer's address.

This avoids the need for a separate call to get the peer address after accepting, which also addresses a race condition which can occur when the peer disconnects between the accept and getPeerAddress calls.

Add a new accept(out Address peerAddress) method to Socket that
accepts an incoming connection while also retrieving the connecting
peer's address. This avoids the need for a separate call to get the
peer address after accepting, which also addresses a race condition
which can occur when the peer disconnects between the accept and
getPeerAddress calls.
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @CyberShadow!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#10941"

Copy link
Contributor

@thewilsonator thewilsonator left a comment

Choose a reason for hiding this comment

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

Otherwise looks fine.

* `SocketAcceptException` if unable to _accept. See `accepting` for use
* with derived classes.
*/
Socket accept(out Address peerAddress) @trusted
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like the blanket use of trusted here, but I see it is used a lot elsewhere in this file?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, the old overload has it too.

Address addr = createAddress();
socklen_t nameLen = addr.nameLen;
auto newsock = cast(socket_t).accept(sock, addr.name, &nameLen);
if (socket_t.init == newsock)
Copy link
Contributor

Choose a reason for hiding this comment

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

if (newsock == socket_t.init), why the backwards check?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. That's how the old overload does it - as with the above, this overload is a copy-paste of the existing one with just the new feature, existing style and quirks are maintained for consistency
  2. Writing the constant first is considered best practice in some C style guides, because it makes a == that was typo'd as = an error rather than a silent bug (assignment)

Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I figured, but 1 fair. It would be good to update the style though. Thanks!

@thewilsonator thewilsonator merged commit fd27b81 into dlang:master Jan 27, 2026
9 of 10 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.

3 participants