-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Version 1.1.0 breaks referential equality of thrown exceptions #921
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
Comments
I am having exactly the same issue. I rely on some error codes (int based) provided inside an Exception, and whatever the error code is originally, it always ends up being 0 once it has gone through "resumeWithException" inside my "suspendCoroutine" block. This change breaks all the error handling in my application. |
The latest info I have is from @qwwdfsad on Twitter:
My assumptions about mutability were wrong, but it seems from your comment that this cloning behavior should at least be documented somewhere so people include errorcodes etc in the copies. |
So let me further explain my point. This behaviour is opt-out rather than opt-in to help newbies with exception handling. Making this behaviour opt-in is much less discoverable and useful. To reduce the impact of broken referential equality, it is enabled only in debug mode. I would be happy to provide readable stackraces without making reflective cloning, but I am not aware of other mechanisms of doing so except mutating real exception stacktrace (and this is much worse). Even though we can live with the fact that
For example, |
What we can do is to provide some way to indicate that exception is non-copyable or to instruct We can either introduce a global settable state (global functions like I am open to discuss other solutions or concerns about proposed ones. |
I think option one should be a quick win. The problem I see with having marker interfaces is that any third party libraries implementing exceptions classes won't necessarily abide to this rule, and therefore users of those exception classes will be forced to extend them (as long as they are not final) and copying them every time they need to be used. The particular use case for me is AerospikeException, which basically ends up storing "0" in all its internal |
The period of being a newbie is short, while there are many medior/experts (and probably some of those newbies too) that'll get bitten by this issue and will need to go google it up, potentially hurting their product and losing valuable engineering time. Having it opt-in with documentation prevents this, and still helps newbies if instead you improve the discoverability and documentation of the parameter for them.
Complicated debugging because it's noisy and it doesn't record thread jumps, not because there is anything broken about them nor they cause problems in client code. A completely different problem than the one described by this report.
With typeclasses as per KEEP-87 you could get the best of both :) In this case it'd have to be the first to work for any exception because that's precisely the worst and most common case where you'll wish it existed, and have documentation somewhere about the tradeoff and the fix. |
@pakoito Type Classes have to be linked at build time, it cannot become a pluggable solution. As alternative solution, it is possible to provide an external component to rebuild the right stack trace, so we does not have to reinstantiate the |
Thanks or the use-case! This is a good point against marker interfaces.
This is a debatable point, in my observations, most of the users remain "newbies" beyond basic public API surface. And automatic debuggability improvements boost productivity.
Exception cloning was done exactly to record threads (actually, coroutines) jumps. I agree that debuggability should be properly documented, I am going to do it soon, including trade-offs and rationale behind these changes.
It does not work :) We already have experience with Kotlin flags itself (and Kotlin is much more popular than @fvasco what you are proposing is actually a magic pill "make it work as desired but without any changes in a previous behaviour". It is impossible to do so concurrently, safely and properly working in large parent-child hierarchies. Moreover, |
I hope this is a shared goal :-)
Using the current implementation it should be possible, without create a new
Obvious, nor we have to request this to every developer. I am considering the root problem: debug the code using a rich stack trace, so it may be enough print the stack when a coroutine crashes. I try to explain my idea with an example fun main(args: Array<String>) {
try {
runBlocking(Dispatchers.Default) {
launch(CoroutineName("main")) { error("fail") }
}
} catch (e: Exception) {
println("Program quit with error:")
e.printStackTrace()
}
}
So in debug this code should print two stack trace, the "right" one and the original one.
|
@ganet @qwwdfsad I think we can have smarter logic that would detect "non copyable" exceptions. Actually, I think any exception that stores additional state beyond the message, cause, etc (e.g. adds additional fields to the ones available in |
@pakoito Can you, please, clarify your use-case for identify comparison of exceptions? I see that your tests had started to fail, but that is not a use-case. I'm interested in an actual application-level use-case that would call for checking exceptions by their reference identity. |
This is not about comparing exceptions by reference as a feature. That's silly for anyone to do, we fixed it and that's it. Other people will still do it tho, and we both have to deal with it. This is about a new set of subtle errors (what I called a footgun on twitter) introduced in an update that took a tradeoff to add a new feature turned on by default in a way that will mostly help newbies and confuse experts, and didn't even document it or announced it on the patchnotes. @qwwdfsad defended the tradeoff, agreed that exceptions in other frameworks are not broken, accepted to add documentation for this new kx.coroutines-exclusive behavior, and with that my job here is done 😄 |
@pakoito Thanks for your report. This feature is still experimental and the whole reason we've rolled it out was to gather feedback. It is indeed a tradeoff and we would have never even attempted to do it this way, but we've learned that Java's |
This also broke several unit tests of mine that test error cases where the underlying dependency is using an RxJava 2 Here is a sample repro (not actually one of my tests 🙂): @Test
fun test() = runBlocking<Unit> {
val exception = RuntimeException()
val errorSingle = Single.error<String>(exception)
var actualException: Exception? = null
try {
errorSingle.await()
} catch (ex: Exception) {
actualException = ex
}
assertThat(actualException).isEqualTo(exception) // Using AssertJ
} With the error being:
|
Fixed in 1.2.0-alpha |
Thanks @qwwdfsad |
When the flag
kotlinx.coroutines.stacktrace.recovery
is set totrue
, as per the default, exceptions thrown within the library aremutated orcloned so they lose referential equality properties.We found this regression in Arrow when updating our adapter module to 1.1.0. We run a test suite that included some referential equality tests in this version. These referential equality tests were removed in the update process, as you can see in master. At least another library was affected, Vert.x, as stated here.
Even if this one fix could be applied to keep the Arrow adapter and Vert.x releases going, there is merit to the discussion of avoiding different behaviors between debug and production, and breaking a property that's respected by other concurrency libraries such as Project Reactor, RxJava, Scalaz, and Scala/Cats.
An alternative like
Throwable#addSuppressed
doesn't break referential equality, even if it mutates the exception. Defaulting tofalse
and making the environment variable more visible in the documentation is another viable alternative :DThe text was updated successfully, but these errors were encountered: