scratchfoundation / scratch-link

Device interoperability layer for Windows and MacOS
BSD 3-Clause "New" or "Revised" License
104 stars 84 forks source link

Display error and quit gracefully if WebSocket server port is busy #102

Closed cwillisf closed 5 years ago

cwillisf commented 5 years ago

Resolves

Resolves #100

Proposed Changes

If the WebSocket library reports that it could not bind its port because the port is already in use, display a message and quit gracefully.

Reason for Changes

Currently Scratch Link throws an exception (crashes) in this situation without providing any user feedback. Out of the small number of sessions which crash, this is by far the most common cause of a crash on Windows and the issue affects the macOS build as well.

How to test

  1. Start Scratch Link
    • On macOS, do this at a terminal with open "Scratch Link.app/Contents/MacOS/scratch-link" to ensure that the OS will let you launch the second copy later.
    • This instance should bind the port successfully and should not display any errors.
  2. Start Scratch Link again
    • On macOS, this copy may be started normally by double-clicking the app bundle or any other method.
    • This instance should display the new "Address already in use!" error message.

Notes

I'm open to suggestions on the text for the error message. This is my first draft:

Address already in use!

Scratch Link was unable to start because port 20110 is already in use.

This probably means another instance of Scratch Link is already running or another application is using port 20110.

This application will now exit.

evhan55 commented 5 years ago

I'd like to help test this, so I tried by doing the following:

1) Run localhost:8601 on one browser tab 2) Connect to micro:bit over Scratch Link 3) Run localhost:8601 on another tab 4) Attempt to connect to micro:bit over Scratch Link again

But nothing happened, no error, and the second tab couldn't find the micro:bit anyway. Is there some other, better way to observe/trigger the error you are trying to fix?

cwillisf commented 5 years ago

@evhan55 sorry! I've added testing steps above :)

evhan55 commented 5 years ago

I have tested this on Mac, but I believe I am not seeing the new error window.

Here are my steps: 1) Check out this branch 2) run sudo make in the macOS folder 3) Launch 'Scratch Link' in the dist folder 4) Check that 'Scratch Link' version is new:

Scratch Link 1.1.14 d96e6e9
macOS Version 10.14.1 (Build 18B75)

5) Try to launch 'Scratch Link' in the dist folder again

Nothing happens, just as with Scratch Link 1.1.11 I do not see a new error window Maybe I am testing incorrectly?

cwillisf commented 5 years ago

@evhan55 sorry, I didn't realize this before but apparently macOS (or maybe Electron when running on macOS) has some built-in protection for opening an app twice. I'd been launching from a terminal and through VS Code so I didn't run into the problem :p

Anyway, I've updated the testing steps above to include a workaround.

evhan55 commented 5 years ago

Thanks! It worked this time after opening it via Terminal twice on Mac OS like you said:

screen shot 2018-12-06 at 12 12 35 pm

Some further notes:

Address already in use!

Scratch Link was unable to start because port 20110 is already in use. This means Scratch Link is already running, or another application is using that port.

This application will now exit.

cwillisf commented 5 years ago

Just to clarify, the reason to start Scratch Link from the terminal is that macOS itself prevents running two copies of the same app bundle. By starting one of them from the console it's possible to bypass this. The second instance of Scratch Link can be opened normally and should display the error message.

I do like your text a bit better than mine, though :)