Bulletproof handling around the GPIO fd#260
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens GPIO resource lifecycle handling by making “closed/not-open” state explicit via pin->fd = -1, aligning cleanup paths, and ensuring stub backend read/write fail cleanly when the pin isn’t open.
Changes:
- Add
pin->fd < 0guards to the stub backend’shal_read_gpio/hal_write_gpioto return-EBADFwhen not open. - Set
pin->fd = -1in the cdev backend’shal_close_gpioafter closing the fd. - Make NIF cleanup call
hal_close_gpiounconditionally and normalizepin->envreset toNULL; document thathal_close_gpiois idempotent.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| c_src/hal_stub.c | Prevents stub read/write operations on unopened/closed pins by returning -EBADF. |
| c_src/hal_cdev_gpio.c | Marks the GPIO fd invalid after close to avoid reuse/accidental operations on a stale fd. |
| c_src/gpio_nif.h | Documents that hal_close_gpio may be called more than once. |
| c_src/gpio_nif.c | Simplifies release logic by always calling hal_close_gpio and using NULL for cleared env pointers. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
No description provided.