Skip to content

Slightly randomize starting port - #625

Merged
corrados merged 1 commit into
jamulussoftware:masterfrom
bflamig:master
Sep 27, 2020
Merged

corrados merged 1 commit into
jamulussoftware:masterfrom
bflamig:master

Conversation

@bflamig

@bflamig bflamig commented Sep 25, 2020

Copy link
Copy Markdown
Contributor

Issue #568 documents problems connecting to a server. The symptoms were narrowed down to a scenario where one client is connected to the server, using port (default + 10 = 22134), and then disconnected. A second client on another machine is started and attempts to connect to the server, and is given the same port number (22134) as a matter of course, since it's the first port number to try. Due to what is probably a faulty router (or some other mechanism?), that port is in fact still associated with the first machine, even though Jamulus is not running there, having been already disconnected from the server. The result is the UDP packets for the second machine are wrongly sent to the first machine. This results in the behavior where the server thinks the new client is connected, but the new client does not, because it is not receiving packets from the server. This behavior persists over an indeterminate amount of time, leading to much frustration on the part of the user trying to connect.

One workaround was suggested in #568 where if a client is not able to reconnect, the initial port used in finding an available can be manually changed from the standard 22134 starting port number to some other number, by specifying the starting port number on the command line using the -p option. While that seems to work, this pull request suggests a more automatic way to work around the issue: Instead of always starting with default + 10 (22134) port number, start instead with default + 10 + some small random number. Chances are then good that any problem due to a "sticky port" on a faulty router (or wherever it happens) is bypassed.

NOTE: Since the assignment of starting port number is done in the initial phases of the client startup, if the client fails to connect, the user should shut down Jamulus and try again, upon which a different starting port number will be used.

@corrados

Copy link
Copy Markdown
Contributor

There is actually already some randomization supported by Jamulus. Can you please try to start Jamulus with:

Jamulus --port 0

And see if that solves your problem? I am curious.

@bflamig

bflamig commented Sep 26, 2020

Copy link
Copy Markdown
Contributor Author

I had seen the if statement in the code that checked for the port number being 0 and then randomizing, but hadn't realized the conditions it occurred under. You are saying it's from when the --port 0 option is used. If so, I don't see why that wouldn't solve the problem. I'll try it. But since the problem is intermittent (the vast majority of the time I don't have any connection problems) it may be a while for it shows up and I can test it.

I am wondering, for information purposes, what's the reason for not randomizing all the time? I can think of a few reasons myself: more consistency in the ports being chosen makes for easier debugging down the line, etc. Easier to administrate, etc.

@corrados

Copy link
Copy Markdown
Contributor

The problem with randomizing is that you cannot do these multithreading tests anymore which we performed a lot recently. If you start a bunch of clients at the same PC, by randomizing, you will most probably get the same port for two clients and the second client will not start up. If you just increment the ports, that cannot happen. That was the reason why I incremented it. It would be good if your problem was solved by using the --port 0.

@bflamig

bflamig commented Sep 26, 2020

Copy link
Copy Markdown
Contributor Author

"by randomizing, you will most probably get the same port for two clients"

Not understanding this. How are they likely to get the same port?

On the other hand, by not randomizing, they are both going to start at 22134, right? So both clients will likely get port 22134 (if the socket bind() function messes up and says that port is available to both). I must be missing something.

How does the bind() function actually work? How does it know whether a port is "available" or not? Is the algorithm different between Windows and Linux? Just curious.

My proposal is in between the two approaches of full randomization and randomizing (within a few ports) where the port scanning range starts.

@corrados

Copy link
Copy Markdown
Contributor

I must be missing something.

No, I was missing something actually... I did not understand my own code anymore ;-).

But I have still one concern. You create a random offset of 0 to 5. So in 20 % probability your two clients will use the same port and you still have issues with your router. How to solve this?

I think by using --port 0 you will get a random port in a much wider range which makes it almost unprobable to get the same port. Do you agree?

@bflamig

bflamig commented Sep 26, 2020 •

Copy link
Copy Markdown
Contributor Author

I agree there is a 20% probability that the two clients will do the port scanning from the same starting port number. But I was figuring that was better than a 100% probability. Chances are that if a client is seeing the connect failure due to a "sticky port" -- that is, a port that is in some kind of limbo as described in the opening comments, that they can just restart the client and chances are good it will work, since there is an 80% chance a different starting port will be used.

By doing it this way, the client doesn't have to set the --port 0 option on the command line. They don't have to know anything about it. If there is a port in the first five ports that is "in limbo", 80% of the time it will not be used, without the client having to do anything. That's what I was intending. If per chance that doesn't work, just tell them in the instructions to restart Jamulus if they are having connection issues. The next time around, there is an 80% chance it will work.

I had used such a small random range because I was being conservative. I didn't want to change the code behavior too much from what was already there. I was thinking "let's do something in between complete random and not random at all." The range could be bigger. I figured you would be better in knowing what that should be.

But I ask the question again: Why not select the port completely randomly all the time anyway? What was the reason for always starting at default_port + 10 , (and then incrementing the number if selected port isn't "available.")

UPDATE: I just tested using --port 0. On my Windows 10 machine it selected port 56038 one time, and 50282 another. I checked the documentation for the bind socket function in Windows, and it said the range of ports selected from is 49152 - 65535, if running Windows Vista or later. So that's far away from the 22134 range. I say this just in case you didn't know.

I do have a question: If I have two machines on the local network running Jamulus clients, how does one machine know what ports the other machine is using? I assumed the router, but in the issue thread #568 someone said that the machine you are running on decides what to use -- not the router. So I am a bit befuddled the moment :)

@bflamig

bflamig commented Sep 26, 2020

Copy link
Copy Markdown
Contributor Author

I changed my last comment a few times trying to get the language right.

@bflamig

bflamig commented Sep 26, 2020

Copy link
Copy Markdown
Contributor Author

UPDATE: Near the latest comments on the thread at #568, @atsampson explains very well how the port assignment works.

@corrados

Copy link
Copy Markdown
Contributor

What was the reason for always starting at default_port + 10 , (and then incrementing the number if selected port isn't "available.")

No specific reason. I'll merge your code now and will slightly modify it. Thanks.

@corrados
corrados merged commit 3ba8743 into jamulussoftware:master Sep 27, 2020
@bflamig

bflamig commented Sep 28, 2020

Copy link
Copy Markdown
Contributor Author

Ok thanks

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants