Skip to content

[Feature request] Support connect to ipv6 address #1252

Open

Description

@Jarvan-Wang

debugpy works well with ipv4,
now I are trying to use ipv6 , and obtain :
Error: invalid --connect <address>: invalid port number

I tried to modify debugpy/server/cli.py to fix address parse error:

def set_address(mode):
    def do(arg, it):
        if options.address is not None:
            raise ValueError("--listen and --connect are mutually exclusive")

        # It's either host:port, or just port.
        value = next(it)
        def get_host_port(address):
            parts = address.split(":")
            host = ":".join(parts[:-1])
            if host == "": 
                host = "127.0.0.1"
            port = parts[-1]
            return host, port
        host, port = get_host_port(value)
        #host, sep, port = value.partition(":")
        #if not sep:
        #    host = "127.0.0.1"
        #    port = value
        try:
            port = int(port)
        except Exception:
            port = -1
        if not (0 <= port < 2 ** 16):
            raise ValueError("invalid port number")

        options.mode = mode
        options.address = (host, port)

    return do

rerun and obtain:

  File "debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 491, in start_client
    s.connect((host, port))
socket.gaierror: [Errno -9] Address family for hostname not supported

Activity

  1. gattytto commented on Jul 17, 2024

    @gattytto

    +1 I need this feature please, I don't know why it doesn't work anymore I used to debug my apps inside containers with ipv6 only scenarios some months ago but something changed in latest versions that will make debugpy only listen on 0.0.0.0 even if there's no ipv4 in the container

  2. gattytto commented on Jul 24, 2024

    @gattytto

    the relevant code is here

    def _new_sock():
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)

    this needs to be extended so when address is not ipv4 but rather ipv6, it needs to be something like:

    def _new_sock_v6():
        sock = socket.socket(socket.AF_INET6...
  3. rchiodo commented on Jul 24, 2024

    @rchiodo
    Contributor

    If you're willing to try, we accept PRs. Sounds like you found where the problem is.

  4. gattytto commented on Jul 24, 2024

    @gattytto

    I don't think I'm that good of a dev but I will give it a try xD

  5. gattytto commented on Jul 25, 2024

    @gattytto

    I've been trying to adapt debugpy code to use ipv6 but it seems like pydevd vendored subfolder also has a lot of ipv4-only sock usage so I get output like this:

    object address  : 0x780f0377fc80
    object refcount : 6
    object type     : 0x21eb1a0
    object type name: gaierror
    object repr     : gaierror(-9, 'Address family for hostname not supported')
    lost sys.stderr

    please if there's some python genius around give us a hand I really would like to have this to be able to debug my containers in ipv6 only cluster

  6. gattytto commented on Jul 28, 2024

    @gattytto

    meanwhile it gets supported by the pydevd code, I've found a workaround that works easily with kubectl and vscode using debug attach (python option from the dropdown list).

    I don't even need to add a containerPort to the pod yaml.
    using apk package in alpine container called 6tunnel

    side note:
    don't use snap provided kubectl because it doesn't work so well (use the one from apt/apk or download manually)

    image
  7. mrjcleaver commented on Mar 26, 2025

    @mrjcleaver

    I'm hitting this on fly.io

    root@48ed01ea3e5de8:/app# python3 -c 'import debugpy; debugpy.listen(("::", 5678)); print("✅ Listening on [::]:5678"); debugpy.wait_for_client(); print("🧠 Debugger attached")'
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/usr/local/lib/python3.11/site-packages/debugpy/public_api.py", line 31, in wrapper
        return wrapped(*args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.11/site-packages/debugpy/server/api.py", line 132, in debug
        log.reraise_exception("{0}() failed:", func.__name__, level="info")
      File "/usr/local/lib/python3.11/site-packages/debugpy/server/api.py", line 130, in debug
        return func(address, settrace_kwargs, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/lib/python3.11/site-packages/debugpy/server/api.py", line 258, in listen
        raise RuntimeError(str(endpoints["error"]))
    RuntimeError: Can't listen for client connections: [Errno -9] Address family for hostname not supported
    

    And

     fly proxy 5678:5678 --debug --verbose
    Proxying local port 5678 to remote [servername.internal]:5678
    

    And in vs-code:

    {
        "version": "0.2.0",
        "configurations": [
          {
            "name": "Attach to Fly Remote",
            "type": "python",
            "request": "attach",
            "connect": {
              "host": "localhost",
              "port": 5678
            },
            "pathMappings": [
              {
                "localRoot": "[...]/app",
                "remoteRoot": "/app"
              }
            ],
            "justMyCode": false,
            "redirectOutput": true,
            "logToFile": true
          }
        ]
      }
    

    root@148e455c103908:/app# tcpdump -v -n port 5678

    tcpdump: listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
    14:42:15.925855 IP6 (hlim 62, next-header TCP (6) payload length: 40) fdaa:13:91b1:a7b:9285:0:a:0.49417 > fdaa:13:91b1:a7b:8a0f:111b:2e80:2.5678: Flags [S], cksum 0xe7af (correct), seq 4116840726, win 27136, options [mss 1360,sackOK,TS val 3897208999 ecr 0,nop,wscale 7], length 0
    14:42:15.925891 IP6 (flowlabel 0x85740, hlim 64, next-header TCP (6) payload length: 20) fdaa:13:91b1:a7b:8a0f:111b:2e80:2.5678 > fdaa:13:91b1:a7b:9285:0:a:0.49417: Flags [R.], cksum 0x520e (correct), seq 0, ack 4116840727, win 0, length 0
    

    Which indicates (per ChatGPT):

    So trying to bind to "::" (IPv6) directly won’t work — even though Fly's internal networking and tcpdump show IPv6 traffic, your Python runtime or OS layer doesn’t support binding to it.

  8. mrjcleaver commented on Mar 26, 2025

    @mrjcleaver

    Using gattytto (@gattytto) 's solution:

    apt-get update && apt-get install -y 6tunnel
    

    However, Bind 6tunnel to :: instead of ::1

    6tunnel -6 -l :: 5678 127.0.0.1 5678
    

    Why?
    ::1 = IPv6 loopback (like 127.0.0.1 for IPv6)
    :: = all IPv6 interfaces (like 0.0.0.0)
    Fly’s proxy connects to your app via its internal IPv6 network (not ::1)
    Whereas, if 6tunnel is only listening on ::1, Fly can’t reach it.

    So now your code can go back to:

     python3 -m debugpy --listen localhost:5678 --wait-for-client 
    

    With:

    • fly proxy tunnelling between your dev machine and the remote container running your app
    • 6tunnel running on the remote container tunnelling between the inbound IPV6 traffic to the Python IPV4-only version of debugpy.

    Referenced from https://community.fly.io/t/debugpy-on-fly-io-ipv4-ipv6-workaround-for-vs-code/24375

  9. added
    Fixed in next release
    on May 22, 2025
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

    Fixed in next releaseenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions