Skip to content

Rework peripheral reset and always use finalizers #8960

Open
@tannewt

Description

@tannewt

While investigating #8726 I realized the crash was due to a conflict between "bulk" reset such as "pulsein_reset()" and "deinit()" based reset that is triggered by deiniting the heap. With early ports, we relied heavily on "bulk" reset so that we knew exactly the state of peripherals at the start of every run. However, we also implemented deinit() so that user code can deinit peripherals at a precise time. (del and finalizers are called at collect time or vm end.) If deinit() wasn't called but the object was GCed, then the resources would "leak" until the next "bulk" reset. More and more objects are now allocated with deinit() as the finalizer so that when the object is GCed, the resources aren't (hopefully) temporarily leaked. The original bug was caused when PulseIn was switched to using finalizers but not removing/changing the "bulk" reset.

We should refactor the CircuitPython API to always use finalizers and remove the bulk resets. That way we'll only have one way that resets occur and it doesn't require state outside the CP objects to achieve it.

The main external data we have is "never reset" information that keeps resources that are actually still in use from being reset by "bulk" reset (but not deinit() reset). This too is a source for bugs. Once we remove bulk resets, we can also remove never_reset because there is no danger for mis-deinit. It is tied to explicit deinit() calls. Any class that needs to live longer than the VM will need to be allocated outside the VM heap so that its finalizer isn't called at VM heap end. It must be explicitly managed instead.

(@dhalbert edit):
May be able to use finalizers; now use *_reset() or reset_*(). List gathered from reset_port() in all ports.

  • - audioio.AudioOut (switch atmel-samd AudioOut to use finalizer #9165)
  • audiobusio.PDMIn
  • audiobusio.I2SOut
  • i2s
  • frequencyio.FrequencyIn
  • touchio.TouchIn
  • analogio.AnalogIn
  • analogio.AnalogOut
  • watchdog
  • pew
  • busio.I2C
  • busio.SPI
  • busio.UART
  • rtc (? reset is in shared-bindings)
  • PWMOut: obsolete reference in broadcom
  • espcamera
  • ssl
  • dualbank
  • espnow
  • espulp
  • ps2io
  • sdioio
  • socketpool
  • touchio
  • neopixel_write
  • countio
  • rp2pio
  • bleio

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions