Fix ports not being released on Service.Close() #4666
Merged
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.
Description
Service.Close()
leaves some listeners open after thefrps
server is closed, locking the ports until the entire program process is stopped. For CLI users offrps
, this isn't an issue because the entire process stops when the server stops. But when usingfrps
like a library, opening a server and then closing it will still leave some ports open. This prevents a newfrps
server from being opened if one was previously opened and then closed.This PR fixes this by tracking all listeners
frps
opens, then closes them in theClose()
function along with the other listeners.Examples
The following code example will start an
frps
server, stop it, then attempt to start it again.Code example
Without this PR, this will fail because the webserver port,
7500
, is still in use:If I disable the webserver in the code example and run it again, the server still fails to start a second time, because port
7000
is still in use:With this PR, the previous server exits cleanly, and a new server can be started with no issue:
Notes
I renamed
listener
tomuxListener
to disambiguate it from the other types of listeners. It ends up being defined bysvr.muxer.DefaultListener()
, so I named it that. This doesn't seem to be a breaking change because no parts of the code try to call the originalsvr.listener
directly. But it doesn't matter what any of these are named as long as it gets closed properly.