Skip to content

Commit bbefdd3

Browse files
committed
Prevent Mechanism.TerminalKey from being serialized
1 parent 4c24f51 commit bbefdd3

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

src/Sentry/Internal/MainExceptionProcessor.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ value is string stringValue &&
122122
sentryEvent.Contexts[key[ExceptionDataContextKey.Length..]] = value;
123123
keysToRemove.Add(key);
124124
}
125+
else if (key.Equals(Mechanism.TerminalKey, StringComparison.OrdinalIgnoreCase))
126+
{
127+
// Terminal is SDK-internal only, used for session tracking. Don't send to Sentry.
128+
// Skip it - will be removed during serialization in Mechanism.WriteTo()
129+
}
125130
else if (key.StartsWith(ExceptionDataKeyPrefix, StringComparison.OrdinalIgnoreCase))
126131
{
127132
sentryEvent.SetExtra($"Exception[{i}][{key}]", value);
@@ -202,6 +207,13 @@ private static Mechanism GetMechanism(Exception exception, int id, int? parentId
202207
// Add HResult to mechanism data before adding exception data, so that it can be overridden.
203208
mechanism.Data["HResult"] = $"0x{exception.HResult:X8}";
204209

210+
// Copy Terminal flag for internal processing, but remove it from Exception.Data
211+
if (exception.Data[Mechanism.TerminalKey] is bool terminal)
212+
{
213+
mechanism.Data[Mechanism.TerminalKey] = terminal;
214+
exception.Data.Remove(Mechanism.TerminalKey);
215+
}
216+
205217
// Copy remaining exception data to mechanism data.
206218
foreach (var key in exception.Data.Keys.OfType<string>())
207219
{

src/Sentry/Protocol/Mechanism.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ public sealed class Mechanism : ISentryJsonSerializable
3232
/// <summary>
3333
/// Key found inside of <c>Exception.Data</c> describing whether the exception is considered terminal.
3434
/// </summary>
35-
/// <remarks> It's not prefixed with 'Sentry' so the MainExceptionProcessor does not remove it. </remarks>
36-
public static readonly string TerminalKey = "Terminal";
35+
/// <remarks>
36+
/// This is an SDK-internal flag used for session tracking and is not sent to Sentry servers.
37+
/// </remarks>
38+
public static readonly string TerminalKey = "Sentry:Terminal";
3739

3840
internal Dictionary<string, object>? InternalData { get; private set; }
3941

@@ -143,7 +145,14 @@ public void WriteTo(Utf8JsonWriter writer, IDiagnosticLogger? logger)
143145
writer.WriteBooleanIfTrue("is_exception_group", IsExceptionGroup);
144146
writer.WriteNumberIfNotNull("exception_id", ExceptionId);
145147
writer.WriteNumberIfNotNull("parent_id", ParentId);
146-
writer.WriteDictionaryIfNotEmpty("data", InternalData!, logger);
148+
149+
// Filter out Terminal flag from Data before serialization (SDK-internal only)
150+
var dataToSerialize = InternalData?.Count > 0
151+
? InternalData.Where(kvp => !kvp.Key.Equals(TerminalKey, StringComparison.OrdinalIgnoreCase))
152+
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)
153+
: null;
154+
writer.WriteDictionaryIfNotEmpty("data", dataToSerialize!, logger);
155+
147156
writer.WriteDictionaryIfNotEmpty("meta", InternalMeta!, logger);
148157

149158
writer.WriteEndObject();

test/Sentry.Tests/Internals/MainExceptionProcessorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void Process_ExceptionWith_TerminalTrue_StoresInMechanismData()
110110
var evt = new SentryEvent();
111111
var exp = new Exception();
112112

113-
exp.Data.Add(Mechanism.TerminalKey, true);
113+
exp.SetSentryMechanism("TestException", terminal: true);
114114

115115
sut.Process(exp, evt);
116116

@@ -127,7 +127,7 @@ public void Process_ExceptionWith_TerminalFalse_StoresInMechanismData()
127127
var evt = new SentryEvent();
128128
var exp = new Exception();
129129

130-
exp.Data.Add(Mechanism.TerminalKey, false);
130+
exp.SetSentryMechanism("TestException", terminal: false);
131131

132132
sut.Process(exp, evt);
133133

test/Sentry.Tests/Protocol/SentryEventTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ public void GetExceptionType_UnhandledTerminalException_ReturnsUnhandled()
210210
[Fact]
211211
public void GetExceptionType_UnhandledTerminalExceptionViaSentryExceptions_ReturnsUnhandled()
212212
{
213-
// Terminal = null or true means default behavior (terminal)
214213
var evt = new SentryEvent
215214
{
216215
SentryExceptions = [new SentryException { Mechanism = new Mechanism { Handled = false } }]

0 commit comments

Comments
 (0)