-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Asynchronous scheduler behaviour #7193
Comments
Isn't this expected? callbackProc is clearly an infinite loop, would you expect
|
Of course this is not expected, expected result is showed in first code sample, So code inside of callbackProc() in 2nd code sample runs every one tick/poll() call and adds itself in callback list for next tick/poll() call. It must not create infinite loop, and it must allow IO operations and timers to be processed concurrently. So this is not actually |
Overall current approach is not totally wrong and can be avoided, but for synchronization between tasks its not very good to have such behavior. |
Events should be dispatched in FIFO order. Or maybe is [0] https://gist.github.com/nitely/c143752aa71bc4ca6cb42e9946422438 |
@nitely, first of all |
@cheatfate well yeah, I was agreeing with you. If you check the gist I posted it behaves as you want it to. |
@nitely sorry, not mentioned that you posted actually Python code, not Nim code. |
In #7759, which was meant to be a fix for this, it is mentioned:
@cheatfate did the fix work? Can we close this? |
@narimiran No this is not fixed. I just tested it. |
Still runs forever: import asyncdispatch
proc testProc() {.async.} =
for i in 1..1_000:
await sleepAsync(5)
echo "Timeout event " & $i
proc callbackProc() {.gcsafe.} =
echo "Callback event"
callSoon(callbackProc)
when isMainModule:
discard getGlobalDispatcher()
asyncCheck testProc()
callSoon(callbackProc)
for i in 1..100:
echo "Iteration " & $i
poll() |
Honestly, I'm tempted to close this as a Practically speaking this hasn't been a problem for me or for anyone from what I've seen, and in some ways as @zielmicha highlights this isn't even surprising behaviour. Anyway, the reason these are the semantics is because of this code: Nim/lib/pure/asyncdispatch.nim Lines 205 to 209 in 4ae3413
So as far as I can tell the fix is simple, but is it really "right"? This contrived example does not convince me that we should potentially break code in changing the semantics here. If someone has a good argument for why it's important that we do change the semantics then please share them. |
Asynchronous dead end.
Recent changes to asyncdispatch caused damage to core scheduler behavior. This changes called:
Lets finish all Futures[T], which we can finish in one tick (poll() call).
Now i want to show you some circumstances:
This code is pretty simple and its output will be looking like this:
You can see here that callbackProc() is called every tick, also you can notice
here work of timer, and everything works as expected.
Now lets see almost equal code sample. Actually it was equal to previous one,
only before "Let finish all Futures[T] we can".
This code change is callSoon(), which was introduced to fix recursion bugs, and allow developers to use it to set callbacks for every tick.
So callSoon(callbackProc) is just adding itself to the list of callbacks which going to be scheduled inside of poll().
Code output is looks like this:
This code will never ends, just because scheduler tries to finish all callbacks.
And because new callback is added, this code will never ends. Also you will never see in output timer events, so timer and any IO operations become unreliable.
The text was updated successfully, but these errors were encountered: