X Tutup
Skip to content

Wayland: Defer event thread initialization to late initialization#111493

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
deralmas:racing-the-thread
Nov 6, 2025
Merged

Wayland: Defer event thread initialization to late initialization#111493
Repiteo merged 1 commit intogodotengine:masterfrom
deralmas:racing-the-thread

Conversation

@deralmas
Copy link
Contributor

This race condition made me pull my hair. wl_display_roundtrip has its own little event loop, which apparently conflicts hard with the always-running event loop thread.

I kinda assumed that it would be thread-safe thanks to its internal wl_display_prepare_read call that the docs talk about but that's clearly not enough.

Luckily this method is called very few times and the only dangerous instances are in the initialization routine, which first starts the thread and then does various roundtrips. Libdecor has also some internal roundtrips of its own which would often fail. Starting the thread after all initialization fixes the issue.

Tested this by spamming lots of wl_display_roundtrip in WaylandThread::init() with and without this fix.


Also added the usual comment-of-knowledge for good measure :P

This race condition made me pull my hair. `wl_display_roundtrip` has its
own little event loop, which apparently conflicts hard with the
always-running event loop thread.

I kinda assumed that it would be thread-safe thanks to its internal
`wl_display_prepare_read` call that the docs talk about but that's
clearly not enough.

Luckily this method is called very few times and the only dangerous
instances are in the initialization routine, which first starts the
thread and then does various roundtrips. Libdecor has also some internal
roundtrips of its own which would often fail. Starting the thread after
all initialization fixes the issue.

Tested this by spamming *lots* of `wl_display_roundtrip` in
`WaylandThread::init()` with and without this fix.
@deralmas
Copy link
Contributor Author

Welp I've just noticed that I've been extremely somehow both generic generic and verbose in this description 😅

In simple words, this fixes a completely random stall that would happen (mainly at initialization) when the engine tried to send a roundtrip message. Both the main event loop and the synchronous roundtrip method would dispatch events, even from different threads, breaking things in a subtle way. The fix is to be sure that we're not roundtripping when the thread is doing stuff.

Copy link
Member

@bruvzg bruvzg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not encountered any random stalls on startup, but change make sense and seems to be working fine.

@akien-mga akien-mga changed the title Wayland: defer event thread initialization to late initialization Wayland: Defer event thread initialization to late initialization Nov 6, 2025
@Repiteo Repiteo merged commit 2d7380c into godotengine:master Nov 6, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Nov 6, 2025

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.

4 participants

X Tutup