Big Sur ServerSocket Trouble

I have an app which uses a ServerSocket to serve HTML over HTTP on localhost to a HTMLViewer, in order to play a video file. Works great.

However, testing in Big Sur reveals an interesting glitch: If I close the window (which destroys both the ServerSocket and the HTMLViewer), and then re-open the window and try it again, sometimes on Big Sur it does not work. I get a blank screen, and the HTMLViewer’s error event fires with a -1001 Timeout after a couple of minutes.

  • If I try it again immediately, it works - so it basically works every other time
  • If, after closing the window, I wait about 60 seconds and try it again, it works.
  • ServerSocket.Listen always succeeds and there are no errors.
  • When it falis, ServerSocket.AddSocket never fires, as if there is no connection attempt being made at all.
  • I’m using the same port number each time

My theory is that the safari video playback engine in Big Sur is not letting go of the sockets immediately, so that when I try to start everything over, the port is still busy and it fails.

Anyone else out there using ServerSockets on BigSur and noticing problems?
I’m using Xojo 2019R1.1.

Ports can be blocked if not closed properly on bs. Do you happen to stopListening in the window close event?

Also some ports are not even allowed anymore (and the listing is not updated by apple) or may be a bs bug.

  • I am definitely calling ServerSocket.StopListening.
  • I tried adding JavaScript to stop the < video > player and reset it’s SRC attribute to “”
  • I tried adding a 1 second delay before actually closing the HTMLViewer

None of these had any effect.

  • I tried using a different PORT number on each run - this solves the issue, which suggests that it definitely is a case of a “stuck” ServerSocket.

For design reasons, I don’t want to have to change the port # on each run, so I’d like to find a better solution if possible.

1 Like

Do you have any links to more info about this?

I’ts experience that tells me this and some apple big sur forum post. Search for port blocked big sur.

When we changed to big sur, some of our serversocket (listening) ports where blocked or unable to work normally). We changed all ports to a number >= 10000 and all worked normal again.

Maybe requires a fb request so xojo can investigate?

Found a solution:

By running this command in Terminal:

while true
do
  lsof -nP -iTCP:XXXXX  # replace with the Port number you are using
  sleep 2
done

I was able to see that some sockets were, indeed staying open, even after the ServerSocket, all of the TCPSockets, and even the HTMLViewer had been destoyed.

The solution was to add this code to the TCPSocket Subclass’s Destructor:

TCPSocket.Destructor
  if me.isConnected then
     me.close()
   end if

You have to check for .isConnected: without that test, then the app will crash (presumably because the TCPSocket gets closed twice)

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.