Skip to content

[BUG] Ox forks don't work inside direct-style server handler methods #4747

@polkx

Description

@polkx

Tapir version: 1.11.41

Scala version: 3.7.2

Describe the bug

I'm trying to understand the OX library, and I'm stuck on a problem. I have a simple HTTP server that accepts requests and has a long-running task. So, I immediately return the status code "Accepted" and run the long-running task on a separate thread. The error I get is:

java.lang.IllegalStateException: Forks can only be started inside a concurrency scope, or from other forks started in the scope.

How to reproduce?

//> using scala 3.7.2
//> using dep com.softwaremill.sttp.tapir::tapir-core:1.11.41
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.11.41
//> using dep ch.qos.logback:logback-classic:1.5.18

import ox.*
import sttp.model.StatusCode
import sttp.tapir.*
import sttp.tapir.server.netty.sync.NettySyncServer

import scala.concurrent.duration.DurationInt

object Main extends OxApp:
  def run(args: Vector[String])(using Ox): ExitCode =
    val helloWorld = endpoint.get
      .in("hello")
      .in(query[String]("name"))
      .out(stringBody)
      .out(statusCode(StatusCode.Accepted))
      .handleSuccess { name =>
        println("Request")
        fork(longRunningTask())
        println("Response")
        s"Hello, $name!"
      }

    NettySyncServer().addEndpoint(helloWorld).startAndWait()
    ExitCode.Success

  private def longRunningTask(): Unit =
    println("Start")
    sleep(5.seconds)
    println("End")

Additional information
Endpoint for testing: http://localhost:8080/hello?name=xxx

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions