fix: guard type assertions and nil dereferences, release messagePool on exit#9
Merged
fix: guard type assertions and nil dereferences, release messagePool on exit#9
Conversation
FLBPluginGetContext returns interface{}. The previous code used an
unguarded type assertion .(int) which panics if the stored value is
not an int (e.g. when FlushCtx is called for an instance whose Init
never completed successfully).
Replace with the two-value form and log a warning on mismatch,
returning 0 so the subsequent configMap lookup fails gracefully.
configMap[id] returns nil when the id is not registered. The previous code dereferenced config unconditionally, causing a panic. Add ok-pattern check on the map lookup and a bounds check on the stream slice before accessing index 0.
…nded growth messagePool is a global sync.Map keyed by protoreflect.MessageDescriptor pointer identity. Entries are created lazily on the first Flush call for each descriptor and were never removed, so descriptor objects (and their associated *dynamicpb.Message caches) could not be GC'd after a config was torn down. Add cleanupMsgPool() which traverses the full descriptor tree rooted at a given MessageDescriptor and calls sync.Map.Delete for each node, including nested STRUCT sub-message descriptors. Call it at the end of FLBPluginExitCtx so that each config's pool entries are released when the output instance is shut down. Add TestCleanupMsgPool covering flat and nested schemas to verify that both the top-level and sub-message pool entries are removed after cleanup.
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
Fix two panic risks and a memory growth issue identified in runtime analysis.
Fix two panic risks and a memory growth issue identified in runtime analysis.
getFLBPluginContextwith thetwo-value form to avoid
panic: interface conversionon unexpected context values.getOffset()(test-only helper) against nil map lookup andempty slice access.
cleanupMsgPool()and call it inFLBPluginExitCtxto deletemessagePoolentries for the config's descriptor tree on shutdown, preventingunbounded memory growth across init/exit cycles.