ImmersiveSpace orphaned when WindowGroup closes

Environment

  • visionOS 26.1, Xcode 26.1.1

Problem

When a WindowGroup opens an ImmersiveSpace and the user closes the window via X button, the async Task in .onDisappear gets cancelled before dismissImmersiveSpace() completes, leaving the ImmersiveSpace active with no way to exit.

Steps

  1. WindowGroup opens ImmersiveSpace in .onAppear
  2. User clicks X to close window
  3. .onDisappear fires but async cleanup cancelled
  4. ImmersiveSpace remains active, user trapped

Expected

ImmersiveSpace dismissed when window closes

Actual

ImmersiveSpace remains active

Code

.onAppear {
    Task {
        await openImmersiveSpace(id: "VideoCallMainCamera")
    }
}
.onDisappear {
    Task {
        await dismissImmersiveSpace() // Gets cancelled
    }
}

What I've Tried

  • Task in .onDisappear ❌
  • scenePhase monitoring ❌
  • High priority Task ❌
  • .restorationBehavior(.disabled) + .defaultLaunchBehavior(.suppressed) ✅ (prevents restoration but doesn't fix immediate cleanup)

Question

What's the recommended pattern for ensuring ImmersiveSpace cleanup when WindowGroup closes? Is there a way to block window closure until async cleanup completes, or should ImmersiveSpaces automatically dismiss with their parent window?

Hey @Jir_253,

This is the expected behavior. Similar to iOS, an application cannot close itself. Since the immersive space is the last open scene, you are unable to dismiss this when the other scene is closed.

Use pushWindow to open the requested window and background the scene that it was presented from. This backgrounded scene will allow you to call dismissImmersiveSpace when the foregrounded scene is dismissed as the backgrounded scene will reappear. In additional to your code above, you may want to dismiss the pushed window when the immersive space is closed.

Let me know if you have any questions,
Michael

ImmersiveSpace orphaned when WindowGroup closes
 
 
Q