maui
maui copied to clipboard
iOS 17.4, Barometer.Default.Start hangs indefinately
Description
I have an app that uses the barometer and initializes it upon app startup. On iOS 17.4, calling Barometer.Start() results a hang. Using the debugger, we can see the exception thrown in the output window. The exception is not bubbled out to the caller, so it cannot be handled.
Steps to Reproduce
Using this code, running on iOS 17.4, the call to Start() hangs indefinitely
var _isBarometerEnabled = Barometer.Default.IsSupported; if (_isBarometerEnabled) { Barometer.Default.ReadingChanged += Barometer_ReadingChanged; Barometer.Default.Start(SensorSpeed.Default); }
Link to public reproduction project repository
No response
Version with bug
8.0.6 SR1
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
8.0.6 SR1
Affected platforms
iOS
Affected platform versions
iOS 17.4
Did you find any workaround?
No workaround, other than not using the Barometer
Relevant log output
*** Terminating app due to uncaught exception 'System.NullReferenceException', reason: 'Object reference not set to an instance of an object. (System.NullReferenceException)
at Microsoft.Maui.Devices.Sensors.BarometerImplementation.<PlatformStart>g__LocationManagerUpdatedHeading|19_0(CMAltitudeData e, NSError error)
at ObjCRuntime.Trampolines.SDActionArity2V67.Invoke(IntPtr block, NativeHandle arg1, NativeHandle arg2) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/ObjCRuntime/Trampolines.g.cs:line 34164
at CoreMotion.CMAltimeter.StartRelativeAltitudeUpdates(NSOperationQueue queue, Action`2 handler) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/CoreMotion/CMAltimeter.g.cs:line 117
at Microsoft.Maui.Devices.Sensors.BarometerImplementation.PlatformStart(SensorSpeed sensorSpeed)
at Microsoft.Maui.Devices.Sensors.BarometerImplementation.Start(SensorSpeed sensorSpeed)
'
*** First throw
sers/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/ObjCRuntime/Trampolines.g.cs:line 34164
at CoreMotion.CMAltimeter.StartRelativeAltitudeUpdates(NSOperationQueue queue, Action`2 handler) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/CoreMotion/CMAltimeter.g.cs:line 117
at Microsoft.Maui.Devices.Sensors.BarometerImplementation.PlatformStart(SensorSpeed sensorSpeed)
at Microsoft.Maui.Devices.Sensors.BarometerImplementation.Start(SensorSpeed sensorSpeed)
'
*** First throw call stack:
(0x1a04ceb28 0x19833ef78 0x100ea21e0 0x100ea1d00 0x100ea1cb0 0x1011bc130 0x1011baca8 0x1011e1abc 0x1011dfd80 0x1011dad34 0x1011d2854 0x100e71a18 0x100eb6044 0x1011e2264 0x1011e0bac 0x1011d55b8 0x1011d3184 0x1011a1e10 0x1011495dc 0x10115e8b4 0x1fc3cda90 0x1fc3ccfcc)
*** Terminating app due to uncaught exception 'System.NullReferenceException', reason: 'Object reference not set to an instance of an object. (System.NullReferenceException)
at Microsoft.
vices.Sensors.BarometerImplementation.Start(SensorSpeed sensorSpeed)
Are you testing this on an actual device or the Simulator? I can imagine this is one thing that is not implemented in the Simulator. Also you indicate that this has worked in previous .NET MAUI versions? Can you confirm that? Do you know which version exactly?
Hi @sbloom82. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
Hi @sbloom82. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md
This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.
I have same problem but on native framework, it cause by the iOS so you must report to Apple.
Are you testing this on an actual device or the Simulator? I can imagine this is one thing that is not implemented in the Simulator.
No, this is not a simulator issue. I see the NullReferenceException on a real device after updating to iOS 17.4 ... (iPad Air 4th gen)
at CoreMotion.CMAltimeter.StartRelativeAltitudeUpdates(NSOperationQueue queue, Action`2 handler) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/build/dotnet/ios/generated-sources/CoreMotion/CMAltimeter.g.cs:line 117 at Microsoft.Maui.Devices.Sensors.BarometerImplementation.PlatformStart(SensorSpeed sensorSpeed) at Microsoft.Maui.Devices.Sensors.BarometerImplementation.Start(SensorSpeed sensorSpeed)
Seems like the interface of CMAltitudeHandler changed, which is a parameter of CMAltimeter.StartRelativeAltitudeUpdates:
https://developer.apple.com/documentation/coremotion/cmaltitudehandler?changes=latest_minor
Are you testing this on an actual device or the Simulator? I can imagine this is one thing that is not implemented in the Simulator. Also you indicate that this has worked in previous .NET MAUI versions? Can you confirm that? Do you know which version exactly?
We encountered this issue in our production app on real devices for users who updated to iOS 17.4 beta. Our app initializes the barometer on startup, so this caused the app to hang on startup and never fully load.
This has worked since we implemented it in our maui 8 app. Users who are not on iOS 17.4 have an app that is working as expected. We had to issue an update to skip barometer initialization for 17.4 ios devices.
Seems like this might have to do with #21057 then maybe?
Our app initializes the barometer on startup, so this caused the app to hang on startup and never fully load.
Hm, why does it hang? I see a hard crash, unfortunately :/
In my case, Barometer.Start is actually inside a try-catch block, but still the NullReferenceException is not caught. So, yes, the only way around this is not to use the barometer.
Seems like this might have to do with #21057 then maybe?
Yes, it's certainly related to API changes in XCode 15.3 / iOS 17.4.
Also you indicate that this has worked in previous .NET MAUI versions? Can you confirm that? Do you know which version exactly?
I'm not aware of any previous MAUI version that works, but it seems that the Barometer class from Xamarin.Essentials does not crash in the same way. In that sense it's a regression wrt Xamarin.
Certainly the Xamarin code suffers from the API change as well. I assume the difference is that the exception is catchable there. I don't understand why it is not in MAUI, but for me this is actually the biggest issue (if the exception was catchble, the API change would be half as bad).
Now that apple has released ios 17.5 (and the 17.4 minor versions), when "Motion & Fitness" permission is not granted, the app will crash after starting the barometer.
Here's all of my work arounds
private async Task InitBarometer()
{
try
{
#if IOS
var version = DeviceInfo.Current.Version;
if (version.Major == 17 && version.Minor == 4)
{
_barometerDisabledReason = "Barometer is disabled for iOS 17.4. Update to iOS 17.5 to enable.";
throw new Exception(_barometerDisabledReason);
}
var status = await Permissions.CheckStatusAsync<Permissions.Sensors>();
if (status != PermissionStatus.Granted)
{
_barometerDisabledReason = "Barometer is disabled. Please grant permission to 'Motion & Fitness' to enable.";
throw new Exception(_barometerDisabledReason);
}
#endif
_isBarometerEnabled = Barometer.Default.IsSupported;
if (_isBarometerEnabled)
{
Barometer.Default.ReadingChanged += Barometer_ReadingChanged;
Barometer.Default.Start(SensorSpeed.Default);
}
}
catch
{
_isBarometerEnabled = false;
}
await Task.CompletedTask;
}
I still see this with Maui 8.0.100, .NET SDK 8.0.404, iOS workload 18.0.8314 and XCode 16.1 (on an iPad with iOS 18.1.1). :frowning_face:
I get a hard crash upon calling Barometer.Start(SensorSpeed.UI):
=================================================================
Native Crash Reporting
=================================================================
Got a SIGABRT while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================