Merge pull request #3498 from orrche/remoteserver_accept_fix

Fix so failed accept doesn't terminate the server
develop
Myk 2023-11-06 17:40:00 -08:00 committed by GitHub
commit 6fa65af4e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

@ -64,6 +64,7 @@ Template for new versions:
- `stockpiles`: hide configure and help buttons when the overlay panel is minimized - `stockpiles`: hide configure and help buttons when the overlay panel is minimized
- `caravan`: price of vermin swarms correctly adjusted down. a stack of 10000 bees is worth 10, not 10000 - `caravan`: price of vermin swarms correctly adjusted down. a stack of 10000 bees is worth 10, not 10000
- `sort`: when filtering out already-established temples in the location assignment screen, also filter out the "No specific deity" option if a non-denominational temple has already been established - `sort`: when filtering out already-established temples in the location assignment screen, also filter out the "No specific deity" option if a non-denominational temple has already been established
- RemoteServer: continue to accept connections as long as the listening socket is valid instead of closing the socket after the first disconnect
## Misc Improvements ## Misc Improvements
- `buildingplan`: display how many items are available on the planner panel - `buildingplan`: display how many items are available on the planner panel

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "PassiveSocket.h" #include "PassiveSocket.h"
#include "PluginManager.h" #include "PluginManager.h"
#include "MiscUtils.h" #include "MiscUtils.h"
#include "Debug.h"
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
@ -85,6 +86,7 @@ namespace {
} }
namespace DFHack { namespace DFHack {
DBG_DECLARE(core, socket, DebugCategory::LINFO);
struct BlockGuard { struct BlockGuard {
std::lock_guard<std::mutex> lock; std::lock_guard<std::mutex> lock;
@ -476,17 +478,33 @@ void ServerMainImpl::threadFn(std::promise<bool> promise, int port)
CActiveSocket *client = nullptr; CActiveSocket *client = nullptr;
try { try {
while ((client = server.socket.Accept()) != NULL) for (int acceptFail = 0 ; server.socket.IsSocketValid() && acceptFail < 5 ; acceptFail++)
{
if ((client = server.socket.Accept()) != NULL)
{ {
BlockGuard lock; BlockGuard lock;
ServerConnection::Accepted(client); ServerConnection::Accepted(client);
client = nullptr; client = nullptr;
} }
else
{
WARN(socket).print("Connection failure: %s (%d of %d)\n", server.socket.DescribeError(), acceptFail + 1, 5);
}
}
} catch(BlockedException &) { } catch(BlockedException &) {
if (client) if (client)
client->Close(); client->Close();
delete client; delete client;
} }
if (server.socket.IsSocketValid())
{
WARN(socket).print("Too many failed accepts, shutting down RemoteServer\n");
}
else
{
WARN(socket).print("Listening socket invalid, shutting down RemoteServer\n");
}
} }
void ServerMain::block() void ServerMain::block()