Make it safe to call timer close method from timer callback.

This commit is contained in:
Rickard Lind 2024-10-20 19:39:17 +02:00 committed by Joris Vink
parent 860e21aa50
commit 510ad9926a

View File

@ -3162,23 +3162,29 @@ pyconnection_x509_cb(void *udata, int islast, int nid, const char *field,
static void static void
pytimer_run(void *arg, u_int64_t now) pytimer_run(void *arg, u_int64_t now)
{ {
PyObject *ret; PyObject *ret;
struct pytimer *timer = arg; struct pytimer *timer = arg;
struct kore_timer *run;
run = timer->run;
timer->run = NULL;
PyErr_Clear(); PyErr_Clear();
ret = PyObject_CallFunctionObjArgs(timer->callable, timer->udata, NULL); ret = PyObject_CallFunctionObjArgs(timer->callable, timer->udata, NULL);
Py_XDECREF(ret); Py_XDECREF(ret);
Py_XDECREF(timer->udata);
timer->udata = NULL;
kore_python_log_error("pytimer_run"); kore_python_log_error("pytimer_run");
if (timer->flags & KORE_TIMER_ONESHOT) { if (timer->flags & KORE_TIMER_ONESHOT) {
timer->run = NULL; run->flags |= KORE_TIMER_ONESHOT;
Py_DECREF((PyObject *)timer); Py_DECREF((PyObject *)timer);
} }
else {
timer->run = run;
}
} }
static void static void
pytimer_dealloc(struct pytimer *timer) pytimer_dealloc(struct pytimer *timer)
{ {
@ -3192,28 +3198,25 @@ pytimer_dealloc(struct pytimer *timer)
timer->callable = NULL; timer->callable = NULL;
} }
PyObject_Del((PyObject *)timer);
}
static PyObject *
pytimer_close(struct pytimer *timer, PyObject *args)
{
if (timer->run != NULL) {
kore_timer_remove(timer->run);
timer->run = NULL;
}
if (timer->callable != NULL) {
Py_DECREF(timer->callable);
timer->callable = NULL;
}
if (timer->udata != NULL) { if (timer->udata != NULL) {
Py_DECREF(timer->udata); Py_DECREF(timer->udata);
timer->udata = NULL; timer->udata = NULL;
} }
Py_INCREF((PyObject *)timer); PyObject_Del((PyObject *)timer);
}
static PyObject *
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;
}
Py_RETURN_TRUE; Py_RETURN_TRUE;
} }