Yelp / ephemeral-port-reserve

Find an unused port, reliably
MIT License
68 stars 15 forks source link

Fix ResourceWarning: unclosed <socket.socket... #14

Closed graingert closed 3 years ago

graingert commented 3 years ago

Fixes #13

asottile commented 3 years ago

iirc this leak is intentional and part of the core to how ephemeral-port-reserve works -- were you able to verify that the port continues to be in the state described in the docstring?

graingert commented 3 years ago

@asottile that's a different socket being closed currently the socket is closed by the gc as soon as .accept is called In fact the ResourceWarning fires just after the socket is closed

asottile commented 3 years ago

were you able to verify that the port continues to be in the state described in the docstring?

graingert commented 3 years ago
graingert@onomastic:~/projects/ephemeral-port-reserve$ python -X tracemalloc=25 -Wall -m ephemeral_port_reserve
/home/graingert/projects/ephemeral-port-reserve/ephemeral_port_reserve.py:48: ResourceWarning: unclosed <socket.socket fd=5, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 39027), raddr=('127.0.0.1', 43044)>
  s.accept()
Object allocated at (most recent call last):
  File "/usr/lib/python3.8/runpy.py", lineno 194
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.8/runpy.py", lineno 87
    exec(code, run_globals)
  File "/home/graingert/projects/ephemeral-port-reserve/ephemeral_port_reserve.py", lineno 59
    exit(main())
  File "/home/graingert/projects/ephemeral-port-reserve/ephemeral_port_reserve.py", lineno 54
    port = reserve(*argv[1:])
  File "/home/graingert/projects/ephemeral-port-reserve/ephemeral_port_reserve.py", lineno 48
    s.accept()
  File "/usr/lib/python3.8/socket.py", lineno 293
    sock = socket(self.family, self.type, self.proto, fileno=fd)
39027
graingert@onomastic:~/projects/ephemeral-port-reserve$ netstat | grep 39027
tcp        0      0 localhost:39027         localhost:43044         TIME_WAIT  
graingert@onomastic:~/projects/ephemeral-port-reserve$ git checkout patch-2
Switched to branch 'patch-2'
Your branch is up-to-date with 'graingert/patch-2'.
graingert@onomastic:~/projects/ephemeral-port-reserve$ python -X tracemalloc=25 -Wall -m ephemeral_port_reserve
35639
graingert@onomastic:~/projects/ephemeral-port-reserve$ netstat | grep 35639
tcp        0      0 localhost:35639         localhost:59418         TIME_WAIT 
graingert commented 3 years ago

@asottile yep it's still in TIME_WAIT

graingert commented 3 years ago

@chriskuehl can I get a review on this?

chriskuehl commented 3 years ago

Thanks @graingert, I've released this as v1.1.2: https://pypi.org/project/ephemeral-port-reserve/1.1.2/

graingert commented 3 years ago

@chriskuehl nice any chance of a 1.1.2.post0 for https://github.com/Yelp/ephemeral-port-reserve/pull/12 ?

chriskuehl commented 3 years ago

@graingert oops sorry, I missed that PR. I flubbed my lines a bit trying to quickly combine that with #15 for a new release, but there's now 1.1.3 (contains just #12) and 1.1.4 (contains #12 and #15) released.