ghostdogpr / caliban

Functional GraphQL library for Scala

Home Page:https://ghostdogpr.github.io/caliban/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Enhancing Cancellation Handling in ZQuery Sequences

asher-bold opened this issue · comments

I'm seeking guidance regarding Scala Caliban's ZQuery sequences for long running queries. Specifically, I'm looking to improve the handling of request cancellations and to prevent the execution of pending ZQuery operations.

Currently, if a client cancels during an ongoing ZQuery sequence, the operations continue until completion. Here's a simplified snippet:

result <- ZQuery.succeed({
  // Simulating resource-intensive operation
  Thread.sleep(5000L)
  "Result Value"
})
_ <- ZQuery.fromZIO(ZIO.succeed({
  println("Second action")
}))
yield result

When a client cancels during the "sleep" operation, I'm aiming to instantly halt subsequent ZQuery operations. Your insights, suggestions, or code examples within the Caliban framework would greatly assist in improving cancellation responsiveness. Thank you.

I don't think zio is able to cancel a resource-intensive operation in the middle if it doesn't run within the IO loop. But it should be able to cancel before "second action" is executed. Is that not the case?

Even after being canceled, the 'second action' continues to execute.

What http lib and interpreter are you using? I believe cancellation should work on Caliban/ZQuery side but not sure if the cancellation is called by the http library and/or tapir.

http4s with blaze

I just investigated this a little more and my conclusion is that the issue is not in Caliban or ZQuery. One way to verify that is to use the timeout wrapper in Caliban: when we reach a timeout, the current operation is canceled by Caliban and we can see that the second action is not printed. However when using a client and cancelling the current request, Caliban never receives the signal that the request is cancelled. I am not sure if the issue is in the http server library itself or in Tapir, but we need to receive the cancellation signal in order to do anything.