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

Handling signals which were emitted in a thread unsafe? #32214

Open
kb173 opened this issue Sep 20, 2019 · 4 comments
Open

Handling signals which were emitted in a thread unsafe? #32214

kb173 opened this issue Sep 20, 2019 · 4 comments

Comments

@kb173
Copy link

kb173 commented Sep 20, 2019

Godot version:
3.1.1

OS/device including version:
Ubuntu 19.04, Windows 10

Issue description:
I want to emit a signal in a thread to notify that something new is available, so that it can be handled safely in the main thread. However, it seems like when I emit a signal in a thread, the function which is connected to it is not thread safe as well.

Since the errors are very hard to interpret (usually something like 10054 socket error), it's hard to know where exactly something unsafe is happening. I think the documentation needs to be extended with how signals and threads interact.

If this is really currently not the case, it would be very helpful for signal handling to always be thread safe, no matter where it came from. Until then, I see this mainly as a documentation issue (there is no mention of signals in the 'Thread safe APIs' page).

@MunWolf
Copy link
Contributor

MunWolf commented Sep 20, 2019

Signals are just as safe as function calls, unless they are set to DEFERRED they will run as soon as emit_signal is called, if you are crashing when calling emit_signal in a thread you are doing something non thread safe in the handler, the signal call itself is safe (as long as you are not adding new connections during that time).

@kb173
Copy link
Author

kb173 commented Sep 20, 2019

Aha! I thought emitted signals aren't handled immediately, but in a different processing stage. That's why I originally assumed emitting a signal in a thread is safe, regardless of the handlers.

Will the handling functions be run in a thread safe way when using DEFERRED?

@MunWolf
Copy link
Contributor

MunWolf commented Sep 20, 2019

DEFFERED is run at the end of a frame by the main thread.
So technically it isn't thread safe if some other thread accesses the same thing (aka if you write some value in a thread at the same time as the main thread is accessing it in your signal handler), but if you are accessing some functionality limited to the main thread then yes it is safe.

@kb173
Copy link
Author

kb173 commented Sep 20, 2019

Right, makes sense! Honestly, things like this should really be in the thread safety docs - debugging them can be really frustrating, especially with some basic information like this not being well available.

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

No branches or pull requests

3 participants