fix: __del__ not freeing up native resources#38
Open
acoroleu-tempus wants to merge 4 commits intoextism:mainfrom
Open
fix: __del__ not freeing up native resources#38acoroleu-tempus wants to merge 4 commits intoextism:mainfrom
acoroleu-tempus wants to merge 4 commits intoextism:mainfrom
Conversation
When Plugin creates its own CompiledPlugin internally, it should also free it in __del__. Added _owns_compiled_plugin flag to track ownership and avoid freeing externally-passed CompiledPlugin instances. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Author
|
@nilslice in case you can take a look, thanks! |
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.
extism/extism#890
Summary
Fix memory leaks caused by del methods not properly freeing native resources.
Bug 1:
Plugin.__del__never calledextism_plugin_freeThe guard check used hasattr(self, "pointer") but Plugin stores the handle in self.plugin, not self.pointer. This caused the method to
always return early, never freeing the native plugin.
Bug 2: Double-free errors in all
__del__methodsWhen del is called multiple times (e.g., via context manager exit then garbage collection), the code would attempt to free
already-freed resources, causing TypeError or segfaults.
Changes
•
Plugin.__del__: Fix attribute check ("pointer" → "plugin") and add -1 guard•
CompiledPlugin.__del__: Add -1 guard to prevent double-free•
_ExtismFunctionMetadata.__del__: Add None guard and set pointer = None after freeingImpact
This fix enables using extism in long-running services that create fresh plugin instances per request, which previously caused unbounded
memory growth.
Test plan
• Added test_plugin_del_frees_native_resources - verifies Plugin cleanup via context manager
• Added test_compiled_plugin_del_frees_native_resources - verifies CompiledPlugin cleanup
• Added test_extism_function_metadata_del_frees_native_resources - verifies function metadata cleanup
• All tests verify that del can be safely called multiple times