Skip to content

ResourceWarning: unclosed socket #1812

Closed

Description

Environment data

  • debugpy version: 1.8.9
  • OS and version: linux ubuntu 22.04 host via ssh via docker devcontainer (nvidia/cuda:12.1.1-runtime-ubuntu22.04)
  • Python version: uv, python 3.12.8
  • Using VS Code or Visual Studio: vscode 1.96.3

Actual behavior

getting unclosed socket warning only when running via debbuger (only extension installed is python + pylance + debugpy):

Exception ignored in: <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57946), raddr=('127.0.0.1', 33845)>
ResourceWarning: unclosed <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57946), raddr=('127.0.0.1', 33845)>

Expected behavior

should not pop

Steps to reproduce:

here is a full code repro:

import time
import warnings
from multiprocessing import Process, set_start_method

warnings.filterwarnings("error")


def script_runner():
    def decorate(func):
        def wrapper(*args, **kwargs):
            set_start_method("spawn")
            func(*args, **kwargs)

        return wrapper

    return decorate


@script_runner()
def main():
    processes = [Process(target=worker_function, args=(f"Worker-{i}",)) for i in range(5)]

    # Start all processes
    for process in processes:
        process.start()

    # Wait for all processes to complete
    for process in processes:
        process.join()

    print("All processes have completed.")


def worker_function(name: str) -> None:
    print(f"Process {name} is starting")
    time.sleep(2)  # Simulate work
    print(f"Process {name} has finished")


if __name__ == "__main__":
    # Create multiple processes
    main()

output:

Process Worker-2 is starting
Process Worker-0 is starting
Process Worker-3 is starting
Process Worker-1 is starting
Process Worker-2 has finished
Process Worker-4 is starting
Process Worker-0 has finished
Exception ignored in: <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57916), raddr=('127.0.0.1', 33845)>
ResourceWarning: unclosed <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57916), raddr=('127.0.0.1', 33845)>
Process Worker-3 has finished
Exception ignored in: <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57946), raddr=('127.0.0.1', 33845)>
ResourceWarning: unclosed <socket.socket fd=5, family=2, type=1, proto=0, laddr=('127.0.0.1', 57946), raddr=('127.0.0.1', 33845)>
Process Worker-1 has finished
Process Worker-4 has finished
All processes have completed.

Activity

  1. added
    needs repro
    on Jan 15, 2025
  2. rchiodo commented on Jan 15, 2025

    @rchiodo
    Contributor

    Thanks for the issue, but I don't think this is something we'd bother fixing.

  3. bersbersbers commented on Feb 3, 2025

    @bersbersbers
    Contributor

    I have come across essentially the same issue. In my eyes, despite the length of the example code above, the problem lies more or less in warnings.filterwarnings("error") - this code already reproduces the issue:

    echo import warnings; warnings.filterwarnings("error") > bug.py  
    debugpy --listen 5000 bug.py
    

    That it's a very old issue rooted deep in the Python code. However, if the very useful discussion in https://emptysqua.re/blog/against-resourcewarnings-in-python-3/#comment-514722438 is still current, that warning is off by default, and

    If you don't override the warnings filters (or use a library or test framework that does so on your behalf), then you won't see it.

    So modifying that warnings filter more carefully might fix the warnings output.

    Still, since this warning

    • is seen time and time again over the Python history,
    • is unlikely to be fixed any time soon,
    • does not give any indication where it is actually emitted, and thus
    • carries a lot of potential for confusion (I just spent an hour convincing myself that my large Python code base does not have a backdoor that opens a socket behind my back),

    ... I wonder if debugpy could go ahead and just explicitly close their socket(s) instead of relying on the Python garbage collector to do so. Happy to help with a PR if someone can point me into the right direction (any maybe hint at why this is not an obvious one-liner, if it isn't).

  4. rchiodo commented on Feb 3, 2025

    @rchiodo
    Contributor
  5. bersbersbers commented on Feb 4, 2025

    @bersbersbers
    Contributor

    Thanks, I think I got somewhere!

    Instructions:

    rem Install https://marketplace.visualstudio.com/items?itemName=ms-python.debugpy, v2024.14.0
    cd %USERPROFILE%\.vscode\extensions\ms-python.debugpy-2024.14.0-win32-x64\bundled\libs
    echo import warnings; warnings.filterwarnings("error") > bug.py  
    set PYDEVD_DISABLE_FILE_VALIDATION=1
    
    python -m debugpy --listen 5000 bug.py

    See

    Exception ignored in: <socket.socket fd=740, family=2, type=1, proto=0, laddr=('127.0.0.1', 54606), raddr=('127.0.0.1', 54604)>
    ResourceWarning: unclosed <socket.socket fd=740, family=2, type=1, proto=0, laddr=('127.0.0.1', 54606), raddr=('127.0.0.1', 54604)>
    

    Then replace debugpy\_vendored\pydevd\pydevd.py with the version from https://github.com/bersbersbers/debugpy/commit/47f1f4e93d7ac6d4496cc2b736967777e52cb7ea.

    Re-run

    python -m debugpy --listen 5000 bug.py
    

    See

    (nothing)

    Voila! I am 99% sure there is better location for the closing of the socket, but I am happy I got this prototype to begin with.

  6. bersbersbers commented on Feb 5, 2025

    @bersbersbers
    Contributor

    For the record, simpler reproduction checks not requiring VS Code at all: checkout debugpy and run

    C:\Git\debugpy\src>python -m debugpy --listen 5000 -c "import warnings; warnings.filterwarnings('error')"
    
  7. added a commit that references this issue on Feb 5, 2025
    fb0b06c
  8. added
    Fixed in next release
    on Feb 5, 2025
  9. rchiodo commented on Mar 5, 2025

    @rchiodo
    Contributor

    This issue should be fixed in the latest build of debugpy. It should ship with VS code in a future version of the Python Debugger Extension or you can install your own debugpy from pypi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Fixed in next releaseneeds repro

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions