Studio: Reclaim unavailable port after site deletion - #195
Conversation
| .catch( ( err ) => { | ||
| console.error( `Failed to kill processes using port ${ port }: ${ err.message }` ); | ||
| } ) | ||
| .finally( () => { |
There was a problem hiding this comment.
These steps are added to the finally block as there are some instances where we can have the desired outcome even if killPort fails. For example:
- User deletes site, but an error occurs when attempting to kill the port.
- The user's laptop gets put to sleep, thus causing the port to be killed by the OS.
- User returns to laptop and creates another site in the app.
- The reclaimed port is used thanks to the fact it was removed from the
#unavailablePortslist and#searchPortwas reset to the default.
| @@ -1,4 +1,5 @@ | |||
| import http from 'http'; | |||
| import killPort from 'cross-port-killer'; | |||
There was a problem hiding this comment.
Before deciding to use this library, I tried a few things to try to release a CLOSED port for use, including:
- Used
http.createServerto attempt to.closethe port explicitly. This led to the port remaining in aCLOSEDstate, but not being released, which is the same as what's already happening when a site is deleted (reference). - Experimented with explicitly setting
socket.setReuseAddress(true), so as to be able to re-use the port more quickly and also detecting/destroying open sockets.
The only success I had was with manually killing the PIDs associated with a given port using a lsof command, which is only available for Unix systems. The cross-port-killer library works for all platforms and using it felt more efficient than the code that would be required.
I'm open to further thoughts or ideas here.
There was a problem hiding this comment.
Thanks for explaining the reasoning behind using the library for that. It seems that the library is a wrapper and uses tools available in win32, linux, and darwin to kill the port, so it would go well with our support for Mac, Windows, and Linux in the future.
|
@SiobhyB I tested it on macOS and couldn't make it work so far:
I would expect it used 8906, assuming all ports between 8881 and 8905 are taken. Another thing I noticed is that Studio still doesn't reuse ports that were used in the past and are free now. Those ports are used by sites I have locally: Studio keeps adding ports over the 8908, and doesn't reuse the free ports. It should work that way:
Based on those, it should take 8882 in the above case. Any thoughts why it doesn't reuse those? |
|
@wojtekn, thank you for spotting that! I found it was because |
|
Thanks for fixing that @SiobhyB. The use case I shared works as expected now, and Studio now uses unused ports from the list I shared above. |
Fixes: 7366-gh-Automattic/dotcom-forge
Proposed Changes
CLOSEDstate following the site deletion.Testing Instructions
8881).lsof -i :8881in the command line (replace8881with your site's port).lsof -i :8881in the command line (again, replacing8881with your site's port).8881in our examples).Pre-merge Checklist