title: How to run a background task in Spring using coroutines in a reactive application while properly inheriting the reactor context
body: <p>I would like to perform an async task in Spring (similar to @Async) using coroutines.</p>
  <p>I've tried the following:</p>
  <pre><code>@PostMapping(&quot;/async&quot;)
  suspend fun post(): ResponseEntity&lt;Unit&gt; {
      logger.info { &quot;Received request&quot; }
  
      val context = coroutineContext
      logger.info { &quot;coroutineContext=${context}&quot; }
  
      CoroutineScope(Dispatchers.Default + coroutineContext).launch {
          delay(5000)
          logger.info { &quot;io + context, 5000ms&quot; }
      }
  
      CoroutineScope(Dispatchers.IO + coroutineContext).launch {
          delay(5000)
          logger.info { &quot;default + context, 5000ms&quot; }
      }
  
      logger.info { &quot;Returning response&quot; }
      return ResponseEntity.accepted()
              .build()
  }
  </code></pre>
  <p>Spring seems to be waiting for the coroutine to finish before returning the response.</p>
  <p>I would also like to inherit the context so I can inherit trace information while inside the coroutine (I am using sleuth).</p>