Ktor: Empty body in response using macosx64 target

Created on 6 Feb 2020  路  24Comments  路  Source: ktorio/ktor

Ktor Version and Engine Used (client or server and name)
1.3.0, client (macosx64) (using curl)

Describe the bug
When using macosx64 target in a Multiplatform project I'm getting empty BODY in response

HttpClient: REQUEST: http://api.open-notify.org/astros.json
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: COMMON HEADERS
HttpClient: -> Accept: application/json
HttpClient: -> Accept-Charset: UTF-8
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: null
HttpClient: BODY START
HttpClient: BODY END

The same code for iOS logs

HttpClient: REQUEST: http://api.open-notify.org/astros.json
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: COMMON HEADERS
HttpClient: -> Accept: application/json
HttpClient: -> Accept-Charset: UTF-8
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: null
HttpClient: BODY START
HttpClient: BODY END
HttpClient: BODY Content-Type: application/json
HttpClient: BODY START
HttpClient: {"people": [{"craft": "ISS", "name": "Andrew Morgan"}, {"craft": "ISS", "name": "Oleg Skripochka"}, {"craft": "ISS", "name": "Jessica Meir"}], "message": "success", "number": 3}
HttpClient: BODY END

The following is the common code that uses ktor

class PeopleInSpaceApi {
    private val url = "http://api.open-notify.org/astros.json"

    private val client by lazy {
        HttpClient() {
            install(JsonFeature) {
                serializer = KotlinxSerializer(Json(JsonConfiguration(strictMode = false)))
            }
            install(Logging) {
                logger = Logger.DEFAULT
                level = LogLevel.ALL
            }
        }
    }

    suspend fun fetchPeople(): AstroResult {
        return client.get(url) 
    }
}

I'm using following dependencies

        macOSMain.dependencies {
            // Coroutines
            implementation('org.jetbrains.kotlinx:kotlinx-coroutines-core-macosx64') {
                version {
                    strictly '1.3.3-native-mt'
                }
            }

            // Ktor
            implementation "io.ktor:ktor-client-curl:${Versions.ktor}"
            implementation "io.ktor:ktor-client-core-macosx64:${Versions.ktor}"
            implementation "io.ktor:ktor-client-json-macosx64:${Versions.ktor}"
            implementation "io.ktor:ktor-client-logging-macosx64:${Versions.ktor}"
            implementation "io.ktor:ktor-client-serialization-macosx64:${Versions.ktor}"

            // Serialize
            implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-macosx64:${Versions.kotlinxSerialization}"

        }

(kotlinxSerialization = "0.14.0")

bug

Most helpful comment

Fixed in master

All 24 comments

Re-reading what was logged it looks like initial BODY START and BODY END represents request body (even though this is GET request)? In that case it seems like nothing is being logged for response.

Could you please try 1.3.1? (This will require jcenter repo, maven central sync got stuck as usual)

@cy6erGn0m I just tried 1.3.1 and see same issue. Seems to be slight difference in logging (extra empty line) FWIW

HttpClient: REQUEST: http://api.open-notify.org/astros.json
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: COMMON HEADERS
HttpClient: -> Accept: application/json
HttpClient: -> Accept-Charset: UTF-8
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: null
HttpClient: BODY START
HttpClient: 
HttpClient: BODY END

Let me know if you need any more details....this is being done as part of https://github.com/joreilly/PeopleInSpace project ....it doesn't contain macOS target yet but can create branch with it if it helps

Hi @joreilly, it looks like you're trying to use native-mt branch of coroutines, but release ktor is not compatible with it right now: it leads to unexpected freezes. Could you recheck it with the regular version?

The process of migration to the native-mt version is in progress right now.

Thanks @e5l. I have other issues in project if I switch back to 1.3.3 (e.g. in terms of how I'm using Kotlin code invoked from iOS)....I'll check back once native-mt migration has been completed (let me know if you have dev/pre-release version at any stage you want me to test :) )

@e5l btw is this specific to macOS....I'm using native-mt version of coroutines along with release versions of ktor for Android and iOS and they're working fine?

It depends on how you're calling the suspend function(coroutineDispatcher and nsqueue under the hood). Could you provide a complete sample to clarify the case?

