[FIX] Kill Parsee on unexcepted stream closure

This commit is contained in:
LDA 2024-10-20 12:53:07 +02:00
commit 02a89270c0
6 changed files with 49 additions and 15 deletions

View file

@ -265,8 +265,9 @@ Main(Array *args, HashMap *env)
}
server = HttpServerCreate(&conf);
((ParseeData *) conf.handlerArgs)->server = server;
if (!ParseeInitialiseSignals(server, xmpp_thr, jabber))
if (!ParseeInitialiseSignals(conf.handlerArgs, xmpp_thr))
{
goto end;
}

View file

@ -32,6 +32,9 @@ ParseeInitData(XMPPComponent *comp)
data->oid_servers = HashMapCreate();
pthread_mutex_init(&data->oidl, NULL);
data->halted = false;
pthread_mutex_init(&data->halt_lock, NULL);
if (data->config->db_size)
{
data->db = DbOpenLMDB(data->config->db_path, data->config->db_size);
@ -109,6 +112,7 @@ ParseeFreeData(ParseeData *data)
}
HashMapFree(data->oid_servers);
pthread_mutex_destroy(&data->oidl);
pthread_mutex_destroy(&data->halt_lock);
Free(data->id);
XMPPEndCompStream(data->jabber);
DbClose(data->db);

View file

@ -6,39 +6,40 @@
#include <XMPP.h>
static HttpServer *server = NULL;
static ParseeData *data;
static pthread_t xmpp_thr;
static XMPPComponent *jabber = NULL;
static void
SignalHandler(int signal)
{
if (server && (signal == SIGTERM || signal == SIGINT))
if (data->server && (signal == SIGTERM || signal == SIGINT))
{
Log(LOG_INFO, "Killing thread...");
XMPPFinishCompStream(jabber);
pthread_mutex_lock(&data->halt_lock);
data->halted = true;
pthread_mutex_unlock(&data->halt_lock);
XMPPFinishCompStream(data->jabber);
pthread_join(xmpp_thr, NULL);
Log(LOG_INFO, "Stopping server...");
HttpServerStop(server);
HttpServerStop(data->server);
return;
}
if (signal == SIGPIPE)
{
Log(LOG_DEBUG, "Caught a SIGPIPE...");
XMPPFinishCompStream(jabber);
pthread_join(xmpp_thr, NULL);
return;
}
}
bool
ParseeInitialiseSignals(HttpServer *s, pthread_t xmpp, XMPPComponent *j)
ParseeInitialiseSignals(ParseeData *d, pthread_t xmpp)
{
struct sigaction sa;
server = s;
data = d;
xmpp_thr = xmpp;
jabber = j;
sigfillset(&sa.sa_mask);
sa.sa_handler = SignalHandler;

View file

@ -136,6 +136,8 @@ ParseeXMPPThread(void *argp)
XMLElement *stanza = NULL;
HashMap *await_table2;
size_t i;
bool error = false;
/* Initialise the await table */
await_table = HashMapCreate();
@ -243,6 +245,14 @@ ParseeXMPPThread(void *argp)
* few threads to be busy, while the rest of Parsee works. */
PushStanza(&info, stanza);
}
pthread_mutex_lock(&args->halt_lock);
if (!args->halted)
{
Log(LOG_WARNING, "XMPP server is closing stream...");
Log(LOG_WARNING, "Stopping %s...", NAME);
error = true;
}
pthread_mutex_unlock(&args->halt_lock);
info.running = false;
for (i = 0; i < info.available_dispatchers; i++)
@ -278,6 +288,10 @@ ParseeXMPPThread(void *argp)
DestroyPEPManager(info.pep_manager);
XMPPFreeManager(info.m);
if (error)
{
HttpServerStop(args->server); /* minor trolling */
}
return NULL;
}

View file

@ -60,6 +60,7 @@ typedef struct ParseeData {
HttpRouter *router;
CommandRouter *handler;
HttpServer *server;
XMPPComponent *jabber;
Db *db;
@ -69,6 +70,10 @@ typedef struct ParseeData {
HashMap *oid_servers;
pthread_mutex_t oidl;
/* If Parsee was intentionally halted */
bool halted;
pthread_mutex_t halt_lock;
} ParseeData;
typedef struct Argument {
@ -285,7 +290,11 @@ extern bool ParseeGetDMOrigin(ParseeData *data, char *chat_id, char *ev, char **
/* Sends presence requests for every MUC around as a fake JID */
extern void ParseeSendPresence(ParseeData *);
extern bool ParseeInitialiseSignals(HttpServer *, pthread_t, XMPPComponent *);
/** Initialises signal-handling code within Parsee.
* --------------------
* Modifies: the signal handler
* Returns: whenever it has properly been setup */
extern bool ParseeInitialiseSignals(ParseeData *data, pthread_t xmpp);
/* Job used to cleanup Parsee data that isn't necessary anymore. */
extern void ParseeCleanup(void *data);