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..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 @@ -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,18 @@ 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 { + 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") + ) + } }