Skip to content

Commit d587d8a

Browse files
author
Mario de Frutos
committed
Handler to manage interrupts for python code inside PLPython
1 parent 730811c commit d587d8a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/pl/plpython/plpy_main.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ PyObject *PLy_interp_globals = NULL;
7474
/* this doesn't need to be global; use PLy_current_execution_context() */
7575
static PLyExecutionContext *PLy_execution_contexts = NULL;
7676

77+
/* postgres backend handler for interruption */
78+
static pqsigfunc coreIntHandler = 0;
79+
static void PLy_handle_interrupt(int sig);
80+
7781

7882
void
7983
_PG_init(void)
@@ -166,6 +170,9 @@ PLy_initialize(void)
166170
if (PyErr_Occurred())
167171
PLy_elog(FATAL, "untrapped error in initialization");
168172

173+
// Catch and process signals
174+
coreIntHandler = pqsignal(SIGINT, PLy_handle_interrupt);
175+
169176
init_procedure_caches();
170177

171178
explicit_subtransactions = NIL;
@@ -454,3 +461,22 @@ PLy_pop_execution_context(void)
454461
MemoryContextDelete(context->scratch_ctx);
455462
PLy_free(context);
456463
}
464+
465+
static void
466+
PLy_python_interruption_handler()
467+
{
468+
PyErr_SetString(PyExc_RuntimeError, "test except");
469+
return NULL;
470+
}
471+
472+
static void
473+
PLy_handle_interrupt(int sig)
474+
{
475+
// custom interruption
476+
int added = Py_AddPendingCall(PLy_python_interruption_handler, NULL);
477+
478+
if (coreIntHandler) {
479+
(*coreIntHandler)(sig);
480+
}
481+
}
482+

0 commit comments

Comments
 (0)