Skip to content

Avoid unsafe string encoder on Android#8637

Open
trask wants to merge 1 commit into
open-telemetry:mainfrom
trask:fix-8636-android-unsafe-string-encoder
Open

Avoid unsafe string encoder on Android#8637
trask wants to merge 1 commit into
open-telemetry:mainfrom
trask:fix-8636-android-unsafe-string-encoder

Conversation

@trask

@trask trask commented Jul 17, 2026

Copy link
Copy Markdown
Member

Avoid probing OpenJDK-specific String fields when selecting the unsafe string encoder on Android. Android exposes sun.misc.Unsafe, but querying its different String internals causes ART to emit an error before OpenTelemetry can fall back to the standard encoder.

Fixes #8636.

@trask
trask requested a review from Copilot July 17, 2026 14:21
Comment on lines +42 to +47
// Android exposes sun.misc.Unsafe, but String does not have the OpenJDK internal fields used by
// UnsafeStringEncoder. ART logs an error when these missing fields are queried, even though the
// exception is caught.
if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
return null;
}

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.

I was torn between adding the check here or in UnsafeStringEncoder.createIfAvailable(). Happy to move if you have a preference.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Jul 17, 2026

Copy link
Copy Markdown

Pull request dashboard status

Status: Waiting on reviewers to review the latest changes.

Copilot AI 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.

Pull request overview

This PR prevents StringEncoderHolder from attempting to initialize the UnsafeStringEncoder on Android, avoiding ART error logs caused by probing OpenJDK-specific String internals while still allowing the normal fallback encoder selection to proceed.

Changes:

  • Short-circuit createUnsafeEncoder() on Android by detecting "Dalvik" via java.vm.name and returning null before touching OpenJDK String internals.
  • Add a regression test that simulates Android by setting java.vm.name=Dalvik and asserting the unsafe encoder is not available.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/StringEncoderHolder.java Avoids attempting unsafe string encoding on Android by checking java.vm.name before probing String internals.
exporters/common/src/test/java/io/opentelemetry/exporter/internal/marshal/StringEncoderTest.java Adds a test ensuring the unsafe encoder is skipped when running under a Dalvik-like VM name.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov

codecov Bot commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.61%. Comparing base (b86f040) to head (40e621d).

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #8637   +/-   ##
=========================================
  Coverage     91.60%   91.61%           
- Complexity    10343    10344    +1     
=========================================
  Files          1013     1013           
  Lines         27352    27354    +2     
  Branches       3215     3215           
=========================================
+ Hits          25057    25059    +2     
  Misses         1567     1567           
  Partials        728      728           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@trask
trask marked this pull request as ready for review July 17, 2026 14:42
@trask
trask requested a review from a team as a code owner July 17, 2026 14:42
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.

StringEncoderHolder tries to create "unsafe encoder" on Android

2 participants