-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[wasm][mt] Guard more places for blocking wait on JS interop threads #98508
Closed
radekdoulik
wants to merge
5
commits into
dotnet:main
from
radekdoulik:pr-wasm-mt-more-blocking-wait-detection-and-tests
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9cf90be
[wasm][mt] Guard more places for blocking wait on JS interop threads
radekdoulik 468bea0
Feedback
radekdoulik aca8dd5
Feedback
radekdoulik 157b487
Fix existing tests
radekdoulik bc688a3
Merge remote-tracking branch 'remotes/origin/main' into pr-wasm-mt-mo…
radekdoulik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,24 +146,48 @@ public class NamedCall | |
override public string ToString() => Name; | ||
} | ||
|
||
static void LocalCtsIgnoringCall(Action<CancellationToken> action) | ||
{ | ||
var cts = new CancellationTokenSource(8); | ||
try { | ||
action(cts.Token); | ||
} catch (OperationCanceledException exception) { | ||
if (exception.CancellationToken != cts.Token) | ||
{ | ||
throw; | ||
} | ||
/* ignore the local one */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mkhamoyan Radek improved it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, will update in my PR. |
||
} | ||
} | ||
|
||
public static IEnumerable<NamedCall> BlockingCalls = new List<NamedCall> | ||
{ | ||
new NamedCall { Name = "Task.Wait", Call = delegate (CancellationToken ct) { Task.Delay(10, ct).Wait(ct); }}, | ||
new NamedCall { Name = "Task.WaitAll", Call = delegate (CancellationToken ct) { Task.WaitAll(Task.Delay(10, ct)); }}, | ||
new NamedCall { Name = "Task.WaitAny", Call = delegate (CancellationToken ct) { Task.WaitAny(Task.Delay(10, ct)); }}, | ||
new NamedCall { Name = "ManualResetEventSlim.Wait", Call = delegate (CancellationToken ct) { | ||
using var mr = new ManualResetEventSlim(false); | ||
using var cts = new CancellationTokenSource(8); | ||
try { | ||
mr.Wait(cts.Token); | ||
} catch (OperationCanceledException) { /* ignore */ } | ||
LocalCtsIgnoringCall(mr.Wait); | ||
}}, | ||
new NamedCall { Name = "SemaphoreSlim.Wait", Call = delegate (CancellationToken ct) { | ||
using var sem = new SemaphoreSlim(2); | ||
var cts = new CancellationTokenSource(8); | ||
try { | ||
sem.Wait(cts.Token); | ||
} catch (OperationCanceledException) { /* ignore */ } | ||
LocalCtsIgnoringCall(sem.Wait); | ||
}}, | ||
new NamedCall { Name = "CancellationTokenSource.ctor", Call = delegate (CancellationToken ct) { | ||
using var cts = new CancellationTokenSource(8); | ||
}}, | ||
new NamedCall { Name = "Mutex.WaitOne", Call = delegate (CancellationToken ct) { | ||
using var mr = new ManualResetEventSlim(false); | ||
var mutex = new Mutex(); | ||
var thread = new Thread(() => { | ||
mutex.WaitOne(); | ||
mr.Set(); | ||
Thread.Sleep(50); | ||
mutex.ReleaseMutex(); | ||
}); | ||
thread.Start(); | ||
Thread.ForceBlockingWait((_) => mr.Wait(), null); | ||
mutex.WaitOne(); | ||
}}, | ||
}; | ||
|
||
|
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree that making
Task.Delay
forbidden operation on UI thread is a way forward.I think we have to find a way that it never calls blocking
.Wait
Here you are just hiding the problem from the unit test, rigth ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the same for
Timer
, there is no theoretical reason why this needs to block.Except maybe for creating new child thread. If that's the reason, we should add
ForceBlockingWaitAsync
intonew Thread()
code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, here I am forcing it to not break the current
ManagedDelay_ContinueWith
test.The
Task.Delay
eventually ends callingSystem.Threading.LowLevelMonitor.AcquireCore()
, which calls to nativepthread_mutex_lock
- that is a blocking call.The
Timer
callsLowLevelMonitor.AcquireCore
as well.Details: