Bug - Fixed Opening relay browser immediately logs out

Ryo_Sangnoir

Developer
Staff member
I think I'm still using the version that was working yesterday, but it's not working today. I can log in fine, I can do things, "chat" works, but opening the relay browser logs me out and closes Mafia. A debug log isn't very helpful:

Code:
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
        KoLmafia v20.4 r20086, Windows 10, Java 1.8.0_191
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Please note: do not post this log in the KoLmafia thread of KoL's
 Gameplay-Discussion forum. If you would like the KoLmafia dev team
 to look at it, please write a bug report at kolmafia.us. Include
 specific information about what you were doing when you made this
 and include this log as an attachment.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
 Timestamp: Tue May 12 14:25:40 BST 2020
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


Loading window: LoginFrame
Preparing for logout...
Sharing mall price data with other users...
Using data override: data/mallprices.txt
Success: Update successful -- no new prices.
Requests complete.

Sending logout request...
class net.sourceforge.kolmafia.request.LogoutRequest
Connecting to logout.php...

Requesting: https://www.kingdomofloathing.com/logout.php
2 request properties
Field: User-Agent = [KoLmafia v20.4]
Field: Accept-Encoding = [gzip]

Retrieving server reply...

[...]
 

Ryo_Sangnoir

Developer
Staff member
I've upgraded Java, but it's still broken.

I've tried it on a completely different machine, and it works there.

I checked the code: it's failing here, in RelayServer.run:

Code:
		while ( !this.openServerSocket() )
		{
			if ( RelayServer.port <= 60089 )
			{
				++RelayServer.port;
			}
			else
			{
				KoLmafia.quit();
			}
		}

I guess all ports are in use (now I've just got to find out what's running on them!). It would be nicer if Mafia had a good error message for this case, instead of just quitting.

Edit: to inch-mile: can I request a feature where I can provide the port I want the relay browser to start on? I checked and I don't seem to have anything running on 60080-90, but creating a server there still didn't work. I changed it to 50000, which was fine.
 
Last edited:

Magus_Prime

Well-known member
I've had this happen, on an infrequent basis, and restarting the OS "fixes" it. :) That may be sub-optimal but I never had the urge to figure out what was happening. Your post may point to the root cause.
 

Veracity

Developer
Staff member
Code:
	private synchronized boolean openServerSocket()
	{
		try
		{
			if ( Preferences.getBoolean( "relayAllowRemoteAccess" ) )
			{
				this.serverSocket = new ServerSocket( RelayServer.port, 25 );
			}
			else
			{
				this.serverSocket = new ServerSocket( RelayServer.port, 25, InetAddress.getByName( "127.0.0.1" ) );
			}

			return true;
		}
		catch ( Exception e )
		{
			return false;
		}
	}
It'd be nice to know what Exception it is catching.
 

Ryo_Sangnoir

Developer
Staff member
Code:
java.net.BindException: Address already in use: JVM_Bind

coming out of java.net.TwoStacksPlainSocketImpl.socketBind(Native Method)
 

Veracity

Developer
Staff member
Exception => IOException => SocketException => BindException

That's the only one I'd really expect for opening ("binding") a server port; other SocketExceptions are for connecting.

We're doing the right thing for that: trying a different port.
 

Ryo_Sangnoir

Developer
Staff member
We're doing the right thing for that: trying a different port.

Sure, but
* you're only trying 10 ports
* the range in which you try ports isn't configurable
* if you don't find a valid port, Mafia quits entirely without informing the user what went wrong.

I'd prefer it if the port range was configurable (or at least that Mafia didn't quit without telling you what was wrong). I've attached a patch showing a rough idea. It has a few flaws as written: once you get a working port you can't change it, and you have to hit the button twice before getting it working after changing the preference, but at least it allows someone in my situation to recover inside Mafia.

View attachment browserPort.patch
 

lostcalpolydude

Developer
Staff member
Is the lack of open ports due to KoLmafia not closing the ports when you exit, for some reason? Since only two instances of KoLmafia can be open at a time, I assume the limit of 10 was chosen because "there's no way anyone will reach that limit".
 

Ryo_Sangnoir

Developer
Staff member
I've got no idea why it's happening. I can't even find which processes are using the ports -- they're not showing up on netstat or get-nettcpconnection. It's possible Mafia isn't releasing them, but I don't know how to check that.
 

Magus_Prime

Well-known member
I shut down KoLmafia completely after running turns for the day and I've seen this behavior many times. On and off for several years. Not reproducible on-demand but, when it happens, restarting mafia doesn't fix it but restarting the computer does. If nothing else a message to the gCLI that mafia couldn't find any open ports, or something like it, would be preferable to a silent "good-bye".
 

Ryo_Sangnoir

Developer
Staff member
This is no longer happening today.

I've attached a patch below that provides an error message instead of quitting, and doesn't allow configuration of the port (unlike the earlier one). RelayLoader still tries to open the web page: I think that's fine?

View attachment browser.patch
 

Veracity

Developer
Staff member
OK. I tried it and see no problems. Revision 20093 is your patch (with some adjustment to formatting).
 
Top