Skip to content
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

Empty body in response using macosx64 target #1622

Closed
joreilly opened this issue Feb 6, 2020 · 24 comments
Closed

Empty body in response using macosx64 target #1622

joreilly opened this issue Feb 6, 2020 · 24 comments
Assignees
Labels
Milestone

Comments

@joreilly
Copy link
Contributor

joreilly commented Feb 6, 2020

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")

@joreilly joreilly added the bug label Feb 6, 2020
@joreilly joreilly changed the title Empty body (and null Content-Type) in response using macosx64 target Empty body in response using macosx64 target Feb 6, 2020
@joreilly
Copy link
Contributor Author

joreilly commented Feb 6, 2020

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.

@cy6erGn0m
Copy link
Contributor

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

@joreilly
Copy link
Contributor Author

joreilly commented Feb 7, 2020

@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

@joreilly
Copy link
Contributor Author

joreilly commented Feb 7, 2020

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

@cy6erGn0m cy6erGn0m added this to the 1.3.2 milestone Feb 7, 2020
@e5l
Copy link
Member

e5l commented Feb 7, 2020

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.

@joreilly
Copy link
Contributor Author

joreilly commented Feb 7, 2020

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 :) )

@joreilly
Copy link
Contributor Author

joreilly commented Feb 7, 2020

@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?

@e5l
Copy link
Member

e5l commented Feb 7, 2020

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?

@joreilly
Copy link
Contributor Author

joreilly commented Feb 7, 2020

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)
        }
    }

@joreilly
Copy link
Contributor Author

@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)

@joreilly
Copy link
Contributor Author

joreilly commented Mar 2, 2020

@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

@glootjofo
Copy link

glootjofo commented Mar 3, 2020

Seeing the same in our code for macosx64

@joreilly
Copy link
Contributor Author

Should this be fixed in 1.3.2 release?

@e5l
Copy link
Member

e5l commented Mar 12, 2020

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

@e5l
Copy link
Member

e5l commented Mar 12, 2020

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

@joreilly
Copy link
Contributor Author

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.

@joreilly
Copy link
Contributor Author

@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.

@martinbonnin
Copy link
Contributor

martinbonnin commented Apr 3, 2020

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

martinbonnin added a commit to HearthSim/Arcane-Tracker that referenced this issue Apr 7, 2020
@joreilly
Copy link
Contributor Author

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

@martinbonnin
Copy link
Contributor

@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

@joreilly
Copy link
Contributor Author

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

@glootjofo
Copy link

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"

@e5l e5l modified the milestones: 1.3.2, 1.4.0 May 21, 2020
@oleg-larshin
Copy link

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

@e5l
Copy link
Member

e5l commented Sep 23, 2020

Fixed in master

@e5l e5l closed this as completed Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants