Skip to content

gh-74667: Make socket.getservbyname(), getservbyport() and getprotobyname() thread-safe - #151850

Open
tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-socket-getservbyname-threadsafe
Open

tonghuaroot wants to merge 2 commits into
python:mainfrom
tonghuaroot:fix-socket-getservbyname-threadsafe

Conversation

@tonghuaroot

@tonghuaroot tonghuaroot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

socket.getservbyname(), getservbyport() and getprotobyname() release the GIL around the non-reentrant POSIX getservbyname(3) / getservbyport(3) / getprotobyname(3), which return a pointer into a shared static buffer, so concurrent calls (including on free-threading builds) can clobber each other's result.

This uses the reentrant *_r() variants where configure detects them, and otherwise serializes the lookups with the existing netdb_lock, mirroring how gethostbyname() is already handled in the same module. A regression test exercises concurrent lookups.

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant