Skip to content

socket.getservbyname(), socket.getservbyport(), socket.getprotobyname() are not threadsafe #74667

Open

Description

@dwfreed
mannequin
BPO 30482
Nosy @pitrou, @vstinner, @njsmith, @dwfreed

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2017-05-26.09:32:15.697>
labels = ['extension-modules', 'type-bug', '3.7']
title = 'socket.getservbyname(), socket.getservbyport(), socket.getprotobyname() are not threadsafe'
updated_at = <Date 2017-05-27.07:09:19.668>
user = 'https://github.com/dwfreed'

bugs.python.org fields:

activity = <Date 2017-05-27.07:09:19.668>
actor = 'njs'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Extension Modules']
creation = <Date 2017-05-26.09:32:15.697>
creator = 'dwfreed'
dependencies = []
files = []
hgrepos = []
issue_num = 30482
keywords = []
message_count = 3.0
messages = ['294540', '294545', '294564']
nosy_count = 4.0
nosy_names = ['pitrou', 'vstinner', 'njs', 'dwfreed']
pr_nums = []
priority = 'normal'
resolution = None
stage = 'needs patch'
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue30482'
versions = ['Python 2.7', 'Python 3.5', 'Python 3.6', 'Python 3.7']

Linked PRs

Activity

  1. dwfreed commented on May 26, 2017

    dwfreedmannequin
    MannequinAuthor

    On at least Linux (and probably most other UNIXes, except OS X), the C functions getservbyname(), getservbyport(), and getprotobyname() are not threadsafe. CPython's wrappers around these functions in the socket module do nothing to cover up this fact. Simple reproduction script for getservbyname (others similar):

    import threading
    import socket
    
    def getservbyname_loop(service, port):
            while True:
                    result = socket.getservbyname(service)
                    if result != port:
                            raise RuntimeError("thread-safety broken, got %d, expected %d" % (result, port))
    
    thread1 = threading.Thread(target=getservbyname_loop, args=("ssh", 22))
    thread2 = threading.Thread(target=getservbyname_loop, args=("smtp", 25))
    thread1.start()
    thread2.start()
    

    One of the threads will throw the RuntimeError, saying it got the port number the other thread should have gotten.

    Naive fix: a lock (eg, just use the netdb_lock already created in the module)

    Proper fix: use the libc's reentrant variant if available, and fall back to locking if not (see gethostbyname_ex() implementation for example).

    I'd be happy to work on this, but as I don't have access to anything other than Linux and OS X at the moment, it would be helpful if platform maintainers could chime in on what if any reentrant variants of these functions exist on their platforms so we can have a more proper fix.

  2. pitrou commented on May 26, 2017

    @pitrou
    Member

    The configure.ac script should check for the availability of gethostbyname_r and friends, like it already does for other functions.

  3. dwfreed commented on May 26, 2017

    dwfreedmannequin
    MannequinAuthor

    It already checks for gethostbyname_r, but the comments in socketmodule.c mention that configure seems to get it wrong. Those comments are probably old, though, so perhaps that can be revisited as well.

  4. transferred this issue fromon Apr 10, 2022
  5. added 2 commits that reference this issue on Jun 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.7 (EOL)end of lifeextension-modulesC modules in the Modules dirtopic-socketRelated to the socket, socketserver and selectors modulestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions