Pioreactor / Pioreactor/pioreactor

race condition when software-pwms stop and lgpio needs to clean up

Open
#435 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

pwm
Dominant language
Python
Stars
149
Forks
18
PR merge metrics
No merged PRs in 30d

Description

If Python exits quickly, and lgpio hasn't cleaned, an error is thrown:

Exception in thread Thread-5 (by_duration):
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/usr/local/lib/python3.11/dist-packages/pioreactor/actions/pump.py", line 106, in by_duration
    self.stop()
  File "/usr/local/lib/python3.11/dist-packages/pioreactor/actions/pump.py", line 85, in stop
    self.pwm.stop()
  File "/usr/local/lib/python3.11/dist-packages/pioreactor/utils/pwm.py", line 246, in stop
    self._pwm.off()
  File "/usr/local/lib/python3.11/dist-packages/pioreactor/utils/pwm.py", line 87, in off
    self.dc = 0.0
    ^^^^^^^
  File "/usr/local/lib/python3.11/dist-packages/pioreactor/utils/pwm.py", line 98, in dc
    lgpio.tx_pwm(self._handle, self.pin, self.frequency, self.dc)
  File "/usr/lib/python3/dist-packages/lgpio.py", line 1083, in tx_pwm
    return _u2i(_lgpio._tx_pwm(
           ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/lgpio.py", line 458, in _u2i
    raise error(error_text(v))
lgpio.error: 'unknown handle'

Reproducible with: pio run add_alt_media --duration 1

The error only really happens when lgpio is inside a thread (like for pumps by_duration function).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with pioreactor/actions/pump.py, especially the by_duration and stop paths, then inspect pioreactor/utils/pwm.py where lgpio cleanup and PWM shutdown occur. Reproduce with pio run add_alt_media --duration 1 and trace the threaded shutdown. Done means quick Python exit no longer produces an lgpio.error: 'unknown handle' exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, raspberry-pi
Domain
embedded-iot
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.