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
- `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
- RemoteServer: continue to accept connections as long as the listening socket is valid instead of closing the socket after the first disconnect
## Misc Improvements
- `buildingplan`: display how many items are available on the planner panel

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