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

tell method in actor is not working with sleep #110

Closed
windbird123 opened this issue Mar 29, 2024 · 4 comments
Closed

tell method in actor is not working with sleep #110

windbird123 opened this issue Mar 29, 2024 · 4 comments

Comments

@windbird123
Copy link

The following code was not working as expected. (ox version: 0.0.24)

import ox.supervised
import ox.channels.*

class Stateful:
  private var counter: Int = 0

  def increment(delta: Int): Int =
    counter += delta
    Thread.sleep(100)
    println(counter)
    counter

supervised {
  val ref = Actor.create(new Stateful)
  ref.tell(_.increment(5))
}

expected results: prints 5
actual results: prints nothing

When Thread.sleep(100) line was removed, it worked as expected.

@adamw
Copy link
Member

adamw commented Mar 29, 2024

You're right, the example in the docs is misleading.

What happens there is that because .tell is non-blocking, the invocation is scheduled, and the scope ends (there's not other code to run - the main body is complete). When this happens, all threads that are still running (which includes actors) are interrupted. Hence, the println is never invoked.

@windbird123
Copy link
Author

Thank you for your kind explanation.

When I replace Thread.sleep(100) with (1 to 10000000).foreach(println), it seemed like there was no interruption. (It printed all numbers)

 def increment(delta: Int): Int =
    counter += delta
    // Thread.sleep(100)
    (1 to 10000000).foreach(println)
    println(counter)
    counter

I thought that (1 to 10000000).foreach(println) to be interrupted since the scope already ends (and main body is complete).
In this case, could we assume that ox tried to interrupt but was not interrupted.

@adamw
Copy link
Member

adamw commented Apr 3, 2024

Interesting! This surprised me as well, but it seems println is not interruptible!

You can observe the interruption e.g. as follows:

    def increment(delta: Int): Int =
      counter += delta
      try Thread.sleep(100)
      catch
        case e: Exception =>
          println("Exception: " + e)
          throw e
      println(counter)
      counter

@adamw
Copy link
Member

adamw commented Apr 3, 2024

I've expanded the example (4542e47), I'll close this for now as I think the code is fine, but please reopen if there are some more problems with tell

@adamw adamw closed this as completed Apr 3, 2024
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

No branches or pull requests

2 participants