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

C# threads / tasks don't work on the web (async / await) #34506

Open
wihrl opened this issue Dec 21, 2019 · 12 comments
Open

C# threads / tasks don't work on the web (async / await) #34506

wihrl opened this issue Dec 21, 2019 · 12 comments

Comments

@wihrl
Copy link

wihrl commented Dec 21, 2019

Godot version:
3.2 beta 4
OS/device including version:
Windows 10
Issue description:
Threading and tasks don't seem to work at all when exporting mono projects to the web.
Steps to reproduce:

  1. Create a new project.
  2. Create a new thread or task in a c# script that does something ( I tried printing to the console via GD.Print )
  3. Export for the web
  4. Run the exported project in the web browser.
    Minimal reproduction project:
    https://1drv.ms/u/s!AvsEaC7oMEMagstwXvyFlCMdOOMO2A?e=uuytsD
    The label should change text to "it worked" when the program starts.
@akien-mga
Copy link
Member

Browsers don't enable multithreading in WebAssembly for now, so this is normal.

Duplicate of #12699.

@neikeq
Copy link
Contributor

neikeq commented Dec 21, 2019

There is a runtime-threads target in Mono's wasm sdks: https://github.com/mono/mono/blob/master/sdks/wasm/README.md#threading-support
I haven't tested this yet. We're using the runtime target for now.

I'll re-open this as this is about Mono's threading while #12699 is about Godot's.

@NovemberDev
Copy link

Apparently in Chrome 70, webassembly will be able to support threads

Source: https://developers.google.com/web/updates/2018/10/wasm-threads

@robbertzzz
Copy link

According to the article linked by @NovemberDev Google Chrome 74+ should have threads for webassembly enabled by default, but doesn't work with my Mono build in Chrome 79

@Flavelius
Copy link
Contributor

It should be possible to schedule the work to run on the main thread. I think unity does this aswell. This would make it more compatible for browsers that don't support threads.

@Calinou Calinou changed the title C# threads / tasks don't work on the web. C# threads / tasks don't work on the web (async / await) Nov 21, 2020
@lenscas
Copy link

lenscas commented Dec 4, 2020

I wouldn't mind checking myself but had trouble compiling godot myself (ran both into an error and finding time is also not the easiest)

because threaded wasm is a thing in 3.2.4 due to: #12699 does that mean that threads and/or tasks now also work in C#?

if it doesn't work in the normal C# way, is there a (possible) work around because that feature got closed? (Maybe through communication with gdscript to have that spawn the thread?)

thanks in advance :)

@Faless
Copy link
Collaborator

Faless commented Dec 11, 2020

because threaded wasm is a thing in 3.2.4 due to: #12699 does that mean that threads and/or tasks now also work in C#?

@lenscas Sadly, we were not yet able to build a mono version with threads support.
So at least the upcoming beta will only ship the threads-enabled and the gdnative-enabled versions for the "classical" builds and not the "mono" one.
We still plan to expand both threads support (hopefully soon) and gdnative support (probably later on) to "mono" builds too.

@akien-mga
Copy link
Member

@neikeq Now that #12699 is solved (threads support in Godot's WebAssembly port), we could indeed try https://github.com/mono/mono/blob/master/sdks/wasm/README.md#threading-support to see if it can work with Mono.

@animanmaster
Copy link

@neikeq Now that #12699 is solved (threads support in Godot's WebAssembly port), we could indeed try https://github.com/mono/mono/blob/master/sdks/wasm/README.md#threading-support to see if it can work with Mono.

Just curious if anyone has tried a custom build with this yet or if this is working in 4.0? Would I just have to compile a new wasm export template with threads on in order to try this myself, or would I have to recompile the engine too?

@Beliar83
Copy link

Beliar83 commented Jan 5, 2022

@animanmaster I tried compiling a mono wasm export template with threads for 3.4, but I am always running into some problems. For example, I get several duplicate symbols between libmonosgen and libstubs.a (from emscripten) (getpwnam_r, getpwuid_r, getgrnam, getgrgid, pthread_sigmask), but maybe someone with more knowledge has better luck.

Edit: I got it to compile by removing the stub functions from "mono\mono\mini\mini-wasm.c". Have to test if everything works now.
Edit 2: I keep getting into problems either running or compiling. Not sure if it is just because of me, my setup or, something else. I'll give up for now.

@YuriSizov YuriSizov modified the milestones: 4.1, 3.x Jun 22, 2023
@YuriSizov
Copy link
Contributor

Regarding the milestone change: Godot 4 C# doesn't support the web platform at all at the moment, so this problem is not really relevant there.

@Bip901
Copy link

Bip901 commented Jan 10, 2025

The symptoms of this issue are:

  • You use a System.Threading.Tasks related function
  • It seems to work, but it internally breaks something in the engine
  • The next time Godot and Mono interact, a seemingly random error will occur. This can even be in a line of code as simple as new Node() - the glue between Godot and Mono breaks so it fails. Here's a list of the errors I've experienced so far:
    • FATAL: Condition "!klass" is true - this is caused by the assert in managed_type.cpp
    • NullReferenceException in your code on things that aren't really null (even if they're guarded by if != null)
    • timeout callback threw a System.NullReferenceException at System.Threading.WasmRuntime.TimeoutCallback (System.Int32 id)

Thus, the following issues are really just duplicates of this one:

I suggest we merge them into this one.

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