-
Notifications
You must be signed in to change notification settings - Fork 182
Reverted: Convert non finite durations #2219
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
Reverted: Convert non finite durations #2219
Conversation
Myabe this can upstream to Scala/scala |
Scala didn't make the same mistake we made. They only have implicit support for asJava on FiniteDuration. Pekko added it for all Durations. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
final implicit class ScalaDurationOps(val self: Duration) extends AnyVal { | ||
def asJava: JDuration = JDuration.ofNanos(self.toNanos) | ||
def asJava: JDuration = self match { | ||
case fd: FiniteDuration => JDuration.ofNanos(fd.toNanos) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe something like :
if (duration.length == 0) JDuration.ZERO
else duration.unit match {
case TimeUnit.NANOSECONDS => JDuration.ofNanos(duration.length)
case TimeUnit.MICROSECONDS => JDuration.of(duration.length, ChronoUnit.MICROS)
case TimeUnit.MILLISECONDS => JDuration.ofMillis(duration.length)
case TimeUnit.SECONDS => JDuration.ofSeconds(duration.length)
case TimeUnit.MINUTES => JDuration.ofMinutes(duration.length)
case TimeUnit.HOURS => JDuration.ofHours(duration.length)
case TimeUnit.DAYS => JDuration.ofDays(duration.length)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The existing solution is def asJava: JDuration = JDuration.ofNanos(self.toNanos)
and this has not caused problems. I know that scala-compat-java8 uses something more like your code but this PR is not about issues with finite durations, it is about issues with non-finite durations (infinite and undefined).
@He-Pin @mdedetrich @raboof I'm not sure whether this change helps much. The code with the bug (not being able to handle infinite or undefined Scala durations) is already inlined into our module jars. |
Technically speaking, this change is correct now, but it may not be a problem in real scenarios. At least I haven't seen anyone report any problems in pekko or akka. |
Sorry I just noticed this now. Actually I am not that supportive of merging this in the 1.x series of Pekko because it can create really confusing behaviour with inlining (as you have pointed out) and I it hasn't been causing any problems in actual production (I understand that its easy to create a scenario where this can break but thats different to people experiencing it in prod). Merging this PR for 2.0.x is absolutely fine, but with how inlining works for the 1.x series of Pekko, we can't even announce it as a normal bug fix because replacing the pekko jar wouldn't work, they would have also to replace every pekko satellite project that the user happens to be running. What are your thoughts about reverting this change? I can still see your argument so I am not going to push strongly, but it is a really confusing experience for end users. |
Thanks @mdedetrich. I'm away from my computer today. I'll think about reverting this and will attempt to justify it otherwise. |
I would say that if we keep this PR, we would need to do a new release for every single Pekko satellite project because thats the only way we can actually state that this fix would work as it would only inline at compile time. Not saying that we should do all releases immediately, but we need to plan about doing at least one such release per satellite project. Its also a bit annoying as Pekko Connectors itself just had a 1.2.0 release, but it is what it is. |
@pjfanning So the inline is been enabled in Pekko 1.2? |
Inlining has been enabled since 1.1.x (iirc) |
Opps。 |
So far, I've only seen the issue in pekko-http - so one solution is that in pekko-http 1.3.0 that we fix the problematic conversions. They are fixed in main branch already. |
This reverts commit d9cab76.
Basically we have a bug in pekko.util.JavaDurationConverters.
It allows you to compile:
But this will fail at runtime because pekko.util.JavaDurationConverters calls toNanos on the scala duration and this fails with
I found a case in pekko-http where we have a scala.concurrent.duration.Duration that we call asJava on and that could fail if the duration was set to a non-finite one (which is allowed).
Scala's builit-in converters for Durations only allow FiniteDuration instances to be converted while Pekko allows all Durations.
Java does not support Inifinite durations but this patch converts to the max allowed duration which is very long and safe enough.
Targetting 1.x as in 2.0.0, we will likely be deleting pekko.util.JavaDurationConverters