-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
creating/binding a socket in a fixture causes it to not close #53
Comments
Hello @altendky Thanks for the time spent on this. However, I am unsure to get it right. Additionally, you probably want to close your socket after the yield statement in your fixture. |
Before rebinding to the port, in either case, the first socket is closed. Looking at the pytest output in the full session it shows that the second parametrization where the socket is created in the test there is no error (be sure to expand the collapsed sections in the OP and below here). Closing the socket in the fixture during teardown would normally make sense, but it isn't relevant to this example since it would occur after the error. Note that the failing test with the socket from the fixture is run first so it can't be caused by an issue not closing the socket from the passing case which is second. I probably shouldn't have parametrized the tests. Here's a more explicit non-parametrized recreation. import socket
import subprocess
import pytest
address = ("127.0.0.1", 33125)
@pytest.fixture(scope="function")
def just_a_socket():
sock = socket.socket()
sock.bind(address)
yield sock
sock.close()
def test_a_socket_from_a_fixture(just_a_socket):
sock = just_a_socket
sock.close()
sock2 = socket.socket()
sock2.bind(address)
def test_a_socket_from_the_test():
sock = socket.socket()
sock.bind(address)
sock.close()
sock2 = socket.socket()
sock2.bind(address)
sock.close()
def main():
subprocess.run(["pytest", "--capture", "no", "--verbose", __file__], check=True)
# yuck
if __name__ == "__main__":
main() commands
session$ cat > x.py << EOF
> import socket
> import subprocess
>
> import pytest
>
>
> address = ("127.0.0.1", 33125)
>
>
> @pytest.fixture(scope="function")
> def just_a_socket():
> sock = socket.socket()
> sock.bind(address)
> yield sock
> sock.close()
>
>
> def test_a_socket_from_a_fixture(just_a_socket):
> sock = just_a_socket
>
> sock.close()
>
> sock2 = socket.socket()
> sock2.bind(address)
>
>
> def test_a_socket_from_the_test():
> sock = socket.socket()
> sock.bind(address)
>
> sock.close()
>
> sock2 = socket.socket()
> sock2.bind(address)
> sock.close()
>
>
> def main():
> subprocess.run(["pytest", "--capture", "no", "--verbose", __file__], check=True)
>
>
> # yuck
> if __name__ == "__main__":
> main()
> EOF $ cat x.py
import socket
import subprocess
import pytest
address = ("127.0.0.1", 33125)
@pytest.fixture(scope="function")
def just_a_socket():
sock = socket.socket()
sock.bind(address)
yield sock
sock.close()
def test_a_socket_from_a_fixture(just_a_socket):
sock = just_a_socket
sock.close()
sock2 = socket.socket()
sock2.bind(address)
def test_a_socket_from_the_test():
sock = socket.socket()
sock.bind(address)
sock.close()
sock2 = socket.socket()
sock2.bind(address)
sock.close()
def main():
subprocess.run(["pytest", "--capture", "no", "--verbose", __file__], check=True)
# yuck
if __name__ == "__main__":
main() $ python3.9 -m venv venv $ venv/bin/python --version --version
Python 3.9.5 (default, Jun 3 2021, 15:18:23)
[GCC 9.3.0] $ venv/bin/python -m pip install --upgrade pip setuptools wheel
Requirement already satisfied: pip in ./venv/lib/python3.9/site-packages (21.1.1)
Collecting pip
Using cached pip-21.3.1-py3-none-any.whl (1.7 MB)
Requirement already satisfied: setuptools in ./venv/lib/python3.9/site-packages (56.0.0)
Collecting setuptools
Using cached setuptools-60.1.0-py3-none-any.whl (952 kB)
Collecting wheel
Using cached wheel-0.37.1-py2.py3-none-any.whl (35 kB)
Installing collected packages: wheel, setuptools, pip
Attempting uninstall: setuptools
Found existing installation: setuptools 56.0.0
Uninstalling setuptools-56.0.0:
Successfully uninstalled setuptools-56.0.0
Attempting uninstall: pip
Found existing installation: pip 21.1.1
Uninstalling pip-21.1.1:
Successfully uninstalled pip-21.1.1
Successfully installed pip-21.3.1 setuptools-60.1.0 wheel-0.37.1 $ venv/bin/pip install attrs==21.2.0 iniconfig==1.1.1 packaging==21.3 pluggy==1.0.0 py==1.11.0 pyparsing==3.0.6 pytest==6.2.5 toml==0.10.2
Collecting attrs==21.2.0
Using cached attrs-21.2.0-py2.py3-none-any.whl (53 kB)
Collecting iniconfig==1.1.1
Using cached iniconfig-1.1.1-py2.py3-none-any.whl (5.0 kB)
Collecting packaging==21.3
Using cached packaging-21.3-py3-none-any.whl (40 kB)
Collecting pluggy==1.0.0
Using cached pluggy-1.0.0-py2.py3-none-any.whl (13 kB)
Collecting py==1.11.0
Using cached py-1.11.0-py2.py3-none-any.whl (98 kB)
Collecting pyparsing==3.0.6
Using cached pyparsing-3.0.6-py3-none-any.whl (97 kB)
Collecting pytest==6.2.5
Using cached pytest-6.2.5-py3-none-any.whl (280 kB)
Collecting toml==0.10.2
Using cached toml-0.10.2-py2.py3-none-any.whl (16 kB)
Installing collected packages: pyparsing, toml, py, pluggy, packaging, iniconfig, attrs, pytest
Successfully installed attrs-21.2.0 iniconfig-1.1.1 packaging-21.3 pluggy-1.0.0 py-1.11.0 pyparsing-3.0.6 pytest-6.2.5 toml-0.10.2 $ venv/bin/pip freeze
attrs==21.2.0
iniconfig==1.1.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
pyparsing==3.0.6
pytest==6.2.5
toml==0.10.2 $ venv/bin/pytest x.py
================================================= test session starts ==================================================
platform linux -- Python 3.9.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/altendky/repos/chia-blockchain
collected 2 items
x.py .. [100%]
================================================== 2 passed in 0.01s =================================================== $ venv/bin/pip install attrs==21.2.0 certifi==2021.10.8 charset-normalizer==2.0.9 idna==3.3 iniconfig==1.1.1 memory-profiler==0.60.0 packaging==21.3 pluggy==1.0.0 psutil==5.8.0 py==1.11.0 pyparsing==3.0.6 pytest==6.2.5 pytest-monitor==1.6.3 requests==2.26.0 toml==0.10.2 urllib3==1.26.7
Requirement already satisfied: attrs==21.2.0 in ./venv/lib/python3.9/site-packages (21.2.0)
Collecting certifi==2021.10.8
Using cached certifi-2021.10.8-py2.py3-none-any.whl (149 kB)
Collecting charset-normalizer==2.0.9
Using cached charset_normalizer-2.0.9-py3-none-any.whl (39 kB)
Collecting idna==3.3
Using cached idna-3.3-py3-none-any.whl (61 kB)
Requirement already satisfied: iniconfig==1.1.1 in ./venv/lib/python3.9/site-packages (1.1.1)
Collecting memory-profiler==0.60.0
Using cached memory_profiler-0.60.0-py3-none-any.whl
Requirement already satisfied: packaging==21.3 in ./venv/lib/python3.9/site-packages (21.3)
Requirement already satisfied: pluggy==1.0.0 in ./venv/lib/python3.9/site-packages (1.0.0)
Collecting psutil==5.8.0
Using cached psutil-5.8.0-cp39-cp39-manylinux2010_x86_64.whl (293 kB)
Requirement already satisfied: py==1.11.0 in ./venv/lib/python3.9/site-packages (1.11.0)
Requirement already satisfied: pyparsing==3.0.6 in ./venv/lib/python3.9/site-packages (3.0.6)
Requirement already satisfied: pytest==6.2.5 in ./venv/lib/python3.9/site-packages (6.2.5)
Collecting pytest-monitor==1.6.3
Using cached pytest_monitor-1.6.3-py3-none-any.whl (14 kB)
Collecting requests==2.26.0
Using cached requests-2.26.0-py2.py3-none-any.whl (62 kB)
Requirement already satisfied: toml==0.10.2 in ./venv/lib/python3.9/site-packages (0.10.2)
Collecting urllib3==1.26.7
Using cached urllib3-1.26.7-py2.py3-none-any.whl (138 kB)
Requirement already satisfied: wheel in ./venv/lib/python3.9/site-packages (from pytest-monitor==1.6.3) (0.37.1)
Installing collected packages: urllib3, psutil, idna, charset-normalizer, certifi, requests, memory-profiler, pytest-monitor
Successfully installed certifi-2021.10.8 charset-normalizer-2.0.9 idna-3.3 memory-profiler-0.60.0 psutil-5.8.0 pytest-monitor-1.6.3 requests-2.26.0 urllib3-1.26.7 $ venv/bin/pip freeze
attrs==21.2.0
certifi==2021.10.8
charset-normalizer==2.0.9
idna==3.3
iniconfig==1.1.1
memory-profiler==0.60.0
packaging==21.3
pluggy==1.0.0
psutil==5.8.0
py==1.11.0
pyparsing==3.0.6
pytest==6.2.5
pytest-monitor==1.6.3
requests==2.26.0
toml==0.10.2
urllib3==1.26.7 $ venv/bin/pytest x.py
================================================= test session starts ==================================================
platform linux -- Python 3.9.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/altendky/repos/chia-blockchain
plugins: monitor-1.6.3
collected 2 items
x.py F. [100%]
======================================================= FAILURES =======================================================
_____________________________________________ test_a_socket_from_a_fixture _____________________________________________
just_a_socket = <socket.socket [closed] fd=-1, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
def test_a_socket_from_a_fixture(just_a_socket):
sock = just_a_socket
sock.close()
sock2 = socket.socket()
> sock2.bind(address)
E OSError: [Errno 98] Address already in use
x.py:24: OSError
=============================================== short test summary info ================================================
FAILED x.py::test_a_socket_from_a_fixture - OSError: [Errno 98] Address already in use
============================================= 1 failed, 1 passed in 0.06s ============================================== $ uname -a
Linux p1 5.4.0-91-generic #102-Ubuntu SMP Fri Nov 5 16:31:28 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux $ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal |
Forcing it to run pytest-monitor/pytest_monitor/pytest_monitor.py Lines 157 to 162 in a73b055
|
It does appear to be down in |
Ok got your point. |
Already filed a bug there since I could recreate with just it. Link above. I'll follow up there if I manage any more debugging. |
Describe the bug
While creating and binding a socket in a test and then closing it allows the same port to be reopened later in the test, if the socket was originally created and bound in a fixture then it fails claiming
OSError: [Errno 98] Address already in use
.To Reproduce
Complete input and output below, but the tl;dr is...
https://gist.github.com/altendky/656bd59340288be28e8e3eb9a8d6f1b8
Copy/paste this into a terminal in an empty directory on a system with Python 3.9 available
To get this session
$ python3.9 -m venv venv
Expected behavior
I expect sockets created and bound in a fixture to be able to be closed.
Desktop (please complete the following information):
Additional context
I probably won't get to it tonight, but I will try to see what I can learn about pytest-monitor towards the end of fixing this. I tested back to pytest-monitor v1.0.0 and it seems to be present throughout.
The text was updated successfully, but these errors were encountered: