Abort threaded preview generators on exit #84716
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See #84200 (comment).
Basically, after the reimport is done we start to generate previews. This is a non-blocking operation and it is out of our control (as in, we don't know how much work it needs to do or when it will be done).
As such, we can't really wait for it to finish, so our only hope is that it correctly aborts on exiting the editor. Which it doesn't. As far as I can tell, it should handle exiting correctly, but there are so many threads and mutexes and semaphores involved, everything and anything can go wrong.
I managed to identify a couple of problems, which this PR addresses. First, we avoid restarting the editor directly from the surface upgrade tool. This is likely needed, because this dialog can appear when we try to load a scene or a mesh resource, and if in turn we trigger the editor restart from it, this may bring us back to resource loaders. And possibly create a lock.
Another issue is that some preview generators are threaded in their own right, on top of the EditorResourcePreview being threaded. These have their own semaphores and checks and they await for the next rendered frame from the RenderingServer. Which never happens at this stage, no matter how many times we do the sync. So I added an explicit abort operation that unlocks the semaphore immediately. It's crudely done, but it seems to resolve the issues and I can at least import Kenney's starter project in Godot 4.2 as expected.
While investigating, I made a couple of clean up changes. I've removed an unimplemented preview generator, renamed some variables for clarity, fixed order mistakes, and flattened for readability
EditorResourcePreview::_iterate
(should lead to no logical changes). Make sure to compare changes without whitespaces.