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

@ -3164,20 +3164,26 @@ pytimer_run(void *arg, u_int64_t now)
{
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,28 +3198,25 @@ pytimer_dealloc(struct pytimer *timer)
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) {
Py_DECREF(timer->udata);
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;
}