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

Conversation

xoriole
Copy link
Contributor

@xoriole xoriole commented Jan 24, 2024

Fixes #7847

@xoriole xoriole marked this pull request as ready for review January 24, 2024 12:43
@xoriole xoriole requested review from a team and drew2a and removed request for a team January 24, 2024 12:43
@drew2a
Copy link
Contributor

drew2a commented Jan 25, 2024

Nice improvement. Could you educate me a bit and share how this change fixes the issue?

@xoriole
Copy link
Contributor Author

xoriole commented Jan 25, 2024

If the local application dies while a connection is active, the port will be tied up in TIME_WAIT. This is also true if the application dies while a connection is pending.
TIME_WAIT is the state that typically ties up the port for several minutes after the process has completed.

@drew2a Here is a nice write-up about Bind: Address Already in Use issue
https://hea-www.harvard.edu/~fine/Tech/addrinuse.html

image

@drew2a
Copy link
Contributor

drew2a commented Jan 25, 2024

Thank you for the article!

I now understand that you modified NetworkUtils.is_port_free() to allow it to return TRUE for ports that are in the TIME_WAIT status.

Copy link
Contributor

@drew2a drew2a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allowing NetworkUtils.is_port_free() to return TRUE for ports that are in the TIME_WAIT status leads to two consequences:

  1. It allows the GUI to reuse a port that is in TIME_WAIT status (which solves the issue with the Application Tester).

if not default_network_utils.is_port_free(api_port):
raise RuntimeError(
"Tribler configuration conflicts with the current OS state: "
"REST API port %i already in use" % api_port
)

  1. It allows NetworkUtils.get_random_free_port() and NetworkUtils.get_first_free_port() to use ports in TIME_WAIT status.

Both cases are risky as there could be a third-party application (not Tribler) that is leaving the port in TIME_WAIT to restore it later.

Although this fix resolves the issue with the Application Tester, it could lead to more severe problems in production.

I suggest solving the problem by using a random port for each consecutive Application Tester run (currently, it uses 20100 all the time).

@xoriole
Copy link
Contributor Author

xoriole commented Jan 25, 2024

I have implemented another PR #7859 as an alternative solution to the current PR.
Closing this PR in favor of #7859.

@xoriole xoriole closed this Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Application tester re-run fails with OS error [Errno 98] Address already in use
2 participants