Starting a timer within a timer expiration lambda function generates exception

Running openhabian openHAB 3.4.0 Build #3029

I’m trying to understand why the following code generates an exception.

I boiled down the code to a minimum for posting. If test is set to 1, it always fails with the exception.

What I am doing wrong with the lambda that is causing this? Importantly, it only happens if I have include the sleep(1) statement.

Any ideas?


from core.rules import rule
from core.triggers import when
from core.jsr223.scope import events
from time import sleep

from org.slf4j import LoggerFactory
from configuration import LOG_PREFIX

from core.actions import ScriptExecution
from java.time import ZonedDateTime

log = LoggerFactory.getLogger("{}.Testing Timers".format(LOG_PREFIX))

Event_Timer = None
timer_count = 0

test = 1

def processTimer(text):
    global timer_count, Event_Timer

    if(timer_count >= 3):
        log.info("Final timer expired. Timer Count = %d" % timer_count)
        return
 
    log.info("Timer #%d, Lambda function with args %s" % (timer_count, text))
    timer_count += 1
    if(test == 1):
        Event_Timer = genericTimer2(Event_Timer, 10, lambda: processTimer("Restart"))
    else:
        Event_Timer = ScriptExecution.createTimer(ZonedDateTime.now().plusSeconds(int(10)), lambda: processTimer("Restart"))
    sleep(1)

@rule("Testing Timers", description="")
@when("Item TestButton changed to ON")
def rTimer_Test(event):
    global Event_Timer, timer_count

    if (Event_Timer is not None):
        log.info("Cancelling Timer")
        Event_Timer.cancel()
        timer_count = 0

    Event_Timer = genericTimer2(Event_Timer, 5, lambda: processTimer("Expired"))
    events.sendCommand("TestButton", "OFF")
    return


def scriptUnloaded():
    global Event_Timer

    if (Event_Timer is not None):
        Event_Timer.cancel()

def genericTimer2(timer,seconds,func):
    if(timer != None):
        timer.cancel()
    timer = ScriptExecution.createTimer(ZonedDateTime.now().plusSeconds(int(seconds)), func)
    return(timer)

2022-09-30 11:32:57.308 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job '<unknown>' failed and stopped
org.python.core.PyException: KeyboardInterrupt: interrupted sleep
	at org.python.modules.time.Time.sleep(Time.java:475) ~[?:?]
	at jdk.internal.reflect.GeneratedMethodAccessor213.invoke(Unknown Source) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190) ~[?:?]
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:208) ~[?:?]
	at org.python.core.PyObject.__call__(PyObject.java:461) ~[?:?]
	at org.python.core.PyObject.__call__(PyObject.java:465) ~[?:?]
	at org.python.pycode._pyx7406.processTimer$1(/etc/openhab/automation/jsr223/python/personal/timer_testing.py:35) ~[?:?]
	at org.python.pycode._pyx7406.call_function(/etc/openhab/automation/jsr223/python/personal/timer_testing.py) ~[?:?]
	at org.python.core.PyTableCode.call(PyTableCode.java:173) ~[?:?]
	at org.python.core.PyBaseCode.call(PyBaseCode.java:134) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:416) ~[?:?]
	at org.python.pycode._pyx7406.f$5(/etc/openhab/automation/jsr223/python/personal/timer_testing.py:48) ~[?:?]
	at org.python.pycode._pyx7406.call_function(/etc/openhab/automation/jsr223/python/personal/timer_testing.py) ~[?:?]
	at org.python.core.PyTableCode.call(PyTableCode.java:173) ~[?:?]
	at org.python.core.PyBaseCode.call(PyBaseCode.java:119) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:406) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:401) ~[?:?]
	at org.python.core.PyFunction.invoke(PyFunction.java:547) ~[?:?]
	at com.sun.proxy.$Proxy622.apply(Unknown Source) ~[?:?]
	at org.openhab.core.model.script.actions.ScriptExecution.lambda$0(ScriptExecution.java:97) ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:191) ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:88) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
2022-09-30 11:33:07.308 [INFO ] [jsr223.jython.Testing Timers        ] - Timer #1, Lambda function with args Restart
2022-09-30 11:33:07.311 [WARN ] [ore.internal.scheduler.SchedulerImpl] - Scheduled job '<unknown>' failed and stopped
org.python.core.PyException: KeyboardInterrupt: interrupted sleep
	at org.python.modules.time.Time.sleep(Time.java:475) ~[?:?]
	at jdk.internal.reflect.GeneratedMethodAccessor213.invoke(Unknown Source) ~[?:?]
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
	at java.lang.reflect.Method.invoke(Method.java:566) ~[?:?]
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:190) ~[?:?]
	at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:208) ~[?:?]
	at org.python.core.PyObject.__call__(PyObject.java:461) ~[?:?]
	at org.python.core.PyObject.__call__(PyObject.java:465) ~[?:?]
	at org.python.pycode._pyx7406.processTimer$1(/etc/openhab/automation/jsr223/python/personal/timer_testing.py:35) ~[?:?]
	at org.python.pycode._pyx7406.call_function(/etc/openhab/automation/jsr223/python/personal/timer_testing.py) ~[?:?]
	at org.python.core.PyTableCode.call(PyTableCode.java:173) ~[?:?]
	at org.python.core.PyBaseCode.call(PyBaseCode.java:134) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:416) ~[?:?]
	at org.python.pycode._pyx7406.f$2(/etc/openhab/automation/jsr223/python/personal/timer_testing.py:32) ~[?:?]
	at org.python.pycode._pyx7406.call_function(/etc/openhab/automation/jsr223/python/personal/timer_testing.py) ~[?:?]
	at org.python.core.PyTableCode.call(PyTableCode.java:173) ~[?:?]
	at org.python.core.PyBaseCode.call(PyBaseCode.java:119) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:406) ~[?:?]
	at org.python.core.PyFunction.__call__(PyFunction.java:401) ~[?:?]
	at org.python.core.PyFunction.invoke(PyFunction.java:547) ~[?:?]
	at com.sun.proxy.$Proxy622.apply(Unknown Source) ~[?:?]
	at org.openhab.core.model.script.actions.ScriptExecution.lambda$0(ScriptExecution.java:97) ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$12(SchedulerImpl.java:191) ~[?:?]
	at org.openhab.core.internal.scheduler.SchedulerImpl.lambda$1(SchedulerImpl.java:88) ~[?:?]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515) [?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]