-
-
Notifications
You must be signed in to change notification settings - Fork 560
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
Add Task async await check when evaluate await expression #1882
Add Task async await check when evaluate await expression #1882
Conversation
After doing some experiments I think this async/await case only failed if I called an async method from C# object. |
Hi @sebastienros @lahma, it would be great if you guys could take a look at this. It is more like fixing a bug I found while using it for my project. Thanks. |
@trannamtrung1st have your cheked: https://github.com/sebastienros/jint/blob/main/Jint.Tests/Runtime/AsyncTests.cs and how |
Wonderful that's exactly what I want. Many thanks. The only problem left is this one, I believe it is because there're some functions return Task but not actually async, e.g, Task.CompletedTask or Task.FromResult(...). Therefore the Line 149 in c0b0369
It does not always happen, I found it while trying to create about 1000 threads (each with their own Engine) to simulate some scenarios. Here the Microsoft's documentation link about the ContinueWith: https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.continuewith?view=net-8.0#system-threading-tasks-task-continuewith(system-action((system-threading-tasks-task-system-object))-system-object-system-threading-cancellationtoken-system-threading-tasks-taskcontinuationoptions-system-threading-tasks-taskscheduler) I already removed all unused part in the PR except for the one is to fix the above issue. You may want to take a look. Thanks for your quick reply! |
Wait a second. Sorry for the confusion but I just tested the ContinueWith, seems like it's not the cause. However I'm not sure why when I apply the fix, it solves the issue. I'll try to investigate it more. |
Hi @lahma, PR updated, below is the explanation |
Could you add some test case(s) that would ensure that the behavior has been corrected and will stay working as expected? |
Hi @lahma ,
|
|
Oh I used a Mac before so I missed that one. Thanks. |
Seems that net462 on Windows behaves in a slightly different way. I will have to spend a bit more time to investigate. |
Windows run fails:
I'll be travelling and back tomorrow evening.. |
Hi @lahma, jint/Jint/JsValueExtensions.cs Line 657 in c0b0369
However, I feel like the Changes are also tested in a Windows 11 machine with net462 framework. Could you help trigger the test for Windows net462 again? This is kinda weird. Please take a look at this commit: 6d02edc |
Hi @lahma , is there any update on this one? It would be great to see the Tasks Interop feature become mature and included in the official package so we can use it without cloning one. |
Honestly, I have no idea why it failed on Windows since I couldn't replicate it in the local. The only situation I can think of now is there are other async codes/threads accessing the queue and executing the Wait there before this line Do you mind if I add one more commit to try solving it? So inconvenient that I couldn't replicate it locally. |
You can work with the PR as much as you want. I'll try to trigger builds when I'm available. |
I think the problem mostly is about how different the concepts are between .NET and JavaScript. All interop async/await have been community contributed and set behind the experimental flag for a reason - I don't have the capacity to debug or ensure the correct working of it and I'm grateful that people like you have time and interest to improve it. The current async/await in Jint is not fully working either, it would require more work and state handling, it's the same issue as with generators, Jint resolves things too eagerly and there are open issues about it.. |
Got it. I'll close this PR for now and see if I have time on the weekends to draft some ideas. Thanks. |
@trannamtrung1st Thanks for working on this! I've checked the proposed changes and wonder - wouldn't it be reasonable to pull in your work even with the possibility of the continuation running on an unexpected thread? The scenario shown in b0388a1 is rather common in real workloads and given the improvement touches just a few files, it seems to me as an interim improvement until/when the event loop is refactored, which appears to be a much bigger undertaking. The tests would have to be fixed of course. |
@lofcz I'm happy to reopen the PR if you feel it does help. |
I see the PR as an improvement over the current state, so I'm for reopening it. The tests are however still failing, is that something you could look further into? |
That's the hard part. |
Looks good now. Please let me know if there's any additional step before this one can be merged. |
061be92
to
50999b8
Compare
Thank you! |
Hi team, this PR is my idea to support await an async method from C# object.
Before, I cannot await an async result when calling async function from a C# object. It always returns Task.
Now I can make it works like below:
Sorry for my English and since I don't have much time to research all the implementation here, so welcome any feedback you may have.
Thanks!
Trung Tran.