Fix memory leaks in exception handling for defer, finally, and async join#436
Open
nbeerbower wants to merge 1 commit into
Open
Fix memory leaks in exception handling for defer, finally, and async join#436nbeerbower wants to merge 1 commit into
nbeerbower wants to merge 1 commit into
Conversation
Fix three memory management issues: 1. try/finally: When finally block throws/returns, the saved exception and return values were lost without being released. Now properly retains saved values before finally executes and releases them appropriately based on control flow outcome. 2. defer stack: When a deferred call throws while an exception is pending, the original exception was lost without being released. Now properly retains the saved exception and releases it if the defer throws a new exception. 3. task join: When joining a task that threw an exception, the exception_state was copied without retaining the exception_value, leading to incorrect refcounting. Now retains the exception value when copying from task to caller context. https://claude.ai/code/session_01DfpaWxNKzvp8vMc6sEhLjC
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
This PR fixes memory leaks in the interpreter's exception handling by properly retaining and releasing exception values when they are copied between execution contexts or saved across control flow statements.
Key Changes
Concurrency (builtin_join): Added
VALUE_RETAIN()when re-throwing an exception from a joined task to prevent premature deallocation since both the task context and caller context hold references to the exception value.Defer Stack Execution: Added
VALUE_RETAIN()when saving the current exception before defer execution, with correspondingVALUE_RELEASE()calls in both success and failure paths to properly manage the reference count.Try-Finally Statements: Added
VALUE_RETAIN()for both saved return and exception values before executing the finally block, withVALUE_RELEASE()calls in both paths (whether finally restores or changes control flow).Test Coverage: Added three new test cases to verify the fixes:
defer_exception_leak.hml: Tests defer throwing while exception is pendingfinally_exception_leak.hml: Tests finally block throwing a new exceptionjoin_exception_refcount.hml: Tests joining a task that threw an exceptionImplementation Details
The fixes follow a consistent pattern: when an exception value is saved and the execution context may change (through defer execution, finally blocks, or async task joining), the value must be explicitly retained to maintain a reference count that accounts for multiple holders. The corresponding release calls ensure the reference count is decremented when the saved value is either restored or discarded.
This prevents use-after-free errors and memory leaks that occurred when exception values were freed prematurely while still being referenced by other contexts.
https://claude.ai/code/session_01DfpaWxNKzvp8vMc6sEhLjC