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

Fix the cleanup logic for the Android render thread #94661

Merged

Conversation

m4gr3d
Copy link
Contributor

@m4gr3d m4gr3d commented Jul 23, 2024

On Android the exit logic goes through Godot#onDestroy() who attempts to cleanup the engine using the following code:

runOnRenderThread {
	GodotLib.ondestroy()
	forceQuit()
}

The issue however is that by the time we ran this code, the render thread has already been paused (but not yet destroyed), and thus GodotLib.ondestroy() and forceQuit() which are scheduled on the render thread queue, are not pulled out off the queue and not executed.

To address this, we instead explicitly request the render thread to exit and block until it does. As part of it exit logic, the render thread has been updated to properly destroy and clean the native instance of the Godot engine, resolving the issue.

Fixes #93826

@Alex2782
Copy link
Contributor

@Alex2782 Can you check if #94661 addresses the issue when you add the 1 second delay.

unfortunately not yet (on Samsung Tab S7, Android 13)

with 1 sec delay
Error ResourceFormatSaverTextInstance::save(const String &p_path, const Ref<Resource> &p_resource, uint32_t p_flags) {
	if (p_path.ends_with(".tscn")) {
		packed_scene = p_resource;
	}

	Error err;
	Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::WRITE, &err);
	ERR_FAIL_COND_V_MSG(err, ERR_CANT_OPEN, "Cannot save file '" + p_path + "'.");
	Ref<FileAccess> _fref(f);

	print_line("BEGIN DELAY: ", OS::get_singleton()->get_ticks_usec());
	OS::get_singleton()->delay_usec(1000000); // 1 000 000 = 1 sec
	print_line("END DELAY: ", OS::get_singleton()->get_ticks_usec());

	local_path = ProjectSettings::get_singleton()->localize_path(p_path);

Logcat:

2024-07-23 23:55:18.812 15610-15646 godot                   org.godotengine.editor.v4.dev        E  USER ERROR: /data/data/org.godotengine.editor.v4.dev/files/config/godot/editor_settings-4.3.tres:1 - Parse Error: 
2024-07-23 23:55:18.812 15610-15646 godot                   org.godotengine.editor.v4.dev        E     at: open (scene\resources\resource_format_text.cpp:1061)
2024-07-23 23:55:18.813 15610-15646 godot                   org.godotengine.editor.v4.dev        E  USER ERROR: Failed loading resource: /data/data/org.godotengine.editor.v4.dev/files/config/godot/editor_settings-4.3.tres. Make sure resources have been imported by opening the project in the editor at least once.
2024-07-23 23:55:18.813 15610-15646 godot                   org.godotengine.editor.v4.dev        E     at: _load (core\io\resource_loader.cpp:284)
2024-07-23 23:55:18.813 15610-15646 godot                   org.godotengine.editor.v4.dev        E  USER ERROR: Could not load editor settings from path: /data/data/org.godotengine.editor.v4.dev/files/config/godot/editor_settings-4.3.tres
2024-07-23 23:55:18.813 15610-15646 godot                   org.godotengine.editor.v4.dev        E     at: create (editor\editor_settings.cpp:1067)

The editor does not crash due to bugfix #94593. The editor loads the default settings. I have switched to English, but then it switches to the default “German”, my system language.


Test without PR #94593
		singleton = ResourceLoader::load(config_file_path, "EditorSettings");
		//before PR #94593
		singleton->set_path(get_newest_settings_path()); // Settings can be loaded from older version file, so make sure it's newest.

		if (singleton.is_null()) {
			ERR_PRINT("Could not load editor settings from path: " + config_file_path);
			//PR #94593
			//config_file_path = get_newest_settings_path();
			goto fail;
		}

		//PR #94593
		//singleton->set_path(get_newest_settings_path()); // Settings can be loaded from older version file, so make sure it's newest.
		singleton->save_changed_setting = true;

Crash:

