Skip to content

Disable EventSource calls to avoid IL2026 trimming errors#5388

Closed
anuchandy wants to merge 1 commit intoAzure:msdata/aotfrom
anuchandy:event-src-aot-1
Closed

Disable EventSource calls to avoid IL2026 trimming errors#5388
anuchandy wants to merge 1 commit intoAzure:msdata/aotfrom
anuchandy:event-src-aot-1

Conversation

@anuchandy
Copy link
Copy Markdown
Member

@anuchandy anuchandy commented Sep 9, 2025

[Internal] Telemetry : Disable EventSource calls to avoid IL2026 trimming errors

Description

Comment out EventSource calls to resolve IL2026 trimming errors

  • Comment out WriteEventCore calls in DocumentClientEventSource to fix
    IL2026 trim analysis errors when building trimmed applications
  • EventSource.WriteEventCore has RequiresUnreferencedCodeAttribute which
    breaks functionality during trimming as it serializes the whole object graph
  • This issue appears in trimmed builds but not AOT builds due to different
    optimization levels
  • Safe to disable for preview trimmed versions that don't rely on EventSource telemetry

Description

Please include a summary of the change and which issue is fixed. Include samples if adding new API, and include relevant motivation and context. List any dependencies that are required for this change.

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • [] New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update

Closing issues

To automatically close an issue: closes #IssueNumber

// must be explicitly be set before writing the event.
CustomTypeExtensions.SetActivityId(ref activityId);

this.WriteEventCore(eventId, eventDataCount, dataDesc);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This works, but I think what you should do is add RequiresUnreferencedCode to this method, then look at the callers. That is, WriteEventCore is annotated with

 [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("EventSource will serialize the whole object graph. Trimmer will not safely handle this case because properties may be trimmed. This can be suppressed if the object is a primitive type")]

I think the same thing applies here. The responsibility is on the callers to ensure that they're either not calling this in any trim-compatible code path, or they are correctly suppressing the warning because they're only serializing primitive types.

dataDesc[32].DataPointer = (IntPtr)(fixedXDate);
dataDesc[32].Size = (xDate.Length + 1) * UnicodeEncodingCharSize;

this.WriteEventCoreWithActivityId(activityId, 1, eventDataCount, dataDesc);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All of these inputs are primitive types, so this should be OK to suppress with UnconditionalSuppressMessage on the parent method.

dataDesc[29].DataPointer = (IntPtr)(fixedVersion);
dataDesc[29].Size = (version.Length + 1) * UnicodeEncodingCharSize;

this.WriteEventCoreWithActivityId(activityId, 2, eventDataCount, dataDesc);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here, it looks like these are all primitive types (Guid, short, double, string).

@agocke
Copy link
Copy Markdown

agocke commented Sep 9, 2025

What concerns me here is that this should be generating warnings before you ever get to MCP. Is this project annotated with <IsAotCompatible>true</>?

@agocke
Copy link
Copy Markdown

agocke commented Sep 9, 2025

Ah, I see the problem -- this project only builds for netstandard2.0. I will try to add a net8.0 target.

@anuchandy
Copy link
Copy Markdown
Member Author

closing this based on the offline discussion with Andy, he is working on a pr to address this by adding annotations

@anuchandy anuchandy closed this Sep 9, 2025
agocke added a commit to agocke/azure-cosmos-dotnet-v3 that referenced this pull request Sep 10, 2025
Same idea as Azure#5388, but this provides appropriate attributes to mark the use as safe.

The long-term goal here should be to add a net8 target to validate that this is correct.
ananth7592 pushed a commit that referenced this pull request Sep 10, 2025
Same idea as #5388,
but this provides appropriate attributes to mark the use as safe.

The long-term goal here should be to add a net8 target to validate that
this is correct.
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.

3 participants