Action still waiting for future running in same executionContext

Hello,

I don’t understand why a future created with the default play ExecutionContext like the code below is blocking the Action.

I tried in dev & prod mode in a fresh new project with the same results. Action always wait 10 sec for rendering whereas there is no dependency with the future block.

I thought the future would run in another thread of ExecutionContext, can someone explain me why it’s not the case ?
It shoud work like using ec2 executionContext.

@Singleton
class HomeController @Inject()(
  val controllerComponents: ControllerComponents
)(implicit ec: ExecutionContext) extends BaseController {

  val ec2 = ExecutionContext.fromExecutor(
    Executors.newFixedThreadPool(2)
  )

  def index() = Action { implicit req =>

    println("start")
    Future {

      Thread.sleep(10000)
      println("future end")

    }(ec).recover{ case (ex) => ex.printStackTrace() }

    println("end")

    Ok(UUID.randomUUID().toString)
  }

  def index2() = Action.async { implicit req =>

    println("start")
    Future {

      Thread.sleep(10000)
      println("future end")

    }(ec).recover{ case (ex) => ex.printStackTrace() }

    println("end")

    Future{
      Ok(UUID.randomUUID().toString)
    }
  }
}

Hi @bpo,
As far as I know if you are running a hard computation is a better approach to run this in a isolated ExecutionContext. Akka has a loot of configurations to create your own dispather that implements ExecutionContext Interface, look at https://doc.akka.io/docs/akka/current/dispatchers.html .

If you running a hard computation in the same execution context then you ill blocked the others call in the same ExecutionContext(EXC). How the EXC ill process the others calls if its busy with the hard computation ?

Yes, i know it’s not recommended to do this.
But why the action wait for that future ? it should return immediately and consume the future in another thread of ec (play default execution context).

@bpo
The ExecutionContext process all threads in same context, if one thread has a hard computation the other ones ill be blocked even if they are diferent futures.

What is the parallelism-factor setting for the default execution context? As I recall, the default is small.

Hi, @chrono_b
Sory for a lazy response, there is the default akka config https://github.com/akka/akka/blob/v2.6.8/akka-docs/src/test/scala/docs/dispatcher/DispatcherDocSpec.scala#L49-L67