std.socket: Add accept overload that returns peer address#10941
Merged
thewilsonator merged 1 commit intodlang:masterfrom Jan 27, 2026
Merged
std.socket: Add accept overload that returns peer address#10941thewilsonator merged 1 commit intodlang:masterfrom
thewilsonator merged 1 commit intodlang:masterfrom
Conversation
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.
Contributor
|
Thanks for your pull request, @CyberShadow! Bugzilla referencesYour 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 locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + phobos#10941" |
Contributor
thewilsonator
left a comment
There was a problem hiding this comment.
Otherwise looks fine.
| * `SocketAcceptException` if unable to _accept. See `accepting` for use | ||
| * with derived classes. | ||
| */ | ||
| Socket accept(out Address peerAddress) @trusted |
Contributor
There was a problem hiding this comment.
I don't like the blanket use of trusted here, but I see it is used a lot elsewhere in this file?
Member
Author
There was a problem hiding this comment.
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) |
Contributor
There was a problem hiding this comment.
if (newsock == socket_t.init), why the backwards check?
Member
Author
There was a problem hiding this comment.
- 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
- 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)
Contributor
There was a problem hiding this comment.
- I figured, but 1 fair. It would be good to update the style though. Thanks!
thewilsonator
approved these changes
Jan 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add a new
accept(out Address peerAddress)method toSocketthat 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
acceptandgetPeerAddresscalls.