Azure / azure-spatial-anchors-samples

Azure Spatial Anchors sample code
Other
293 stars 139 forks source link

Exception caught when running SpatialAnchorManager.StartSessionAsync() #241

Closed TheDrDisco closed 3 years ago

TheDrDisco commented 3 years ago

Hey guys, this issue has been posted before by someone else but there was no resolution. Essentially, I have issues starting a session. I get an exception. I also get this same exception when running the ASA basic sample.

The exception reads as: LogException @ /Users/runner/work/1/s/SpatialServices/AP/I/src/Android/AureSpatialAnchorsApiNdk/Platform/ARCoreNdkPlatformSession.cpp: 26, SpatialServices::ARCoreNdkPlatformSession::ARCoreNdkPlatformSession(ssc_platform_session_handle) : Assertion failed : m_platformSessionHandle != nullptr Callstack: ReposeStackWalker is not supported on this platform :-(. Request CV: . Response CV: .

Here's the code where the SessionStartAsync is called where the exception is caught

`public override async Task CreateAttractionAnchor() { try { LoadingText.text = "Initializing listeners"; await InitializeListeners(); await Task.Delay(300);

            if (CloudManager.Session == null)
            {
                LoadingText.text = "Creating a new session";
                await CloudManager.CreateSessionAsync();
                await Task.Delay(300);
            }

            LoadingText.text = "Starting session";
            await CloudManager.StartSessionAsync();
            await Task.Delay(500);

            LoadingScreen.SetActive(false);
            isPlacingObject = true;
            isSearchingForFirstPlane = true;
            FeedbackText.text = "Move your device around slowly to scan the environment";
            videoPlayerManager.PlayFindAPlane();
            while (!isObjectPlaced)
            {
                await Task.Delay(300);
            }

            //wait for user to set flag
            while (!isReadyToStartSaveAnchor)
            {
                await Task.Delay(300);
            }
            FeedbackText.text = "Saving anchor to cloud";
            await SaveCurrentObjectAnchorToCloudAsync();

            //After saving anchor
            LoadingText.text = "Saving " + attractionName + " attraction to server";
            await SaveAttractionAsync();

            LoadingText.text = "Done! " + attractionName + ", " + attractionDesc + ", data:" + texData.Length;
            await Task.Delay(5000);
        }
        catch (Exception ex)
        {
            isErrorActive = true;
            LoadingText.text = "Exception caught: " + LoadingText.text + " " + ex.Message;
            await Task.Delay(3000);
            LoadingScreen.SetActive(false);
        }
    }`
msftradford commented 3 years ago

Thanks for bringing this to our attention @TheDrDisco! Since you were able to reproduce this with the basic demo sample, it makes sense for us to focus on getting that working correctly first since I can run that same code. What version of the ASA SDK are you using? Does this only happen on Unity/Android? Does the exact same error message appear in the same location for the sample code? Have you attempted to deploy either of the native Android sample apps? Have you attempted to run the Unity sample on other devices (like iOS)?

TheDrDisco commented 3 years ago

Hey there! Currently, I'm using ASA SDK v. 2.7.2 on Unity 2019.4.14f1 and I've only tried using it for Android. I haven't tried it for iOS but will let you know if I do!

I'm mostly testing on a samsung galaxy note 10 lite but I've reproduced the exact same error on one other device that I test with and at the same location, and no I've not attempted to deploy the Android native sample app. It's strange but something worth noting is that the application works more or less, half the time but other times, I'll get this error.

nikhilsawlani commented 3 years ago

Hi @msftradford,

We are facing the same issue (App crashes or It doesn’t create an Anchor) using Unity SDK and the issue is on Android and iOS both, ASA version 2.7.2

This generally happens in the second session SpatialAnchor when we call await CloudManager.StartSessionAsync(); In the first session of SpatialAnchor, everything seems fine.

Just to add we are calling ARSession.Reset() before every SpatialAnchor session, and if don’t call ARSession.Reset() everything seems to work fine

msftradford commented 3 years ago

Thanks @TheDrDisco and @nikhilsawlani for the info. I want to make sure that we are talking about the same issue here. @nikhilsawlani, does your issue also occur when you run the ASA basic sample or does it only repro in your app? @TheDrDisco, does your issue occur every time you try to start the session? Does it happen more often on subsequent session starts?

nikhilsawlani commented 3 years ago

@msftradford, Yes I'm using the Demo Sample Script/Scenes provided in Unity SDK, the issues appear when I Reset my AR Session before calling ASA Session in the subsequent sessions

TheDrDisco commented 3 years ago

@TheDrDisco, does your issue occur every time you try to start the session? Does it happen more often on subsequent session starts?

The issue only occurs when I try to start the session but sometimes (about half the time) it works fine, so I've got little clue about what could be causing it. For an anchor relationship kind of activity where the session stops and starts again, it usually doesn't happen on subsequent sessions if it manages to work the first time.

msftradford commented 3 years ago

@TheDrDisco, we're having a tough time reproducing the issue so let me make sure that we're on the same page. To repro you are deploying the ASA (SDK 2.7.2) Unity (2019.4.14f1) sample to Android device(s) and stepping through the basic demo. When you get to the part of the demo where it calls await CloudManager.StartSessionAsync(); the very first time, you receive the exception about half of the time. Is that all correct?

nikhilsawlani commented 3 years ago

@msftradford in our case we call the ARFoundation method arSession.Reset(); and wait until tracking begins then we call await CloudManager.StartSessionAsync();` and we get exception after the 1st session.

TheDrDisco commented 3 years ago

@msftradford yes that is right. But I'd like to add one more thing to clarify: when this exception is thrown on either one of the app I'm developing or on the basic demo, it will be thrown on the other one too. Like for example, if my app throws this exception, then when I launch the basic demo, I'll also get the exception. When the app works, the basic demo also works.

So that leads me to believe this could be an issue with the SDK or possibly the azure spatial anchors service

The issue seems to occur quite randomly and I can't predict when it would throw this error.

msftradford commented 3 years ago

@TheDrDisco that sounds pretty strange. If this is happening across multiple apps, it sounds a bit like ARCore is getting stuck in a weird state. I would expect that each app would behave independently with respect to ASA because they are creating brand new sessions for each run. Can you please let us know the version numbers for the ARFoundation and ARCore plugins that you are using? What is the version of your Google Play Services for AR? Have you attempted to uninstall and reinstall Google Play Services for AR? Does your app code do anything explicitly with ARSession or ARAnchorManager (ex: @nikhilsawlani mentioned calling ARSession::Reset() can trigger this exception)? Can you enable diagnostics logging and send us the logs for the sessions before and during a time when the exception is fired?

wyliefoxxx commented 3 years ago

Hey ya'll, I've got to bring this issue back up...

I'm getting the same error message when calling CreateSessionAsync() ->

InvalidOperationException: LogException @ D:/a/_work/1/s/SpatialServices/API/src/Android/AzureSpatialAnchorsApiNdk/Platform/ARCoreNdkPlatformSession.cpp:27, SpatialServices::ARCoreNdkPlatformSession::ARCoreNdkPlatformSession(ssc_platform_session_handle) : Assertion failed: m_platformSessionHandle != nullptr CallStack: ReposeStackWalker is not supported on this platform :-(. Request CV: . Response CV: . at Microsoft.Azure.SpatialAnchors.NativeLibraryHelpers.CheckStatus (System.IntPtr handle, Microsoft.Azure.SpatialAnchors.status value) [0x00000] in <00000000000000000000000000000000>:0 at Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorManager.CompleteARFoundationInitialization () [0x00000] in <00000000000000000000000000000000>:0 at Microsoft.Azure.SpatialAnchors.Unity.SpatialAnchorManager+d__46.MoveNext () [0x00000] in <00000000000000000000000000000000>:0 at System.Threading.ContextCallback.Invoke (System.Object state) [0x00000] in <00000000000000000000000000000000>:0 at Sy

We're in the process of upgrading our project from: Unity 2019.4.8.f1 MRTK 2.4 ASA 2.7

to: Unity 2020.3.15f2 MRTK 2.7.2 ASA 2.10.2 Mixed Reality OpenXR Plugin 1.0.1

Spatial anchors had been working well with our previous setup and there has been no code change involving the spatial anchors and session starting. Someone mentioned in https://github.com/Azure/azure-spatial-anchors-samples/issues/227 that they just had to wait until the AR session was tracking before calling this, but I have confirmed in numerous ways that the AR session is indeed in the "SessionTracking" state prior to calling CreateSessionAsync(), yet we are still getting this error message, with no ability to use ASA features.

I've tried uninstalling and reinstalling 'Google Play Services for AR' as @msftradford mentioned to no avail.

The packages used in our project are: image

One worry, is that the device I'm using, Android - Samsung Galaxy S9, is not compatible with the new OpenXR framework. If that's the case, I'd love any advice on using ASA 2.10+ on older devices, as ASA 2.7 with our older setup worked well on the same device. I'm ready to upgrade, but would like to keep as much compatibility with older devices as possible. I'm frankly not sure how much OpenXR is coming into play however, as the guide on this page shows guidance for Unity 2020 + Windows XR Plugin.

I've been trying to replicate our hybrid MRTK - ASA setup with the demo project from git, and from that guide (mentioned above) for use with the MRTK, but have not yet been able to build out a working demo as of yet - although I will continue trying as time permits throughout the week, to build out different combinations that mimic our setup and report any extra findings.

One thing that caught my eye, on this page is this warning: image

Adding both the AR Camera Manager and AR Session to the MixedRealityToolkit object causes all kinds of problems with our current setup, but I'm wondering if this is completely necessary for the new ASA 2.10 and above in all cases. I will be trying to rearrange our project to conform to the guidance from this warning, but this has proven to be a bit of a task with our complicated setup. I'll report back with any findings.

We're super excited to assess the accuracy improvements touted in the 2.10 release notes, so we greatly appreciate any help or guidance as we all ride on this cutting-edge into the future of the metaverse! :) Thanks!

vastja commented 3 years ago

Hi, I encountered the same behavior as you @wyliefoxxx . I think it is related to #283. I edited my answer there to address this issue too.

wyliefoxxx commented 3 years ago

@vastja awesome, I just saw it, thanks a lot, I'll update both of these threads with my results.. I/we owe you a few beers ;)

JShull commented 3 years ago

Hey all - having the same issue on iOS.

Spatial Anchor 2.10.2 MRTK 2.7.2 AR Foundation 4.1.7 ARKit 4.1.7 OpenXR 1.2.8

I am using the exact tutorial information from https://docs.microsoft.com/en-us/windows/mixed-reality/develop/unity/tutorials/mr-learning-asa-02?tabs=openxr

I move the cube, click the start azure session button and it throws the error.

Screen Shot 2021-08-26 at 8 06 44 PM
JShull commented 3 years ago

I'm going to attempt to follow the directions written up by @vastja under #283 and will loop back around to see if that solves the iOS issues - on the surface this looks to be a similar issue as I had problems with the Azure OpenXR package originally (just removed it - lol) and it solved my compiling problems but appears that might have not been the best policy/plan 😄

johannesvollmer commented 3 years ago

I'm having the same Issue, but for the first start of the session, before even calling any reset or destroy function, 100% of the time.

Unity Editor Version: 2021.1.21f1 Build Target: Android (Galaxy A40) Building with: Windows 10 Spatial Anchors Version: 2.10.2 AR Foundation Version: 4.1.7 No MRTK (as far as I know) No OpenXR (Only ARCore and ARKit) Unity XR Management Version: 4.1.0

grafik

Debug.Log("hooking up session events...");
Assert.IsNotNull(this.spatialAnchors, "spatial anchors does not exist");
Assert.IsNull(this.spatialAnchors.Session, "spatial anchors session already exists");
Assert.IsFalse(this.spatialAnchors.IsSessionStarted, "spatial anchors session already runs");

this.spatialAnchors.SessionCreated += (_, eventArgs) => Debug.Log("Spatial Anchors OnSessionCreated");
// ... a bunch more of those hookups ....

Debug.Log("creating  session...");
await this.spatialAnchors.CreateSessionAsync();

Debug.Log("starting session...");

try {
    await this.spatialAnchors.StartSessionAsync();
}
catch (Exception exception) {
    // this catches the error from the OP, namely `Assertion failed: m_platformSessionHandle != nullptr`
    Debug.Log("error while starting session");
    Debug.LogException(exception);
    throw;
}

Assert.IsNotNull(this.spatialAnchors.Session, "spatial anchors did not create session");
Assert.IsTrue(this.spatialAnchors.IsSessionStarted, "spatial anchors session was not started");

This is the log output: grafik

johannesvollmer commented 3 years ago

Trying to solve the issue, I tried:

  1. I will try to apply the suggested workaround from the mentioned issue:

    You need to download the com.microsoft.azure.spatial-anchors-sdk.core from here, expand the tgz content into a folder, and then import the package to your project using these instruction. Then edit the Runtime\Scripts\SpatialAnchorManager.cs and SpatialAnchorExtensions.cs files inside ASA package, and replace instances of

    if MIXED_REALITY_OPENXR

    with

    if (UNITY_WSA || WINDOWS_UWP) && MIXED_REALITY_OPENXR

    Assuming this is the source of the bug, how long would it take for the azure spatial anchors team to fix this? Is there a new release planned? Maybe a hot fix? The last release is from July.

    Update: It didn't work.

  2. Trying the other work-around now:

    Remove MIXED_REALITY_OPENXR directive form asmdef

    Update: Also didn't work

  3. Using older XR Plugin Management Version 4.0.7 Also didn't work.

  4. Using Unity 2020.3.16f didn't work

wyliefoxxx commented 3 years ago

@johannesvollmer did you try the two workarounds suggested by @vastja, involving moving the library folders to the packages folder on https://github.com/Azure/azure-spatial-anchors-samples/issues/283 ? Fixed my issue without any of the steps you suggested in your last comment

TheDrDisco commented 3 years ago

Hello everybody, it's me again, been a while. Unfortunately @vastja 's solution would not work for me because I'm not using MRTK. But on the bright side, I've discovered the reason why I'm getting this error.

It turns out that the ASA won't start properly because the AR Session is not tracking. Have to move the device around in a lit environment so that the AR Session starts tracking. Only then can you start the SpatialAnchorManager.StartSessionAsync() method.

Discussed further here: #203

johannesvollmer commented 3 years ago

@wyliefoxxx I have tried the suggested work-arounds, they did not work for me. Fortunately, after a lot of fiddling, it now works. I assume it is because the code now waits for the ARFoundation ARSession to start, before starting the Azure session. This is strange because the source code of the SpatialAnchorManager.cs looks like it should be able to handle that, but maybe there is a bug.

msftradford commented 3 years ago

Thanks for the updates on this, we'll take a look (#36746853).