Skip to content

[Android] Fix OnIdiom threading issue by removing using statements in DeviceInfo #30288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2025

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 27, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description

This PR fixes a NotImplementedException that occurs when multiple threads access OnIdiom simultaneously on Android devices. The issue was reported on Galaxy XCover 5 with Android 14 but could affect any Android device under concurrent access scenarios.

Root Cause

The problem occurred in DeviceInfoImplementation.Idiom getter where using statements were used with Java objects:

// Line 44 - UiModeManager with using statement
using var uiModeManager = UiModeManager.FromContext(Application.Context);

// Line 69 - DisplayMetrics with using statement  
using var metrics = Application.Context.Resources?.DisplayMetrics;

When multiple threads access OnIdiom simultaneously:

  1. Thread A calls DeviceInfo.Idiom and creates these objects with automatic disposal
  2. Thread B calls DeviceInfo.Idiom and gets the same Java object instances
  3. Thread A completes and disposes the objects via using
  4. Thread B tries to access already-disposed objects → NotImplementedException: Do not know how to dispose: Invalid

Solution

Removed the using keyword from both problematic lines to prevent automatic disposal:

// Fixed - no automatic disposal
var uiModeManager = UiModeManager.FromContext(Application.Context);
var metrics = Application.Context.Resources?.DisplayMetrics;

This eliminates the race condition while allowing normal garbage collection when objects go out of scope.

Verification

  • ✅ All existing OnIdiom tests pass (13 tests)
  • ✅ Builds successfully across Android projects
  • ✅ Code formatted according to repository standards
  • ✅ Minimal, surgical change addressing exactly the identified issue

The fix prevents double-disposal of Java interop objects that caused the threading race condition, resolving the crash when multiple threads access OnIdiom concurrently.

Fixes #29853.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] [Android] OnIdiom throws NotImplementedException [Android] Fix OnIdiom threading issue by removing using statements in DeviceInfo Jun 27, 2025
@Copilot Copilot AI requested a review from PureWeen June 27, 2025 20:11
Copilot finished work on behalf of PureWeen June 27, 2025 20:11
Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

This is worth a try, because the using statements in here weren't helping cross-thread access.

@PureWeen PureWeen marked this pull request as ready for review June 27, 2025 21:21
@PureWeen PureWeen requested a review from a team as a code owner June 27, 2025 21:21
@PureWeen PureWeen requested a review from jfversluis June 27, 2025 21:21
@PureWeen PureWeen changed the base branch from main to inflight/current June 27, 2025 21:21
@PureWeen PureWeen merged commit 205d3a4 into inflight/current Jun 27, 2025
1 check passed
@PureWeen PureWeen deleted the copilot/fix-29853 branch June 27, 2025 21:27
PureWeen added a commit that referenced this pull request Jun 28, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jun 29, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jun 29, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 1, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 2, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 2, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 8, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 8, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
github-actions bot pushed a commit that referenced this pull request Jul 9, 2025
… DeviceInfo (#30288)

* Initial plan

* Fix OnIdiom threading issue by removing using statements in DeviceInfo.android.cs

Co-authored-by: PureWeen <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: PureWeen <[email protected]>
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.

[Android] OnIdiom throws NotImplementedException
3 participants