Skip to content

Commit

Permalink
Allow python threads and async io
Browse files Browse the repository at this point in the history
  • Loading branch information
thommey committed Jun 29, 2024
1 parent 2fa3042 commit 79beaa4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,10 @@ int main(int arg_c, char **arg_v)
sigaction(SIGILL, &sv, NULL);
sv.sa_handler = got_alarm;
sigaction(SIGALRM, &sv, NULL);
// Added for python.mod because the _signal handler otherwise overwrites it
// see https://discuss.python.org/t/asyncio-skipping-signal-handling-setup-during-import-for-python-embedded-context/37054/6
sv.sa_handler = got_term;
sigaction(SIGINT, &sv, NULL);

/* Initialize variables and stuff */
now = time(NULL);
Expand Down
6 changes: 5 additions & 1 deletion src/mod/modvals.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@
#define HOOK_LOADED 13
#define HOOK_BACKUP 14
#define HOOK_DIE 15
#define REAL_HOOKS 16
#define HOOK_PRE_SELECT 16
#define HOOK_POST_SELECT 17

#define REAL_HOOKS 18

#define HOOK_SHAREOUT 105
#define HOOK_SHAREIN 106
#define HOOK_ENCRYPT_PASS 107
Expand Down
16 changes: 16 additions & 0 deletions src/mod/python.mod/python.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static PyObject *pirp, *pglobals;

#undef global
static Function *global = NULL, *irc_funcs = NULL;
static PyThreadState *_pythreadsave;
#include "src/mod/python.mod/pycmds.c"
#include "src/mod/python.mod/tclpython.c"

Expand All @@ -53,6 +54,16 @@ static int python_expmem()
return 0; // TODO
}

static int python_gil_unlock() {
_pythreadsave = PyEval_SaveThread();
return 0;
}

static int python_gil_lock() {
PyEval_RestoreThread(_pythreadsave);
return 0;
}

// TODO: Do we really have to exit eggdrop on module load failure?
static void init_python() {
PyObject *pmodule;
Expand Down Expand Up @@ -119,6 +130,8 @@ static void python_report(int idx, int details)
static char *python_close()
{
Context;
del_hook(HOOK_PRE_SELECT, (Function)python_gil_unlock);
del_hook(HOOK_POST_SELECT, (Function)python_gil_lock);
kill_python();
rem_builtins(H_dcc, mydcc);
rem_tcl_commands(my_tcl_cmds);
Expand Down Expand Up @@ -160,5 +173,8 @@ char *python_start(Function *global_funcs)
/* Add command table to bind list */
add_builtins(H_dcc, mydcc);
add_tcl_commands(my_tcl_cmds);
add_hook(HOOK_PRE_SELECT, (Function)python_gil_unlock);
add_hook(HOOK_POST_SELECT, (Function)python_gil_lock);

return NULL;
}
3 changes: 3 additions & 0 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <fcntl.h>
#include "main.h"
#include "modules.h"
#include <limits.h>
#include <string.h>
#include <netdb.h>
Expand Down Expand Up @@ -924,11 +925,13 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
t.tv_sec = td->blocktime.tv_sec;
t.tv_usec = td->blocktime.tv_usec;

call_hook(HOOK_PRE_SELECT);
x = select((SELECT_TYPE_ARG1) maxfd + 1,
SELECT_TYPE_ARG234 (maxfd_r >= 0 ? &fdr : NULL),
SELECT_TYPE_ARG234 (maxfd_w >= 0 ? &fdw : NULL),
SELECT_TYPE_ARG234 (maxfd_e >= 0 ? &fde : NULL),
SELECT_TYPE_ARG5 &t);
call_hook(HOOK_POST_SELECT);
if (x == -1)
return -2; /* socket error */
if (x == 0)
Expand Down

0 comments on commit 79beaa4

Please sign in to comment.