From 4cc37a8902a55a5c506906588a2c2af30de6c915 Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Mon, 16 Nov 2026 07:59:55 +0000 Subject: [PATCH 1/2] Fix ZIO streamed request bodies hanging when sent in an uninterruptible region Fixes #3072 --- .../impl/zio/InterruptibleStreamPublisher.scala | 4 +++- .../sttp/client4/impl/zio/ZioStreamingTest.scala | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/effects/zio/src/main/scalajvm/sttp/client4/impl/zio/InterruptibleStreamPublisher.scala b/effects/zio/src/main/scalajvm/sttp/client4/impl/zio/InterruptibleStreamPublisher.scala index 0dc45c7e7..61cb7ea50 100644 --- a/effects/zio/src/main/scalajvm/sttp/client4/impl/zio/InterruptibleStreamPublisher.scala +++ b/effects/zio/src/main/scalajvm/sttp/client4/impl/zio/InterruptibleStreamPublisher.scala @@ -14,7 +14,9 @@ private[client4] object InterruptibleStreamPublisher { */ // zio-interop only stops requesting elements on cancel, so a stream waiting for its next element never ends def apply[A](stream: ZStream[Any, Throwable, A]): UIO[Publisher[A]] = - ZIO.runtime[Any].map { runtime => + // the captured runtime's flags are inherited by the stream's fibers; if interruption was disabled (e.g. `send` was + // called in an uninterruptible region), `interruptWhen` would wait forever for its promise-awaiting fiber to end + ZIO.interruptible(ZIO.runtime[Any]).map { runtime => new Publisher[A] { override def subscribe(subscriber: Subscriber[_ >: A]): Unit = { Objects.requireNonNull(subscriber) diff --git a/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala b/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala index 362beb0a0..eeec1c5d2 100644 --- a/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala +++ b/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala @@ -1,8 +1,10 @@ package sttp.client4.impl.zio import sttp.capabilities.zio.ZioStreams +import sttp.client4._ import sttp.client4.internal._ import sttp.client4.testing.ConvertToFuture +import sttp.client4.testing.HttpTest.endpoint import sttp.client4.testing.streaming.StreamingTest import sttp.model.sse.ServerSentEvent import zio._ @@ -32,4 +34,17 @@ trait ZioStreamingTest extends StreamingTest[Task, ZioStreams] with ZioTestBase override def failingBodyProducer(chunk: Array[Byte], error: Throwable): ZStream[Any, Throwable, Byte] = ZStream.fromChunk(Chunk.fromArray(chunk)) ++ ZStream.fail(error) + + "send a streamed request body when sending is uninterruptible" in { + basicRequest + .post(uri"$endpoint/streaming/echo") + .streamBody(streams)(bodyProducer(List("hello".getBytes(Utf8)))) + .response(asStringAlways) + // an uninterruptible effect can't be timed out, so the read timeout is the only way to end a hanging request + .readTimeout(scala.concurrent.duration.Duration(5, "s")) + .send(backend) + .uninterruptible + .map(_.body shouldBe "hello") + .toFuture() + } } From 554113464da3c54b4c05ccb0feaa6d36c99e08aa Mon Sep 17 00:00:00 2001 From: Adam Warski Date: Mon, 16 Nov 2026 08:17:29 +0000 Subject: [PATCH 2/2] Fix test compilation on Scala 2 --- .../client4/impl/zio/ZioStreamingTest.scala | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala b/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala index eeec1c5d2..4e6a87136 100644 --- a/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala +++ b/effects/zio/src/test/scala/sttp/client4/impl/zio/ZioStreamingTest.scala @@ -36,15 +36,16 @@ trait ZioStreamingTest extends StreamingTest[Task, ZioStreams] with ZioTestBase ZStream.fromChunk(Chunk.fromArray(chunk)) ++ ZStream.fail(error) "send a streamed request body when sending is uninterruptible" in { - basicRequest - .post(uri"$endpoint/streaming/echo") - .streamBody(streams)(bodyProducer(List("hello".getBytes(Utf8)))) - .response(asStringAlways) - // an uninterruptible effect can't be timed out, so the read timeout is the only way to end a hanging request - .readTimeout(scala.concurrent.duration.Duration(5, "s")) - .send(backend) - .uninterruptible - .map(_.body shouldBe "hello") - .toFuture() + convertToFuture.toFuture( + basicRequest + .post(uri"$endpoint/streaming/echo") + .streamBody(streams)(bodyProducer(List("hello".getBytes(Utf8)))) + .response(asStringAlways) + // an uninterruptible effect can't be timed out, so the read timeout is the only way to end a hanging request + .readTimeout(scala.concurrent.duration.Duration(5, "s")) + .send(backend) + .uninterruptible + .map(_.body shouldBe "hello") + ) } }