2

I've been running a Ktor server application locally and deploying to Heroku for the last several months without any issues. However after deploying the latest version, I started to see the following error on any POST requests trying to process the request body on Heroku:

java.lang.NoSuchMethodError: java.nio.ByteBuffer.limit(I)Ljava/nio/ByteBuffer;
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at io.ktor.utils.io.ExceptionUtilsJvmKt$createConstructor$$inlined$safeCtor$3.invoke(ExceptionUtilsJvm.kt:103)
    at io.ktor.utils.io.ExceptionUtilsJvmKt$createConstructor$$inlined$safeCtor$3.invoke(ExceptionUtilsJvm.kt:90)
    at io.ktor.utils.io.ExceptionUtilsJvmKt.tryCopyException(ExceptionUtilsJvm.kt:66)
    at io.ktor.utils.io.ByteBufferChannelKt.rethrowClosed(ByteBufferChannel.kt:2456)
    at io.ktor.utils.io.ByteBufferChannelKt.access$rethrowClosed(ByteBufferChannel.kt:1)
    at io.ktor.utils.io.ByteBufferChannel.readRemaining$suspendImpl(ByteBufferChannel.kt:2126)
    at io.ktor.utils.io.ByteBufferChannel.readRemaining(ByteBufferChannel.kt)
    at io.ktor.utils.io.ByteReadChannelKt.readRemaining(ByteReadChannel.kt:217)
    at io.ktor.serialization.SerializationConverter.convertForReceive(SerializationConverter.kt:158)
    at io.ktor.features.ContentNegotiation$Feature$install$3.invokeSuspend(ContentNegotiation.kt:224)
    at io.ktor.features.ContentNegotiation$Feature$install$3.invoke(ContentNegotiation.kt)
    at io.ktor.features.ContentNegotiation$Feature$install$3.invoke(ContentNegotiation.kt)
    at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:248)
    at io.ktor.util.pipeline.SuspendFunctionGun.proceed(SuspendFunctionGun.kt:116)
    at io.ktor.util.pipeline.SuspendFunctionGun.execute(SuspendFunctionGun.kt:136)
    at io.ktor.util.pipeline.Pipeline.execute(Pipeline.kt:78)
    at io.ktor.request.ApplicationReceiveFunctionsKt.receive(ApplicationReceiveFunctions.kt:116)
    at io.ktor.request.ApplicationReceiveFunctionsKt.receiveOrNull(ApplicationReceiveFunctions.kt:135)

Thinking that there might be something unexpected with the request body, I first tried changing call.receive<MyObject>() to call.receiveOrNull<MyObject>(), yet the issue persisted.

I got tipped off that this might be a JVM mismatch issues, so I checked Heroku's Java documentation and learned that the default JVM is 1.8. I then ran my application locally with Java 1.8 and was able to re-create the same behavior.

After changing the target JVM on Heroku for my application to version 11, this has apparently fixed the problem. While I did recently add the Ktor Authentication "feature", I can't see any other changes that might have caused this. I tried to find in the Ktor documentation what the minimum required JVM target is and was not able to find it. It's great that it's working again, but I'd like to have more confidence moving forward.

In order to ensure proper execution of the server and to avoid this kind of issue in the future, I'd like to ask what the supported JVM target(s) are for Ktor Server (and if possible, have this added to the Ktor documentation).

Derek Lee
  • 3,452
  • 3
  • 30
  • 39
  • 4
    This is a bug in Ktor 1.6.5 (https://youtrack.jetbrains.com/issue/KTOR-3358) that will be fixed in the next upcoming release. As a workaround, please use JDK 11 or later. – Aleksei Tirman Nov 23 '21 at 06:25
  • 1
    See [this Q&A](https://stackoverflow.com/q/61267495/2711488) for a discussion of similar incompatibility. In short, when compiling software for an older JDK using a newer JDK, you must use the `--release` option, just using `-source` and `-target` is not enough, as it can lead to such issues – Holger Nov 23 '21 at 08:05
  • Thanks for the tip @Holger! Yes I found similar results when searching for information on this issue; however, it was strange as my Ktor application worked previously without any issues. Sounds like according to @Aleksei Tirman that this is a bug with the current version, so I am now watching that. – Derek Lee Nov 23 '21 at 08:22

1 Answers1

0

I solved by upgrding to ktor_version:1.6.7.

Sam Chen
  • 7,597
  • 2
  • 40
  • 73