zio icon indicating copy to clipboard operation
zio copied to clipboard

Missing stacktraces from right-side effect of `.flatMap`

Open kluen opened this issue 2 years ago â€ĸ 9 comments

When watching @adamw's recent talk where he compares the usability of direct-style approaches to plain ZIO, I was a bit surprised to see that he argued that ZIO's stacktraces are only "basic". In particular, I was surprised about ZIO's behaviour in this example.

I experimented a bit to understand the behaviour better and found that the stacktrace only contains the "frames" of the left-hand side of a flatMap. Here is an example to show the issue:

//> using scala 3.4
//> using dep dev.zio::zio:2.0.22

import zio.*

object PrintsStacktrace extends ZIOAppDefault:
  def run = a
  def a = b.flatMap(_ => ZIO.unit)
  def b = c.flatMap(_ => ZIO.unit)
  def c = ZIO.fail("The failure")

This prints:

timestamp=2024-04-15T09:50:00.275792Z level=ERROR thread=#zio-fiber-1 message="" cause="Exception in thread "zio-fiber-4" java.lang.String: The failure
	at <empty>.PrintsStacktrace.c(PrintsStacktrace.scala:10)
	at <empty>.PrintsStacktrace.b(PrintsStacktrace.scala:9)
	at <empty>.PrintsStacktrace.a(PrintsStacktrace.scala:8)"

which includes the helpful info on where the effects where constructed.

Here is the example where we do not get helpful output:

//> using scala 3.4
//> using dep dev.zio::zio:2.0.22

import zio.*

object NoStacktrace extends ZIOAppDefault:
  def run = a
  def a = ZIO.unit.flatMap(_ => b)
  def b = ZIO.unit.flatMap(_ => c)
  def c = ZIO.fail("The failure")

which prints

timestamp=2024-04-15T09:51:59.317835Z level=ERROR thread=#zio-fiber-1 message="" cause="Exception in thread "zio-fiber-4" java.lang.String: The failure
	at <empty>.NoStacktrace.c(NoStacktrace.scala:10)"

I am not sure if this behaviour is intentional, but I agree that this is not ideal from a usability standpoint, because we are missing valuable information to locate the failed effect.

kluen avatar Apr 15 '24 10:04 kluen

/bounty $100

I would expect to see a and b in the stack trace of c.

jdegoes avatar May 08 '24 18:05 jdegoes

💎 $100 bounty â€ĸ ZIO

Steps to solve:

  1. Start working: Comment /attempt #8747 with your implementation plan
  2. Submit work: Create a pull request including /claim #8747 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!

Add a bounty â€ĸ Share on socials

Attempt Started (GMT+0) Solution
🟡 @itsdheerajdp Sep 7, 2024, 7:14:46 AM WIP

algora-pbc[bot] avatar May 08 '24 18:05 algora-pbc[bot]

@jdegoes just want to add a requirement to this ticket if possible?

Fixing this can negatively impact performance of the runloop depending on the implementation. I'm actually not sure if it's even possible to fix it without impacting performance to some degree.

I thin any attempted solution should either :

  1. Provide before/after benchmark results of the NarrowFlatMapBenchmark and BroadFlatMapBenchmark suites (at the very least) showing that the runloop performance wasn't affected, or
  2. Perhaps create a runtime flag (disabled by default?) to enable detailed stack traces

kyri-petrou avatar May 09 '24 00:05 kyri-petrou

/attempt #8747

Algora profile Completed bounties Tech Active attempts Options
@itsdheerajdp 4 bounties from 2 projects
HTML, JavaScript,
TypeScript & more
īšŸ9153
Cancel attempt

dheerajsingh89 avatar Sep 07 '24 07:09 dheerajsingh89

@itsdheerajdp: Reminder that in 7 days the bounty will become up for grabs, so please submit a pull request before then 🙏

algora-pbc[bot] avatar Sep 14 '24 07:09 algora-pbc[bot]