# Buggy code
def fromfd(fd, keep_fd=True):
    """Create a socket from a file descriptor

    socket domain (family), type and protocol are auto-detected. By default
    the socket uses a dup()ed fd. The original fd can be closed.

    The parameter `keep_fd` influences fd duplication. Under Python 2 the
    fd is still duplicated but the input fd is closed. Under Python 3 and
    with `keep_fd=True`, the new socket object uses the same fd.

    :param fd: socket fd
    :type fd: int
    :param keep_fd: keep input fd
    :type keep_fd: bool
    :return: socket.socket instance
    :raises OSError: for invalid socket fd
    """
    family = _raw_getsockname(fd)
    if hasattr(socket, "SO_TYPE"):
        typ = _raw_getsockopt(fd, socket.SOL_SOCKET, getattr(socket, "SO_TYPE"))
    else:
        typ = socket.SOCK_STREAM
    if hasattr(socket, "SO_PROTOCOL"):
        proto = _raw_getsockopt(fd, socket.SOL_SOCKET, getattr(socket, "SO_PROTOCOL"))
    else:
        proto = 0
    if keep_fd:
        return socket.fromfd(fd, family, typ, proto)
    else:
        return socket.socket(family, typ, proto, fileno=fd)


# Traceback (OSError)
[2019-11-25 11:33:44 +0800] [3967] [INFO] Starting gunicorn 20.0.2
[2019-11-25 11:33:44 +0800] [3967] [INFO] Listening at: http://127.0.0.1:8000 (3967)
[2019-11-25 11:33:44 +0800] [3967] [INFO] Using worker: sync
[2019-11-25 11:33:44 +0800] [3970] [INFO] Booting worker with pid: 3970
[2019-11-25 11:33:51 +0800] [3967] [INFO] Handling signal: usr2
[2019-11-25 11:33:51 +0800] [3975] [INFO] Starting gunicorn 20.0.2
Traceback (most recent call last):
File "/Users/xx/project/py37/bin/gunicorn", line 10, in <module>
sys.exit(run())
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/app/wsgiapp.py", line 58, in run
WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/app/base.py", line 219, in run
super().run()
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/app/base.py", line 71, in run
Arbiter(self).run()
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/arbiter.py", line 198, in run
self.start()
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/arbiter.py", line 155, in start
self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds)
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/sock.py", line 171, in create_sockets
sock = fromfd(fd)
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/socketfromfd.py", line 95, in fromfd
family = _raw_getsockopt(fd, socket.SOL_SOCKET, SO_DOMAIN)
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/socketfromfd.py", line 74, in _raw_getsockopt
ctypes.byref(optval), ctypes.byref(optlen))
File "/Users/xx/project/py37/lib/python3.7/site-packages/gunicorn/socketfromfd.py", line 44, in _errcheck_errno
raise OSError(errno, os.strerror(errno))
OSError: [Errno 42] Protocol not available

# Fixed code
def fromfd(fd, keep_fd=True):
    """Create a socket from a file descriptor

    socket domain (family), type and protocol are auto-detected. By default
    the socket uses a dup()ed fd. The original fd can be closed.

    The parameter `keep_fd` influences fd duplication. Under Python 2 the
    fd is still duplicated but the input fd is closed. Under Python 3 and
    with `keep_fd=True`, the new socket object uses the same fd.

    :param fd: socket fd
    :type fd: int
    :param keep_fd: keep input fd
    :type keep_fd: bool
    :return: socket.socket instance
    :raises OSError: for invalid socket fd
    """
    sockaddr = _getsockname(fd)
    family = sockaddr.sa_family
    if hasattr(socket, "SO_TYPE"):
        typ = _getsockopt(fd, socket.SOL_SOCKET, getattr(socket, "SO_TYPE"))
    else:
        typ = socket.SOCK_STREAM

    if hasattr(socket, "SO_PROTOCOL"):
        proto = _getsockopt(fd, socket.SOL_SOCKET, getattr(socket, "SO_PROTOCOL"))
    else:
        proto = 0
    if keep_fd:
        return socket.fromfd(fd, family, typ, proto)
    else:
        return socket.socket(family, typ, proto, fileno=fd)