Skip to content
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

Running StereoKit from an async Main #361

Open
sandrist opened this issue Apr 16, 2022 · 6 comments
Open

Running StereoKit from an async Main #361

sandrist opened this issue Apr 16, 2022 · 6 comments

Comments

@sandrist
Copy link
Contributor

Description

I recently updated our apps to use the latest StereoKit nuget (0.3.6-preview.5), but it caused them to crash/hang immediately on startup. I think the problem has to do with the fact that we use an async Main() (static async Task Main()) rather than void Main(). We can work around it by isolating our async stuff and using a regular void Main(), but I thought it was strange. The exception seems to be thrown somewhere deep in the OpenXR native code during initialization, but it's a bit hard to trace. Is this a known issue?

Platform / Environment

Observed on ARM and ARM64 (UWP) deploying to the HoloLens 2.

@paulmelis
Copy link
Contributor

I encountered a similar issue in #338, although that wasn't on UWP. But there might very well be an issue with async Main, but only @maluoi would known why 😄

@maluoi
Copy link
Collaborator

maluoi commented Apr 17, 2022

To be quite honest, I didn't know you could make Main async until @paulmelis mentioned it, and I'm still not entirely sure what the ramifications of that are. It sounds like this may be a more common scenario than I had initially imagined, so I guess it's time for me to do a deeper dive on that! Will let you all know how that goes :)

@maluoi
Copy link
Collaborator

maluoi commented Apr 19, 2022

Okay, so I learned something new today! It seems as though await calls can cause your code to switch threads, even in cases that may intuitively look otherwise!

So using an await between SK.Initialize and SK.Run/Step can mean execution is no longer on the same thread. Initialize and Run/Step both expect to be run from the same thread, which is a bit of a requirement for how graphics works. This may not have been an issue before (it would be a problem for GL, but DX would be okay with it), but with the async assets feature, I've been trying to do some smarter thread handling, and hadn't handled this case!

I think the most I can do in this case is double check the thread Id is the same from Initialize and Run/Step, and fail vocally if not.

@sandrist
Copy link
Contributor Author

Interesting! I did not know that either, but it explains a lot.

HoloLens requires a lot of async API calls from various UWP libraries for reading/writing files and folders, accessing spatial anchors, etc. We're using such calls for additional initialization between SK.Initialize and SK.Run, which led to the whole async Main business. So it seems like this might not be all that uncommon of a situation for people to potentially run into? Certainly an informative error message will help!

maluoi added a commit that referenced this issue Apr 28, 2022
…n a separate thread from initialize (or similarly, create GPU assets before step). #361
@maluoi
Copy link
Collaborator

maluoi commented Apr 28, 2022

Okay, I have a pretty good idea how to prevent this in the future, so this situation will continue to improve as async assets develop! In the meantime, I have added a pair of warnings that should help illuminate the situation.

technobaboo pushed a commit to technobaboo/StereoKit that referenced this issue Jul 7, 2022
…n a separate thread from initialize (or similarly, create GPU assets before step). StereoKit#361
@laultman
Copy link

laultman commented Nov 8, 2022

I've been experiencing this async strangeness too, and my Program main() is this.

		public static async Task Main(string[] args)

I too depend on async call between Initialize and Step(run). #512

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

No branches or pull requests

4 participants