commit 510ad9926a5a93e0d382b50d1c35dde0d1b0087c
parent 860e21aa50f7bf6e4bd222c08a7412f665792d85
Author: Rickard Lind <rickard@familjen-lind.se>
Date: Sun, 20 Oct 2024 19:39:17 +0200
Make it safe to call timer close method from timer callback.
Diffstat:
1 file changed, 19 insertions(+), 16 deletions(-)
diff --git a/src/python.c b/src/python.c
@@ -3162,23 +3162,29 @@ pyconnection_x509_cb(void *udata, int islast, int nid, const char *field,
static void
pytimer_run(void *arg, u_int64_t now)
{
- PyObject *ret;
- struct pytimer *timer = arg;
+ PyObject *ret;
+ struct pytimer *timer = arg;
+ struct kore_timer *run;
+
+ run = timer->run;
+ timer->run = NULL;
PyErr_Clear();
ret = PyObject_CallFunctionObjArgs(timer->callable, timer->udata, NULL);
Py_XDECREF(ret);
- Py_XDECREF(timer->udata);
- timer->udata = NULL;
kore_python_log_error("pytimer_run");
if (timer->flags & KORE_TIMER_ONESHOT) {
- timer->run = NULL;
+ run->flags |= KORE_TIMER_ONESHOT;
Py_DECREF((PyObject *)timer);
}
+ else {
+ timer->run = run;
+ }
}
+
static void
pytimer_dealloc(struct pytimer *timer)
{
@@ -3192,6 +3198,11 @@ pytimer_dealloc(struct pytimer *timer)
timer->callable = NULL;
}
+ if (timer->udata != NULL) {
+ Py_DECREF(timer->udata);
+ timer->udata = NULL;
+ }
+
PyObject_Del((PyObject *)timer);
}
@@ -3201,19 +3212,11 @@ pytimer_close(struct pytimer *timer, PyObject *args)
if (timer->run != NULL) {
kore_timer_remove(timer->run);
timer->run = NULL;
+ Py_DECREF((PyObject *)timer);
+ } else {
+ timer->flags |= KORE_TIMER_ONESHOT;
}
- if (timer->callable != NULL) {
- Py_DECREF(timer->callable);
- timer->callable = NULL;
- }
-
- if (timer->udata != NULL) {
- Py_DECREF(timer->udata);
- timer->udata = NULL;
- }
-
- Py_INCREF((PyObject *)timer);
Py_RETURN_TRUE;
}