I've pushed my changes to https://github.com/joreilly/PeopleInSpace/tree/macos branch (there's now a macOS XCode project in macos folder)

Following is key part of Kotlin repository class (PeopleInSpaceRepository)...that calls the peopleInSpaceApi.fetchPeople() method shown above. In init it makes api request and stores data in local db (using SQLDelight). This same code as mentioned works on Android and iOS.

    init {
        GlobalScope.launch (Dispatchers.Main) {
            fetchAndStorePeople()
        }
    }

    suspend fun fetchAndStorePeople()  {
        val result = peopleInSpaceApi.fetchPeople()
        result.people.forEach {
            peopleInSpaceQueries.insertItem(it.name, it.craft)
        }
    }

@e5l let me know if there's any more info I can provide....sounded in any case like native-mt migration needs to happen before this will work (for macOS anyway)

@e5l if there's say eap build available at some point I'd be happy to test it out to confirm this issue has been resolved

Seeing the same in our code for macosx64

Should this be fixed in 1.3.2 release?

Unfortunately, the native-mt branch will be compatible only with kotlin 1.3.71 :(

Btw the body reading was fixed in ios in general: it should work without native-mt

I just tried this without native-mt and still seeing same issue. Following are versions I'm using:

const val kotlin = "1.3.70"
const val kotlinCoroutines = "1.3.4"
const val ktor = "1.3.2"
const val kotlinxSerialization = "0.20.0"

Android and iOS are working.

@e5l do you know if there is sample macOS XCode project anywhere that uses Kotlin/Native and Ktor/curl? I still haven't been able to get this working.

Looks like I have the same issue. I made a repo for reproducing there:

https://github.com/martinbonnin/kmpCli/blob/ktor-hangs/src/macosX64Main/kotlin/net/mbonnin/kmpcli/MacOSMain.kt

To reproduce:

git clone https://github.com/martinbonnin/kmpCli
cd kmpCli
git checkout ktor-hangs
 ./gradlew runKmpCliDebugExecutableMacosX64 
  • version 1.2.6 works fine
  • using runBlocking {} instead of mainDispatcher works

Re. suggestion to use runBlocking as workaround, looks like that can't be used in common code (as part of a multiplatform project)?

@joreilly indeed, runBlocking is implemented in the different targets. I guess Javascript is why there's no common runBlocking but for JVM/native, you could make an expect/actual wrapper

Just tried that here and it did the job, thanks. Am now getting back response as shown below. Hopefully this is short term fix until issue is resolved

HttpClient: REQUEST: http://api.open-notify.org/astros.json
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: COMMON HEADERS
HttpClient: -> Accept: application/json
HttpClient: -> Accept-Charset: UTF-8
HttpClient: CONTENT HEADERS
HttpClient: BODY Content-Type: null
HttpClient: BODY START
HttpClient: 
HttpClient: BODY END
HttpClient: RESPONSE: 200 OK
HttpClient: METHOD: HttpMethod(value=GET)
HttpClient: FROM: http://api.open-notify.org/astros.json
HttpClient: COMMON HEADERS
HttpClient: -> Server: nginx/1.10.3
HttpClient: -> Date: Sun, 12 Apr 2020 20:27:20 GMT
HttpClient: -> Content-Type: application/json
HttpClient: -> Content-Length: 308
HttpClient: -> Connection: keep-alive
HttpClient: -> access-control-allow-origin: *
HttpClient: BODY Content-Type: application/json
HttpClient: BODY START
HttpClient: {"people": [{"name": "Andrew Morgan", "craft": "ISS"}, {"name": "Oleg Skripochka", "craft": "ISS"}, {"name": "Jessica Meir", "craft": "ISS"}, {"name": "Chris Cassidy", "craft": "ISS"}, {"name": "Anatoly Ivanishin", "craft": "ISS"}, {"name": "Ivan Vagner", "craft": "ISS"}], "message": "success", "number": 6}
HttpClient: BODY END

Still an issue on macosx with the following dependencies:
val kotlinVersion = "1.3.71"
val serializationVersion = "0.14.0"
val coroutinesVersion = "1.3.6"
val ktorVersion = "1.3.2"

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.

Fixed in master

Was this page helpful?
0 / 5 - 0 ratings