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

The race function doesn't handle functions that can throw a ControlThrowable exception #213

Closed
rcardin opened this issue Sep 13, 2024 · 1 comment · Fixed by #216
Closed

Comments

@rcardin
Copy link
Contributor

rcardin commented Sep 13, 2024

I'm the maintainer of the library raise4s(link to the repo here). I use context functions to let the user declare functions that can raise logic-typed errors instead of exceptions. Jon Pretty is doing something close to my library with Contingency.

I'd love to integrate with ox and write code like the following:

@main
def main(): Unit = {

  val result: Int raises String = ox.race(
    {
      ox.sleep(500.milliseconds)
      println("short task")
      Raise.raise("Something went wrong")
      2
    }, {
      ox.sleep(1.second)
      println("long task")
      Raise.raise("Something went wrong too late")
      1
    }
  )

  Raise.run(result) match {
    case value: Int => println(s"Value: $value")
    case error: String => println(s"Error: $error")
  }
}

However, the above code doesn't work. In detail, the execution of the race function never stops.

The implementation of the raise method uses the following exception:

private[raise4s] case class Raised[Error](original: Error)
    extends ControlThrowable
    with NoStackTrace

However, the implementation of the ox.race function handles the execution of the given lambdas through a Try:

fs.foreach(f => forkUnsupervised(result.put(Try(f()))))

Unfortunately, the Try type doesn't handle exceptions extending ControlThrowable. You can find here more details.

I already tried changing the ox code locally to add the handling of ControlThrowable, and everything started to integrate smoothly.

Can I open a PR to address the problem? Would you like to chat about it first?

@adamw
Copy link
Member

adamw commented Sep 13, 2024

Hey - sure, a PR would be great. Definitely we don't want any situations where Ox's code just "hangs". Worst case it should throw at least some exception. So maybe that's a wider problem, but let's start with race.

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

Successfully merging a pull request may close this issue.

2 participants