Skip to content

Config.create_sockets() leaves the socket unclosed when bind() fails #94

Closed

Description

@MichengLiang

Config._create_sockets (inherited from Hypercorn) creates a socket, configures it, then calls sock.bind(binding). If bind() raises (e.g. EADDRINUSE), the socket that was just created is never closed, and neither are the sockets already created for earlier entries in binds. They are only reclaimed by the garbage collector, with ResourceWarning: unclosed <socket.socket ... laddr=('0.0.0.0', 0)>.

Reproduced on anycorn 0.20.1, Python 3.13, Linux:

import gc
import socket
import warnings

from anycorn.config import Config

warnings.simplefilter("always", ResourceWarning)

occupied = socket.create_server(("127.0.0.1", 0))
config = Config()
config.bind = [f"127.0.0.1:{occupied.getsockname()[1]}"]

gc.disable()
try:
    config.create_sockets()
except OSError as error:
    print("bind failed as expected:", error)
print("collecting...")
gc.collect()
gc.enable()
occupied.close()
bind failed as expected: [Errno 98] Address already in use
repro.py:17: ResourceWarning: unclosed <socket.socket fd=4, family=2, type=1, proto=0, laddr=('0.0.0.0', 0)>
collecting...

Here the socket is released by reference counting as soon as the exception is dropped. When the exception travels through task groups / exception groups, the traceback (holding the _create_sockets frame and its local sock) tends to end up in a reference cycle, so the socket stays open until the cyclic GC runs at some arbitrary later point. We hit it in a test suite that fails on ResourceWarning: a test that deliberately binds an occupied port made unrelated tests fail intermittently.

Closing what was created when anything in the loop raises would fix it, e.g. a try/except BaseException around each socket's setup that closes sock and re-raises, and an outer one that closes the already-collected sockets. (For fd:// the socket wraps a caller-owned descriptor, so that path may want detach() rather than close(); it only raises SocketTypeError before any bind.)

The same code is in Hypercorn, reported there as pgjones/hypercorn#392. Happy to send a PR here if you'd like.

Activity

  1. davidbrochart commented on Nov 2, 2026

    @davidbrochart
    Owner

    Thanks for reporting @MichengLiang, I opened #96.

  2. MichengLiang commented on Nov 13, 2026

    @MichengLiang
    Author

    Thanks again for the quick fix in #96! Is a release that includes it planned soon?

    We currently bind the listening sockets ourselves to avoid the leaked socket on a failed bind(), and we'd like to drop that workaround and hand binding back to Config.create_sockets() once the fix is on PyPI (0.20.1 doesn't include it yet).

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions