Repository: godotengine/godot (language: cpp)

Issue:
Floating game window and visible Popup error when resizing window
### Tested versions

Godot v4.4.beta2

### System information

Fedora Linux 41 (KDE Plasma) on Wayland - X11 display driver, Multi-window

### Issue description

If you just add a PopupPanel to a scene.
Add code
```
func _unhandled_input(event: InputEvent) -> void:
	if event.is_action_pressed("ui_accept"):
		$PopupPanel.popup_centered()
```
Run scene.
Press space.
Resize window.
Error printed.

```
E 0:00:04:0505   _sub_window_update: Condition "index == -1" is true.
  <C++ Source>   scene/main/viewport.cpp:320 @ _sub_window_update()

```

Video is with a OptionButton, first found this here https://github.com/godotengine/godot/issues/100613#issuecomment-2630683083 and probably a related issue.

https://github.com/user-attachments/assets/e9908510-da35-4d0b-85ac-aebf8de10918


### Steps to reproduce

^

### Minimal reproduction project (MRP)


[popup_error.zip](https://github.com/user-attachments/files/18641752/popup_error.zip)


Discussion / hints:

Fix patch:
diff --git a/scene/main/viewport.cpp b/scene/main/viewport.cpp
index d0054baeff25..fe3fa7b58870 100644
--- a/scene/main/viewport.cpp
+++ b/scene/main/viewport.cpp
@@ -317,7 +317,13 @@ void Viewport::_sub_window_register(Window *p_window) {
 
 void Viewport::_sub_window_update(Window *p_window) {
 	int index = _sub_window_find(p_window);
-	ERR_FAIL_COND(index == -1);
+
+	// _sub_window_update is sometimes called deferred, and the window may have been closed since then.
+	// For example, when the user resizes the game window.
+	// In that case, _sub_window_find will not find it, which is expected.
+	if (index == -1) {
+		return;
+	}
 
 	SubWindow &sw = gui.sub_windows.write[index];
 	sw.pending_window_update = false;


Test patch: