Skip to content
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

Set socket reuseaddr to true while checking free port #7850

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/tribler/core/utilities/network_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def is_port_free(self, port):
try:
for socket_class in self.socket_class_set:
with socket_class() as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', port))
return True
except OSError:
Expand Down
7 changes: 7 additions & 0 deletions src/tribler/core/utilities/tests/test_network_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import socket

import pytest

from tribler.core.utilities.network_utils import FreePortNotFoundError, NetworkUtils
Expand All @@ -18,6 +20,11 @@ def bind(self, host_port):
finally:
self.bound_ports.add(port)

def setsockopt(self, level, option_name, option_value):
assert level == socket.SOL_SOCKET
assert option_name == socket.SO_REUSEADDR
assert option_value == 1

def __enter__(self):
return self

Expand Down
Loading