2024-07-24 00:29:59.005 20916-20954 godot                   org.godotengine.editor.v4.dev        E  USER ERROR: /data/data/org.godotengine.editor.v4.dev/files/config/godot/editor_settings-4.3.tres:1 - Parse Error: 
2024-07-24 00:29:59.005 20916-20954 godot                   org.godotengine.editor.v4.dev        E     at: open (scene\resources\resource_format_text.cpp:1061)
2024-07-24 00:29:59.007 20916-20954 godot                   org.godotengine.editor.v4.dev        E  USER ERROR: Failed loading resource: /data/data/org.godotengine.editor.v4.dev/files/config/godot/editor_settings-4.3.tres. Make sure resources have been imported by opening the project in the editor at least once.
2024-07-24 00:29:59.007 20916-20954 godot                   org.godotengine.editor.v4.dev        E     at: _load (core\io\resource_loader.cpp:284)
2024-07-24 00:29:59.007 20916-20954 libc                    org.godotengine.editor.v4.dev        A  Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x0 in tid 20954 (VkThread), pid 20916 (e.editor.v4.dev)
2024-07-24 00:29:59.865 21020-21020 DEBUG                   crash_dump64                         A  Cmdline: org.godotengine.editor.v4.dev
2024-07-24 00:29:59.865 21020-21020 DEBUG                   crash_dump64                         A  pid: 20916, tid: 20954, name: VkThread  >>> org.godotengine.editor.v4.dev <<<

Empty file:

Screenshot 2024-07-24 004430

@Alex2782
Copy link
Contributor

Notes:

I can't see Destroying in the logs and I don't have to press Backbutton.
2 days ago I tried to reproduce on Windows, from 100 ms to 500 ms, it was not possible, then I increased to 1 second. Windows Editor seems to wait at least 500 ms.


on Android: (Dev-Build)

from 75 ms to 500 ms delay -> Parse Error

with 50 ms, 2 x OK, 1 x Parse Error

	print_line("BEGIN DELAY: ", OS::get_singleton()->get_ticks_usec());
	OS::get_singleton()->delay_usec(50000); // 1 000 000 = 1 sec
	print_line("END DELAY: ", OS::get_singleton()->get_ticks_usec());

If Parse Error then i can't see END DELAY: output.
On Windows, the function is always executed completely. (tested with 1s, 5s and 30s)

2024-07-24 01:40:25.483 32363-32406 godot                   org.godotengine.editor.v4.dev        I  BEGIN ResourceFormatSaverTextInstance::save
2024-07-24 01:40:25.484 32363-32406 godot                   org.godotengine.editor.v4.dev        I  BEGIN DELAY:  25324678

@m4gr3d m4gr3d force-pushed the fix_android_render_thread_cleanup branch from 3f8dc42 to d27514f Compare July 24, 2024 17:17
On Android the exit logic goes through `Godot#onDestroy()` who attempts to cleanup the engine using the following code:

```
runOnRenderThread {
	GodotLib.ondestroy()
	forceQuit()
}
```

The issue however is that by the time we ran this code, the render thread has already been paused (but not yet destroyed), and thus `GodotLib.ondestroy()` and `forceQuit()` which are scheduled on the render thread are not executed.

To address this, we instead explicitly request the render thread to exit and block until it does. As part of it exit logic, the render thread has been updated to properly destroy and clean the native instance of the Godot engine, resolving the issue.
@m4gr3d m4gr3d force-pushed the fix_android_render_thread_cleanup branch from d27514f to 4d0da74 Compare July 24, 2024 17:18
@m4gr3d m4gr3d marked this pull request as ready for review July 24, 2024 17:19
@m4gr3d m4gr3d requested a review from a team as a code owner July 24, 2024 17:19
@m4gr3d
Copy link
Contributor Author

m4gr3d commented Jul 24, 2024

Thanks for testing @Alex2782!

I made some additional changes which should address the issue. I was able to confirm by replicating your test - adding a delay to the saving logic - and I'm no longer able to repro the issue.

Can you take another look.

@Alex2782
Copy link
Contributor

Looks great!

Tested with up to 10 seconds delay (no ANRs) and Backbutton.

@akien-mga akien-mga merged commit ab80e56 into godotengine:master Jul 24, 2024
18 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Android Editor] Error parsing editor_settings-4.3.tres
3 participants