zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
757 stars 387 forks source link

ClassCastException when running ServerSentEventEndpoint Example #2695

Closed khajavi closed 6 days ago

khajavi commented 5 months ago
  1. Run the ServerSentEventEndpoint from the example project.
  2. Run curl -N "http://127.0.0.1:8080/sse" -i

Expected: The curl should print the events created by the server to the console

Current Behaviour: The curl prints the following message:

> curl -N "http://127.0.0.1:8086/sse" -i
HTTP/1.1 200 OK
content-type: application/json
transfer-encoding: chunked

curl: (18) transfer closed with outstanding read data remaining

The app prints these exceptions:

Feb 23, 2024 11:56:28 AM io.netty.channel.DefaultChannelPipeline onUnhandledInboundException
WARNING: An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.
java.lang.ClassCastException: class zio.http.ServerSentEvent cannot be cast to class java.lang.Byte (zio.http.ServerSentEvent is in unnamed module of loader 'app'; java.lang.Byte is in module java.base of loader 'bootstrap')
    at scala.runtime.BoxesRunTime.unboxToByte(BoxesRunTime.java:91)
    at scala.runtime.ScalaRunTime$.array_update(ScalaRunTime.scala:80)
    at zio.Chunk$Singleton.toArray(Chunk.scala:1818)
    at zio.Chunk.toArray(Chunk.scala:1002)
    at zio.Chunk.toArray(Chunk.scala:873)
    at zio.http.netty.NettyBodyWriter$.$anonfun$writeAndFlush$14(NettyBodyWriter.scala:123)
    at zio.http.netty.NettyFutureExecutor$.$anonfun$make$1(NettyFutureExecutor.scala:67)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:904)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:967)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:948)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:967)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1024)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:967)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1024)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1024)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:967)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:890)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1024)
    at zio.internal.FiberRuntime.runLoop(FiberRuntime.scala:1024)
    at zio.internal.FiberRuntime.evaluateEffect(FiberRuntime.scala:381)
    at zio.internal.FiberRuntime.evaluateMessageWhileSuspended(FiberRuntime.scala:504)
    at zio.internal.FiberRuntime.drainQueueOnCurrentThread(FiberRuntime.scala:220)
    at zio.internal.FiberRuntime.run(FiberRuntime.scala:139)
    at zio.internal.ZScheduler$$anon$4.run(ZScheduler.scala:478)

Please note that I have written another server-sent example that works properly:

object ServerSentExample extends ZIOAppDefault {

  val stream: ZStream[Any, Nothing, ServerSentEvent] =
    ZStream.repeatWithSchedule(ServerSentEvent(ISO_LOCAL_TIME.format(LocalDateTime.now)), Schedule.spaced(1.second))

  val app =
    Routes(
      Method.GET / "sse" ->  handler {
          Response.fromServerSentEvents(stream)
      }
    ).toHttpApp
  def run = Server.serve(app).provide(Server.default)
}
jdegoes commented 2 months ago

/bounty $150 for identifying and fixing the underlying class cast exception, and having a test to ensure it doesn't happen anymore.

algora-pbc[bot] commented 2 months ago

πŸ’Ž $150 bounty β€’ ZIO

Steps to solve:

  1. Start working: Comment /attempt #2695 with your implementation plan
  2. Submit work: Create a pull request including /claim #2695 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bounty β€’ Share on socials

Attempt Started (GMT+0) Solution
πŸ”΄ @varshith257 Jun 22, 2024, 6:47:32 PM WIP
🟒 @987Nabil #2951
varshith257 commented 1 month ago

/attempt #2695

algora-pbc[bot] commented 1 month ago

@varshith257: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then πŸ™

algora-pbc[bot] commented 1 month ago

The bounty is up for grabs! Everyone is welcome to /attempt #2695 πŸ™Œ

algora-pbc[bot] commented 3 weeks ago

πŸ’‘ @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] commented 6 days ago

πŸŽ‰πŸŽˆ @987Nabil has been awarded $150! 🎈🎊