Skip to content

perf(modules): cache serialized installed modules mapping - #8418

Open
M-Colley wants to merge 1 commit into
getsentry:masterfrom
M-Colley:fix/issue-8090-modules-cache
Open

M-Colley wants to merge 1 commit into
getsentry:masterfrom
M-Colley:fix/issue-8090-modules-cache

Conversation

@M-Colley

Copy link
Copy Markdown

Description

ModulesIntegration previously attached the raw _get_installed_modules() dictionary to every error event, causing serialize() to walk every installed distribution entry on each captured error event. This change caches the serialized modules dictionary in sentry_sdk/integrations/modules.py keyed by source mapping identity, max_value_length, and custom_repr. Specifically, each event receives a _SerializedModules wrapper whose __sentry_repr__() method returns a fresh plain dict during serialize(), isolating per-event mutations in before_send and re-serializing under the modules path if an event processor mutates the mapping. When serialization produces _meta annotations, _get_serialized_modules() falls back to returning a plain copy so serialize() records event metadata normally. Regression tests in tests/integrations/modules/test_modules.py verify single-pass module serialization, before_send and event processor mutation isolation, and max_value_length truncation.

Issues

Fixes getsentry#8090

Signed-off-by: Mark Colley <39366189+M-Colley@users.noreply.github.com>
@M-Colley
M-Colley requested a review from a team as a code owner November 28, 2026 10:48
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 28, 2026
@github-actions

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit 7472999.

Main concerns: data sent to Sentry, host performance, reach.

This label is a hint for reviewers, not an approval.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 3 potential issues.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Want higher recall? High effort reviews run extra passes and find more bugs. A team admin can switch effort levels in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 7472999. Configure here.

{"modules": dict(self)},
max_value_length=self._max_value_length,
custom_repr=self._custom_repr,
)["modules"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutation path drops truncation metadata

Medium Severity

When an event processor mutates event["modules"], __sentry_repr__ re-serializes through a nested serialize() call and returns only the inner modules mapping. Truncation annotations from that call never reach the outer event, so _meta for shortened module values is omitted. The cache path already falls back to a plain copy to preserve those annotations.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7472999. Configure here.

super().__init__(data)
self._max_value_length = max_value_length
self._custom_repr = custom_repr
self._mutated = False

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dict subclass rejects no-arg construction

Medium Severity

_SerializedModules.__init__ requires data, so same-type construction with no arguments fails. Standard dict operations that do that (copy(), |, copy.copy(), deepcopy) raise TypeError on event["modules"]. If an event processor hits one of those, the callback error drops the event.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7472999. Configure here.

{"modules": dict(self)},
max_value_length=self._max_value_length,
custom_repr=self._custom_repr,
)["modules"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nested serialize clears capture-event guard

Low Severity

Mutated modules call serialize() from inside the outer serialize(). The inner finally sets disable_capture_event back to False while the outer walk is still running, so later event fields serialize with capture enabled again.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 7472999. Configure here.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: ModulesIntegration output is re-serialized on every error event

1 participant