← index #4458Issue #1128
Related · medium · value 0.328
QUERY · ISSUE

Various unexpeced errors, if initialized timer is not referenced by pythoncode

openby JohnMTuopened 2019-02-03updated 2026-03-24
proposed-close

When Timer is initialized, but instance is no longer referenced from python code, it's probably subject for GC.
Unfortunatelly after GC there are unexpected errors, when timer should fire.
Most probably underlaying RTOS or c++ code is still trying to reference memory, which was used by python Timer object, which is now gone.

initial topic: https://forum.micropython.org/viewtopic.php?f=2&t=5898&p=33812#p33812

CANDIDATE · ISSUE

Performing operations on non-initialized Timer leads to crashes/lockups

openby nadrimajstoropened 2015-02-22updated 2015-10-25
ports

After pressing RESET button and typing:

Micro Python v1.3.10 on 2015-02-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> n = 3
>>> def countdown(timer):
...     global n
...     if n <= 0:
...         print("Launch!")
...         timer.deinit()
...     else:
...         print(n)
...         n -= 1
... 
>>> tim = pyb.Timer(4, freq=1, callback=countdown)

At this moment REPL hangs and I need to press RESET to recover. :cry:

Probably related issue:

Micro Python v1.3.10 on 2015-02-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> n = 3
>>> def countdown(timer):
...     global n
...     if n <= 0:
...         print("Launch!")
...         timer.deinit()
...     else:
...         print(n)
...         n -= 1
... 
>>> tim = pyb.Timer(4)
>>> tim.callback(countdown)
>>> tim.init(freq=1)
>>> # Nothing happens :|

Nevertheless it does work if I continue and provide both kwargs:

>>> tim.init(freq=1, callback=countdown)
3
>>> 2
1
Launch!

However, if I continue with:

>>> tim.deinit()
>>> tim.callback(countdown)
>>> Launch!

I did not expect after issuing deinit() to have triggered callbacks without explicitly calling init with freq kwarg. :confused:
And after issuing a soft reset

PYB: sync filesystems
PYB: soft reboot
Micro Python v1.3.10 on 2015-02-13; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> tim = pyb.Timer(4, callback=print("I did not set freq option"))
I did not set freq option
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: must specify either freq, or prescaler and period
>>> tim = pyb.Timer(4)
>>> tim.callback(print("I did not set freq option"))
I did not set freq option
>>> 

It certainly comes as a surprise that callback is still kicking without explicit freq

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied