retryResult signature makes it easy to use wrong context
See original GitHub issueIt seems that issue #4 was closed, but I have also inadvertently run into this situation today with similar code:
withTimeout(1000) {
retryResult {
// This crashes because coroutineContext is obtained from the withTimeout scope
// println("My retryStatus is ${coroutineContext.retryStatus}")
// This is okay (naming conflict with coroutineContext)
println("My retryStatus is ${kotlin.coroutines.coroutineContext.retryStatus}")
// This is also okay
coroutineScope {
println("My retryStatus is ${coroutineContext.retryStatus}")
}
Ok(Unit)
}
}
I was able to workaround this issue fairly easily, but it is easy to shoot yourself in the foot. I understand it would be a breaking api change to update the type signature of ResultProducer. Is there any other reason why you wouldn’t want to provide the proper scope to the retryResult block?
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Issues · michaelbull/kotlin-retry
Issues list. retryResult signature makes it easy to use wrong context. #18 opened on Nov 15, 2021 by vrendina.
Read more >Retry Failing Tasks with Cats and Scala - Alexandru Nedelcu
And the raiseError function lifts an E error into the F[A] context (the equivalent of Java's and Scala's throw for exceptions).
Read more >Retry a method based on result (instead of exception)
I have been using failsafe build in retry. You can retry based on predicates and exceptions. Your code would look like this:
Read more >Kubernetes contexts from agent not injected into pipeline of ...
This actually does not fix the issue. Without the KUBE_CONTEXT variable, the deployment will use the wrong context (the default one using the ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve gone ahead and made a rather significant refactoring that would be part of a v2 in 2babe2a1f91308d642b66df4646aacb9eb0dcb78. The main benefit is that it removes the burden on the caller of interfacing with the
coroutineContext. Instead, both the policies and the calling code are written in context of aRetryScope(analogous to a CoroutineScope or a SequenceScope from the stdlib), which gives readonly access to the attempts, previous delay, cumulative delay, etc.This results in a lot less boilerplate in both the calling code and the policy implementations:
The current type signature for
retryResult(without the typealias) is:Updating the signature to include the
CoroutineScopeas the lambda receiver would break any clients that have explicitly defined theblocktype. Including those clients which are using method references for example::someFunctionThatReturnsResult.Need to think a little more about the most appropriate solution.