lampepfl / gears

A strawman for a low-level async library in Scala 3.
https://lampepfl.github.io/gears/
Apache License 2.0
247 stars 24 forks source link

Unexpected behavior of CoreFuture #3

Closed belamenso closed 5 months ago

belamenso commented 1 year ago

Consider this piece of code in futures.scala:

ac.scheduler.execute: () =>
    async:
    link()
    Async.group:
        complete(Try(body))
    unlink()

and add print statements to it:

ac.scheduler.execute: () =>
    println("-- 1")
    async:
    println("-- 2")
    link()
    println("-- 3")
    Async.group:
        println("-- 31")
        complete(Try(body))
        println("-- 32")
    println("-- 4")
    unlink()
    println("-- 5")

Now run this code

  given ExecutionContext = ExecutionContext.global
  val a = Future{ setName("a"); Thread.sleep(2000L); 22 }
  Async.blocking:
    println(a.value)

This will be the result:

-- 1
-- 2
-- 3
-- 31
22

Process finished with exit code 0

The same output is achieved if val a = Future{ setName("a"); Thread.sleep(2000L); 22 } is inside the Async.blocking block.