Fix memory leak on interrupt notifications#252
Merged
Merged
Conversation
It turns out that the message environment when calling enif_send needs to be cleared. This was found by Claude and is easily confirmed by watching `:erlang.memory(:total)` whie processing a lot of interrupts.
There was a problem hiding this comment.
Pull request overview
This PR fixes an Erlang VM memory leak observed while processing high volumes of GPIO interrupt notifications by ensuring the message-building NIF environment used with enif_send is cleared after each send.
Changes:
- Update
send_gpio_messageto build messages in a dedicatedmsg_env, copygpio_specinto that env, andenif_clear_env(msg_env)after sending. - Adjust the cdev interrupt poller thread to allocate a reusable
msg_envonce and reuse it across events. - Update the stub HAL notification path to allocate/free a per-send
msg_envaround message sending.
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 | Allocate a message env per notification send and free it afterward. |
| c_src/hal_cdev_gpio_interrupts.c | Reuse a thread-local message env for interrupt message construction/sending. |
| c_src/gpio_nif.h | Document and extend send_gpio_message API to accept a message env. |
| c_src/gpio_nif.c | Build interrupt message terms in msg_env, send via enif_send, and clear msg_env to prevent leaks. |
💡 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.
It turns out that the message environment when calling enif_send needs
to be cleared. This was found by Claude and is easily confirmed by
watching
:erlang.memory(:total)whie processing a lot of interrupts.