-
Notifications
You must be signed in to change notification settings - Fork 64
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
Stop server does not free the accept socket port (on Windows) #70
Comments
Thanks for the report! I will take a look and try to reproduce it. I just don't have the time at the moment, so it will take be a little while. Thanks for the patience! |
Hi @monsieurgustav, when you write you "stop" the sever, what exactly are you doing? Killing an application? Deleting the C++ object? |
Sure, here it is:
I call this function when the root folder of the FTP server changes. (I don't think there is another way to do it) If I remember correctly, one may need to connect (anonymously) to the server before it is destroyed/restarted to trigger the issue. I use WinSCP to connect. |
I confirmed the issue with wireshark. Indeed, the socket is not closed properly. I think the reason is the following code: fineftp-server/fineftp-server/src/server_impl.cpp Lines 114 to 122 in 6588420
The server stops the io_service, which makes the application to rely on the OS to clean up the sockets. And that may not happen fast enough. I will check how to fix that bug. For future references: You can also check the existence of that bug in Wireshark, as it will show the improperly terminated connection in red: My test application looks like follows (Windows only, due to hacky system commands): #include <chrono>
#include <fineftp/server.h>
#include <string>
#include <thread>
void restartFtpServer(std::unique_ptr<fineftp::FtpServer>& ftpServer, const std::string& folder, uint16_t port, size_t threadCount = 1)
{
if (ftpServer)
{
ftpServer->stop();
ftpServer.reset();
}
ftpServer = std::make_unique<fineftp::FtpServer>(port);
ftpServer->addUserAnonymous(folder, fineftp::Permission::ReadOnly);
ftpServer->start(threadCount);
}
int main() {
std::unique_ptr<fineftp::FtpServer> ftpServer;
restartFtpServer(ftpServer, "C:\\", 2121, 1);
system("pause");
restartFtpServer(ftpServer, "D:\\", 2121, 1);
system("pause");
}
|
Did you try to pause between the stop/start and wait a long time? (30s? 1mn?) |
Actually, I did not experience any issue. In my test, fineftp was able to re-open the port immediately (code above). I didn't check netstat though, as I already confirmed the bug with Wireshark. What needs to be done is entirely drop the
|
@monsieurgustav: I fixed the socket-close issue in a branch: https://github.com/eclipse-ecal/fineftp-server/tree/hotfix/properly_close_sockets It's not perfect, yet, but ready for a test. Can you test it out? Btw, I am not sure if it actually fixes your issue, but it is worth a try. I found that you can actually keep the socket open from the outside (e.g. using curl) and I am unsure whether that is something caused by the Windows Kernel or by the fineftp server library. So maybe while trying to fix your issue I fixed an unrelated bug. Let's see. Either way, here is a Powershell command that shows all open ports for an app (here Get-NetTCPConnection | Where-Object { (Get-Process | Where-Object { $_.Name -eq 'fineftp_example' }).Id -eq $_.OwningProcess } |
Hi Florian,
Unless you see another benefit, I suggest leaving this fix out. |
Based on this discussion, I tried something else:
Just in case you don't read Qt code: I wait an event loop iteration between the destruction and the re-creation of the server. It's not ideal, I'd like to know how it is different, but I also don't care that much and I can live with it! So I close the issue, feel free to re-open it if you want to investigate more. |
I am still investigating. I used a lot of Qt in the past and still maintain Qt projects, so I know what you are doing there. I just translated your Maybe you can answer a few questions, as I totally fail to reproduce your issue (Wrote a test that restarts the server 1000 times and accessed it with CURL and WinSCP in parallel, still no issue):
I pushed another commit to the same branch. This time I didn't aim to fix the issue but actually make it worse. Before, fineFTP opened sockets shared, so multiple server could be opened on the same port (which doesn't really make sense). I removed that code, so now you should be confronted with the following error message, if fineFTP is supposed to open a port that is already in use: |
Hi,
I have an unsual use-case where I must stop and restart the FTP server during the app lifetime.
But when I stop it the 1st time, I can observe that the listening port is still busy. (using "netstat -abn -p tcp") The next start works without an error, but no connection is actually accepted.
It happens even if no client connect to the server.
Do you have a clue?
Best regards,
Guillaume
The text was updated successfully, but these errors were encountered: