fix: Prevent crash due to concurrent map access in Go plugin log processing #1284
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.
Issue Description
A fatal error, "concurrent map read and map write," occurs when using Go plugins under high log volume and concurrent conditions. The root cause of this issue is traced back to the ReceiveLogGroup function in plugin_runner_v1.go. The context passed to subsequent log processing did not undergo a deep copy, leading to the use of the same map object.
Problematic Scenario
In the aforementioned function, while the context map is being assigned new values, the Add function in aggregator_context.go is concurrently accessing values from the same context. This simultaneous operation on the same map object results in concurrent read and write errors, which is not permissible in Go without proper synchronization mechanisms.
Resolution
To address this problem, a deep copy of the context map is now implemented for each log processing instance. By doing so, each context uses a separate map object, eliminating the reuse of the same map and thereby preventing concurrent read and write conflicts. This deep-copy approach ensures that each log processor operates on an independent copy of the context, maintaining the integrity of the map access throughout the logging system.