fix(spans): Avoid unpack() crashes when merging sets#113442
Merged
Conversation
unpack() can crash if it gets more than 200 arguments. ref INC-2130
lvthanh03
approved these changes
Apr 20, 2026
Comment on lines
+60
to
70
| -- at dest_key can be massive. if it is, SUNIONSTORE will copy the | ||
| -- entire target set again, which appears to be _worse_ than | ||
| -- copying the source set into lua memory and out again. | ||
| redis.call("sadd", dest_key, unpack(members)) | ||
| end | ||
| until cursor == "0" | ||
| end | ||
|
|
||
| local num_spans = ARGV[1] | ||
| local parent_span_id = ARGV[2] | ||
| local has_root_span = ARGV[3] == "true" |
Contributor
There was a problem hiding this comment.
Bug: The unpack(hset_args) call in the Lua script may exceed the argument limit if num_spans is large, as there is no explicit chunking for the hset operation.
Severity: HIGH
Suggested Fix
Apply a similar batching mechanism to the hset call as was done for the sadd calls in the same script. This involves iterating over the arguments in chunks to avoid exceeding the Lua stack limit when calling unpack.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/sentry/scripts/spans/add-buffer.lua#L43-L70
Potential issue: The Lua script at `src/sentry/scripts/spans/add-buffer.lua` uses
`redis.call("hset", ..., unpack(hset_args))`. The `unpack` function has a stack limit of
around 8000 arguments. Since `hset_args` contains two entries per span, if the number of
spans exceeds approximately 3999, the call will fail with a stack overflow, causing a
runtime crash. While other calls like `sadd` were updated to handle large numbers of
arguments by batching, this `hset` call was not, despite the `max_spans_per_evalsha`
setting defaulting to an unlimited value (0). This appears to be an oversight.
Also affects:
src/sentry/scripts/spans/add-buffer.lua:124
Did we get this right? 👍 / 👎 to inform future reviews.
Member
Author
There was a problem hiding this comment.
we limit EVALSHA on the caller side
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.
unpack() can crash if it gets more than 200 arguments.
ref INC-2130