Skip to content

(WIP) feat: add sentry_merge_context#1835

Open
JoshuaMoelans wants to merge 5 commits into
masterfrom
joshua/feat/merge_context
Open

(WIP) feat: add sentry_merge_context#1835
JoshuaMoelans wants to merge 5 commits into
masterfrom
joshua/feat/merge_context

Conversation

@JoshuaMoelans

Copy link
Copy Markdown
Member

Adding this for downstream SDKs, so they can avoid overwriting pre-set context from our native SDK init.

Popped up as part of https://github.com/getsentry/sentry-xbox/pull/151 where the unreal integration test calls AddDeviceContext which does a plain set_by_key, making us lose device.X context set by the console SDK.

@JoshuaMoelans JoshuaMoelans changed the title feat: add sentry_merge_context (WIP) feat: add sentry_merge_context Jul 1, 2026
Comment thread src/sentry_core.c Outdated
Comment on lines +1086 to +1087
sentry__value_merge_objects(context, value);
sentry_value_decref(value);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The return value of sentry__value_merge_objects is ignored, which could lead to silent data loss if the merge operation fails.
Severity: LOW

Suggested Fix

Check the return value of sentry__value_merge_objects. If it indicates an error, consider logging the failure to make the code more robust and aid in debugging potential issues with context merging.

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_core.c#L1086-L1087

Potential issue: In `sentry_merge_context` and `sentry_merge_context_n`, the return
value of the internal function `sentry__value_merge_objects` is not checked. This
function can return an error if it fails, for example, if the destination object is
frozen or if a memory allocation fails. While the analysis indicates that such failure
conditions are unlikely in normal production use, ignoring the return value creates a
defensive coding gap. If a merge operation were to fail, it would do so silently,
potentially leading to incomplete context data being processed without any indication of
an error. The reference counting is correct, so no memory leak is introduced, but data
could be lost.

Also affects:

  • src/sentry_core.c:1101~1102

Did we get this right? 👍 / 👎 to inform future reviews.

Comment thread src/sentry_core.c
Comment thread include/sentry.h
const char *key, size_t key_len, sentry_value_t value);
/**
* Merges a context object with the existing context object.
* Existing keys within the context object will not be overwritten.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hmm, shouldn't downstream values take precedence? For example, passing "Xbox" should override "PC", right? 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In general there are definitely values that should take precedence (e.g., on detecting steamOS for example), but these could then still use the normal SetContext. That would lose the 'merge' for other OS context though, so maybe alternatively we could add a flag to our merge called "overwrite_dst" that says whether to keep values of existing keys, or overwrite them 🤔

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What if we made the OS/Device context extensible by allowing downstream SDKs to provide the initial device context as a starting point, and then internally merge sentry-native's OS context into that?

Comment thread include/sentry.h
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants