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

Remove extra lock-taking in preopen setup #491

Merged
merged 1 commit into from
May 2, 2024

Conversation

abrown
Copy link
Collaborator

@abrown abrown commented Apr 26, 2024

Issue #8392 in Wasmtime highlights how preopen registration can result in a hang when compiled for a threaded environment. The problem is that internal_register_preopened_fd_unlocked assumes it will not touch the global lock because its caller, __wasilibc_populate_preopens, already has taken the lock. Unfortunately, a refactoring in #408 (which introduces internal_register_preopened_fd_unlocked) did not catch that the resize function called internally also takes the global lock. This change removes that locking in resize under the assumption that it will only be called when the lock is already taken.

Issue [#8392] in Wasmtime highlights how preopen registration can result
in a hang when compiled for a threaded environment. The problem is that
`internal_register_preopened_fd_unlocked` assumes it will not touch the
global lock because its caller, `__wasilibc_populate_preopens`, already
has taken the lock. Unfortunately, a refactoring in WebAssembly#408 (which
introduces `internal_register_preopened_fd_unlocked`) did not catch that
the `resize` function called internally also takes the global lock. This
change removes that locking in `resize` under the assumption that it
will only be called when the lock is already taken.

[#8392]: bytecodealliance/wasmtime#8392
@abrown
Copy link
Collaborator Author

abrown commented Apr 26, 2024

I would like to verify that this is correct somehow: it would be nice to run cargo/rust with this change (if anyone can remind me how!) and its probably worth taking some time to flesh out the test suite.

Copy link
Collaborator

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Looks reasonable to me!

@abrown
Copy link
Collaborator Author

abrown commented May 2, 2024

@alexcrichton, is there a way I can "plug in" an external wasi-libc sysroot to rustc so I can check this out without recompiling rustc?

[edit:] never mind, I see you explained this elsewhere!

@abrown
Copy link
Collaborator Author

abrown commented May 2, 2024

Ok, I confirmed by building a new rustc with these changes that this fix does not miss anything. I am still thinking about how to add this to the testsuite but I don't yet have a great way to do that; #369 seemed problematic to me so maybe there's a different way to test the threads bits. I'll go ahead and merge this and leave the testing for later.

@abrown abrown merged commit 8876138 into WebAssembly:main May 2, 2024
8 checks passed
@abrown abrown deleted the fix-8392 branch May 2, 2024 21:07
@namse
Copy link

namse commented Jun 28, 2024

If this change is not released, the resize function in the existing version 22 will end up waiting indefinitely for a lock. I think this is quite an important fix. Is there any plan for an urgent release?

@sbc100
Copy link
Member

sbc100 commented Jun 28, 2024

If this change is not released, the resize function in the existing version 22 will end up waiting indefinitely for a lock. I think this is quite an important fix. Is there any plan for an urgent release?

IIRC the wasi threads effort is kind of on hold, pending shared everything threads. Are you using it production somehow / somewhere despite that?

@abrown
Copy link
Collaborator Author

abrown commented Jun 28, 2024

I'm not opposed releasing a v22.1 but, like @sbc100 mentioned, with how experimental wasi-threads is it likely does not need to be urgent (?).

@namse
Copy link

namse commented Jul 1, 2024

@sbc100: IIRC the wasi threads effort is kind of on hold, pending shared everything threads. Are you using it production somehow / somewhere despite that?

Yes I use threads as my game, ui engine. I didn't know that wasi threads is the pending one. Should I have to stop using wasi thread?

@sbc100
Copy link
Member

sbc100 commented Jul 1, 2024

@sbc100: IIRC the wasi threads effort is kind of on hold, pending shared everything threads. Are you using it production somehow / somewhere despite that?

Yes I use threads as my game, ui engine. I didn't know that wasi threads is the pending one. Should I have to stop using wasi thread?

That is up to you. I don't know which engines (if any) support wasi threads so if you choose to depend on wasi-threads you would likely be severely limiting your portability.

@namse
Copy link

namse commented Jul 2, 2024

that's my own engine, implemented threads on rust wasm32-wasip1-threads target with web worker.

It's somewhat difficult to understand. I think threads are a very important feature for performance in WASI as well, but could you explain why there seems to be little interest in supporting them? Is there a roadmap that I am not aware of, or should I assume that the WebAssembly Team is not interested in multithreading in WASI?

@sbc100
Copy link
Member

sbc100 commented Jul 2, 2024

There is very much interest in multi-threading in Wasm and in WASI. Currently work is ongoing on the shared-everything threading proposal, and my understanding is that most of the WASI efforts are now focused on building on top of that: https://github.com/WebAssembly/shared-everything-threads. @abrown I wonder if we can add something to this effect in the readme for https://github.com/WebAssembly/wasi-threads?

@loganek
Copy link
Contributor

loganek commented Jul 2, 2024

Indeed the shared-everything threading proposal is the long-term approach, however, there are teams (like mine) that currently use wasi-threads (through WAMR) and due to the limited options for updating the code, will have to keep it around for at least 5 years. We currently use Rust so we don't depend on the wasi-sdk release (at least for now) as Rust stdlib uses wasi-libc directly.

@sbc100
Copy link
Member

sbc100 commented Jul 2, 2024

Sorry I didn't mean to block the release of new version of wasi-sdk. I think the bar for cutting new releases should be very low (assuming the steps involved are not cumbersome, but if they are we should fix that).

@abrown
Copy link
Collaborator Author

abrown commented Jul 3, 2024

@loganek, do you want to take a stab at creating a new v22.1 release following this guide?

@abrown
Copy link
Collaborator Author

abrown commented Jul 10, 2024

@namse, do you want to test out the new wasi-sdk-23 release to check if it works as expected?

@namse
Copy link

namse commented Jul 11, 2024

Wonderful! I've just tested it and problem fixed. Thank you ^_______^ @abrown

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

Successfully merging this pull request may close these issues.

5 participants