-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Studio: Reclaim unavailable port after site deletion #195
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d034786
fix: Reclaim unavailable port after deletion
c7e15de
fix: Ensure port is correctly killed to reclaim
4bbc5d7
Merge branch 'trunk' into fix/reclaim-unavailable-port-after-deletion
7c464af
chore: Apply changes following `npm install`
d9192e8
refactor: Update function name for clarity
39b85aa
docs: Tweak code comment for consistency
f1c9f97
refactor: Ensure default port value is reset
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import http from 'http'; | ||
import killPort from 'cross-port-killer'; | ||
|
||
const DEFAULT_PORT = 8881; | ||
|
||
|
@@ -72,6 +73,26 @@ class PortFinder { | |
this.#unavailablePorts.push( port ); | ||
} | ||
} | ||
|
||
public reclaimUnavailablePort( port?: number ): void { | ||
if ( port && this.#unavailablePorts.includes( port ) ) { | ||
killPort( port ) | ||
.then( () => { | ||
console.log( `Killed processes using port ${ port }` ); | ||
} ) | ||
.catch( ( err ) => { | ||
console.error( `Failed to kill processes using port ${ port }: ${ err.message }` ); | ||
} ) | ||
.finally( () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These steps are added to the
|
||
// Ensure port finder cycles through newly reclaimed ports by removing | ||
// from unavailable ports list and resetting #searchPort. | ||
this.#unavailablePorts = this.#unavailablePorts.filter( | ||
( unavailablePort ) => unavailablePort !== port | ||
); | ||
this.#searchPort = DEFAULT_PORT; | ||
} ); | ||
} | ||
} | ||
} | ||
|
||
export const portFinder = PortFinder.getInstance(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before deciding to use this library, I tried a few things to try to release a
CLOSED
port for use, including:http.createServer
to attempt to.close
the port explicitly. This led to the port remaining in aCLOSED
state, but not being released, which is the same as what's already happening when a site is deleted (reference).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. Thecross-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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.