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

Move task cancelation on blocking pool #1851

Merged
merged 1 commit into from
Feb 16, 2022

Conversation

danilbykov
Copy link
Contributor

Now task cancelation is executed on main vertx pool. But task cancelation may be blocking operation. Here is such example:

import zio._
import zio.blocking.Blocking

val task =
   for {
     blocking <- ZIO.service[Blocking.Service]
     _ <- blocking.effectBlocking({
       println(Thread.currentThread().getName())
       Thread.sleep(5000)
       println("end")
     })
  } yield ()

val runtime = Runtime.default

val canceler = runtime.unsafeRunAsyncCancelable(task) { _ => () }

canceler(Fiber.Id.None) // this call is blocked by Thread.sleep(5000)

So interrupting of several tasks may block main vertx pool and leads to following warnings

Thread Thread[vert.x-eventloop-thread-1,5,zio-default-async] has been blocked for 6381 ms, time limit is 2000 ms

So it is better to execute this operation on blocking pool.

@@ -26,7 +26,7 @@ object VertxZioServerOptions {
CustomInterceptors(
createOptions = (ci: CustomInterceptors[RIO[R, *], VertxZioServerOptions[RIO[R, *]]]) =>
VertxZioServerOptions(
Defaults.createTempFile().getParentFile.getAbsoluteFile,
new java.io.File(System.getProperty("java.io.tmpdir")).getAbsoluteFile,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noted that new temp file is created each time when server is started. It seems that it is equivalent code to get temp dir.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, thanks :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually the same code is present in cats & future interpreters, so maybe we could make ew java.io.File(System.getProperty("java.io.tmpdir")) an internal utility function and simply use it in these places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. I moved this line inside function VertxServerOptions.uploadDirectory()

@@ -75,14 +75,20 @@ trait VertxZioServerInterpreter[R] extends CommonServerInterpreter {

rc.response.exceptionHandler { (t: Throwable) =>
cancelRef.getAndSet(Some(Left(t))).collect { case Right(c) =>
c(FiberId.None)
rc.vertx().executeBlocking[Unit]((promise: Promise[Unit]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Shouldn't we do the same in VertxCatsServerInterpreter ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this issue is not applied for VertxCatsServerInterpreter because CancelToken is function from Unit to Future[Unit]. So calling such function doesn't block caller and just returns Future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok:)

@adamw adamw merged commit 1b2b9a5 into softwaremill:master Feb 16, 2022
@adamw
Copy link
Member

adamw commented Feb 16, 2022

Thanks!

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 this pull request may close these issues.

2 participants