### Godot version

4.0.3-stable

### System information

Windows 10

### Issue description

In some projects ViewportTexture remains pink and doesn't render viewport content after upgrading from 4.0.2-stable to 4.0.3-stable. If a pair of files viewport.h/viewport.cpp are downgraded back to what they were in 4.0.2-stable, problem goes away.

This is how it looks in 4.0.3-stable:
![4 0 3_stable_viewport_4 0 3](https://github.com/godotengine/godot/assets/1449921/44c191a9-4b67-4b02-90b6-4ab615f62a73)

And this is how it looks if viewport.h/viewport.cpp are downgraded back to what they were in 4.0.2-stable.
![4 0 3_stable_viewport_4 0 2](https://github.com/godotengine/godot/assets/1449921/f5abc605-f129-4222-9c0e-e9a07621dd9d)



### Steps to reproduce

Unfortunately, I cannot come up wit a simple demo project reproducing the problem. It exists in some projects and doesn't exist in others.


### Minimal reproduction project

Cannot produce a simple project reproducing the problem. In simple projects it works fine.
int index = _sub_window_find(p_window);
	ERR_FAIL_COND(index == -1);
	ERR_FAIL_NULL(RenderingServer::get_singleton());
	RS::get_singleton()->free(gui.sub_windows[index].canvas_item);
	gui.sub_windows.remove_at(index);
	if (gui.sub_windows.size() == 0) {
		RS::get_singleton()->free(subwindow_canvas);
		subwindow_canvas = RID();
	}
	if (gui.currently_dragged_subwindow == p_window) {
		gui.subwindow_drag = SUB_WINDOW_DRAG_DISABLED;
		gui.currently_dragged_subwindow = nullptr;
	}
	if (gui.subwindow_focused == p_window) {
		Window *new_focused_window;
		Window *parent_visible = p_window->get_parent_visible_window();
		gui.subwindow_focused->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_OUT);
		if (parent_visible) {
			new_focused_window = parent_visible;
		} else {
			new_focused_window = Object::cast_to<Window>(this);
		}
		if (new_focused_window) {
			int new_focused_index = _sub_window_find(new_focused_window);
			if (new_focused_index != -1) {
				gui.subwindow_focused = new_focused_window;
			} else {
				gui.subwindow_focused = nullptr;
			}
			new_focused_window->_event_callback(DisplayServer::WINDOW_EVENT_FOCUS_IN);