Skip to content

Fix memory leak on interrupt notifications#252

Merged
fhunleth merged 1 commit into
mainfrom
memory-leak
Jun 13, 2026
Merged

Fix memory leak on interrupt notifications#252
fhunleth merged 1 commit into
mainfrom
memory-leak

Conversation

@fhunleth

Copy link
Copy Markdown
Member

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.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_message to build messages in a dedicated msg_env, copy gpio_spec into that env, and enif_clear_env(msg_env) after sending.
  • Adjust the cdev interrupt poller thread to allocate a reusable msg_env once and reuse it across events.
  • Update the stub HAL notification path to allocate/free a per-send msg_env around 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.

@fhunleth fhunleth merged commit a0e7fd5 into main Jun 13, 2026
9 of 10 checks passed
@fhunleth fhunleth deleted the memory-leak branch June 13, 2026 01:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants