Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't rethrow exceptions after handling message #574

Merged
merged 1 commit into from
Oct 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import cats.effect.syntax.all._
import cats.effect.{Sync, SyncIO}
import cats.syntax.flatMap._
import cats.syntax.functor._
import cats.syntax.applicativeError._
import fs2._
import fs2.grpc.client.ClientOptions
import fs2.grpc.shared.StreamOutput
Expand Down Expand Up @@ -153,6 +154,7 @@ private[client] object Fs2UnaryCallHandler {
case Outcome.Errored(e) => F.delay(call.cancel(e.getMessage, e))
case Outcome.Canceled() => onCancel(call)
}
.handleError(_ => ())
.start
.map(sending => Some(sending.cancel >> onCancel(call)))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ private[server] trait Fs2ServerCallListener[F[_], G[_], Request, Response] {
call.sendHeaders(headers) *> call.request(1) *> sendResponse.compile.drain

private def unsafeRun(f: F[Unit])(implicit F: Async[F]): Unit = {
val bracketed = F.guaranteeCase(f) {
case Outcome.Succeeded(_) => call.closeStream(Status.OK, new Metadata())
case Outcome.Canceled() => call.closeStream(Status.CANCELLED, new Metadata())
case Outcome.Errored(t) => reportError(t)
}
val bracketed =
F.handleError {
F.guaranteeCase(f) {
case Outcome.Succeeded(_) => call.closeStream(Status.OK, new Metadata())
case Outcome.Canceled() => call.closeStream(Status.CANCELLED, new Metadata())
case Outcome.Errored(t) => reportError(t)
}
}(_ => ())

// Exceptions are reported by closing the call
dispatcher.unsafeRunAndForget(F.race(bracketed, isCancelled.get))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ private[server] final class Fs2ServerCall[Request, Response](

private def run[F[_]](completed: F[Unit], dispatcher: Dispatcher[F])(implicit F: Sync[F]): SyncIO[Cancel] = {
SyncIO {
val cancel = dispatcher.unsafeRunCancelable(F.guaranteeCase(completed) {
case Outcome.Succeeded(_) => close(Status.OK, new Metadata()).to[F]
case Outcome.Errored(e) => handleError(e).to[F]
case Outcome.Canceled() => close(Status.CANCELLED, new Metadata()).to[F]
})
val cancel = dispatcher.unsafeRunCancelable(
F.handleError {
F.guaranteeCase(completed) {
case Outcome.Succeeded(_) => close(Status.OK, new Metadata()).to[F]
case Outcome.Errored(e) => handleError(e).to[F]
case Outcome.Canceled() => close(Status.CANCELLED, new Metadata()).to[F]
}
}(_ => ())
)
SyncIO(cancel()).void
}
}
Expand Down
28 changes: 27 additions & 1 deletion runtime/src/test/scala/fs2/grpc/server/ServerSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,32 @@ class ServerSuite extends Fs2GrpcSuite {
assertEquals(dummy.currentStatus.get.isOk, true)
}

runTest("messages to streamingToStreaming with error") { (tc, d) =>
runTest("messages to streamingToStreaming with error") { (tc, d0) =>
@volatile var errorInDispatcher = false
val d = new Dispatcher[IO] {
import scala.concurrent._
def unsafeToFutureCancelable[A](fa: IO[A]): (Future[A], () => Future[Unit]) = {
// d0.unsafeToFutureCancelable(fa)
implicit val parasitic: ExecutionContext = new ExecutionContext {
def execute(runnable: Runnable) = runnable.run()
def reportFailure(t: Throwable) = t.printStackTrace()
}

val (fut, cancel) = d0.unsafeToFutureCancelable(fa)
val reported = fut.transform(
identity,
t => {
errorInDispatcher = true
t
}
)
(reported, cancel)
}

override def unsafeRunSync[A](fa: IO[A]): A =
d0.unsafeRunSync(fa.onError(_ => IO { errorInDispatcher = true }))
}

val dummy = new DummyServerCall
val error = new RuntimeException("hello")

Expand All @@ -225,6 +250,7 @@ class ServerSuite extends Fs2GrpcSuite {
assertEquals(dummy.messages.toList, List(1, 2, 0))
assertEquals(dummy.currentStatus.isDefined, true)
assertEquals(dummy.currentStatus.get.isOk, false)
assert(!errorInDispatcher, "no error should be encountered by the dispatcher")
}

runTest("streamingToStreaming send respects isReady") { (tc, d) =>
Expand Down