-
Notifications
You must be signed in to change notification settings - Fork 133
Fix bug in ESP32 gpio:stop/0 and gpio:close/1 #1057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is free-ing platform_data and nullifying the reference supposed to be done here or should it be deferred to
context_destroy. Perhaps @pguyot should weigh in on best practices here. There is the reference in the context_destroy comments about interrupts possibly getting a handle on the context, which could be relevant here, since we have a queue attached to this process, but I haven't studied it in detail.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like to hear what @pguyot has to say. This is also what is done in the I2C driver, nullifying the reference was very recently added, which pointed me in the direction of using this, and does fix the crash described in #816.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the absence of a response, I say we merge this in. The context_destroy function is defensive about whether to delete platform data if it is already null, so we don’t have to worry about double deletes.
IMHO this module needs to be re-written as a platform Nif so that it will automatically close on a GC after the last reference goes out of scope, but that is a different kettle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this change is correct but unsufficient. If
platform_datais freed (as it currently is ingpiodriver_close), it should be nullified as indeedcontext_destroywill attempt to free it again if it is not null, hence crash #816.The reason the free was added in
context_destroyis to avoid any memory leak. And free is called at the end of the destroy function, after the process is made unreachable (is removed from the process table), to cope with cases where the platform data is called from interruptions. This is typically the "ugly hack" mentioned in i2c driver.It looks like there still is a problem with GPIO driver, though.
gpiodriver_closeloops over listeners likegpiodriver_remove_int, but doesn't do it as well. It does callgpio_isr_handler_remove, but it doesn't callsys_unregister_listenerunlikegpiodriver_remove_int.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. I was unsafely removing the
listener.listeners_list_headmanually (without the lock), rather than using the safesys_unregister_listenerThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fixed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 9 duplicate lines. Do you think we should factorize them?
277-286 and 457-466.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactoring may be a good idea, it could alleviate future discrepancies between the
gpiodriver_closeandgpiodriver_remove_inthandling of removing the listener.If I were to replace the duplicate code with a common
remove_gpio_listenerfunction would it make sense to make that an inline function in this case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course the factorized code can be inlined.