pycom / pycom-micropython-sigfox

A fork of MicroPython with the ESP32 port customized to run on Pycom's IoT multi-network modules.
MIT License
196 stars 167 forks source link

[Timer.Alarm] triggered at the wrong time when cancelling the alarm with the shortest deadline #89

Closed quentin-ol closed 6 years ago

quentin-ol commented 6 years ago

Please include the following information when submitting a bug report:

PyCom board: LoPy firmware version: 1.9.0.b2

Steps to reproduce the bug:

from machine import Timer
import utime

ALARM_1 = None
ALARM_2 = None
ALARM_3 = None
TICKS = None

def cb1(_):
    '''
    first callback: cancel alarm_2
    '''
    ALARM_2.cancel()

def cb2(_):
    '''
    second callback: will not be called
    '''
    print('I won\'t be called')

def cb3(_):
    '''
    third callback: will be called at the wrong time
    '''
    print(
        '[cb3] BUG: called {0}ms after being scheduled instead of 3000ms'
        .format(utime.ticks_diff(TICKS, utime.ticks_ms()))
    )

TICKS = utime.ticks_ms()
ALARM_1 = Timer.Alarm(cb1, ms=1000, periodic=False)
ALARM_2 = Timer.Alarm(cb2, ms=2000, periodic=False)
ALARM_3 = Timer.Alarm(cb3, ms=3000, periodic=False)

Expected result:

cb3 is called ~3000ms after ALARM_3 is initialised.

Actual result:

cb3 is called ~2000ms after ALARM_3 is initialised, i.e. when cb2 was expected prior to cancellation.

Wild guess:

The hardware timer is not reset when removing the alarm heap head when heap size > 2.

quentin-ol commented 6 years ago

Firmware version 1.10.0.b1 fixes this issue.