Ensure libgpiod resources are cleaned up on exit - #1109
Merged
makermelissa merged 1 commit intoApr 22, 2026
Conversation
Add proper cleanup for libgpiod GPIO lines, chips, and PulseIn subprocesses to prevent 'Unable to set line to input' errors when scripts are interrupted with Ctrl+C or SIGTERM and then restarted. Changes: - Add __del__/deinit to libgpiod Pin classes (1.x and 2.x) to release GPIO lines and close chips when Pin objects are garbage collected - Add __del__ to libgpiod Chip classes (1.x and 2.x) to close chip handles on garbage collection - Add __del__ to all PulseIn implementations (bcm283x, meson_g12, a311d) as a safety net for cleanup when deinit() is not called - Make PulseIn.deinit() idempotent (safe to call multiple times) - Add SIGTERM signal handler to PulseIn modules so subprocesses and message queues are cleaned up even when the process is killed - Fix digitalio.DigitalInOut.deinit() to call pin.deinit() instead of just deleting the reference, ensuring proper resource release Fixes adafruit#435
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #435 — GPIO lines held by libgpiod and PulseIn subprocesses were not being properly released when scripts were interrupted (Ctrl+C, SIGTERM), causing "Unable to set line to input" errors on subsequent runs.
Problem
When a Python script using Blinka's GPIO or PulseIn is interrupted:
__del__ordeinit()method, so GPIO lines were never released unlessinit()was called again to reconfigureatexithandlers which don't fire on SIGTERM, and thedeinit()method would crash if called twicedigitalio.DigitalInOut.deinit()just diddel self._pinwhich doesn't release GPIO lines on libgpiod 1.xChanges
deinit()method to explicitly release lines and close chips. Add__del__as a GC safety net.__del__to close chip handles on garbage collection.__del__safety net. Makedeinit()idempotent (safe to call multiple times). Add SIGTERM signal handler alongside existingatexithandler to ensure subprocesses and message queues are cleaned up.deinit()now callspin.deinit()when available instead of just deleting the reference.