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

HttpClient(OkHttp) throw: java.net.SocketTimeoutException: timeout #1425

Closed
merli-dev opened this issue Nov 6, 2019 · 1 comment
Closed
Labels

Comments

@merli-dev
Copy link

Ktor Version and Engine Used (client or server and name)
io.ktor:ktor-client-okhttp:1.3.0-beta-1

Other modules included in 'buil.gradle.kts':

 sourceSets["commonMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.13.0")
        implementation("io.ktor:ktor-client-core:$ktor")
        implementation("io.ktor:ktor-client-json-native:$ktor")
        implementation("io.ktor:ktor-client-json:$ktor")
        implementation("io.ktor:ktor-client-serialization:$ktor")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2")
        implementation("androidx.lifecycle:lifecycle-extensions:2.1.0")
    }

    sourceSets["androidMain"].dependencies {
        implementation("org.jetbrains.kotlin:kotlin-stdlib")
        implementation(kotlin("stdlib-jdk8"))
        implementation("io.ktor:ktor-client-okhttp:$ktor")
        implementation("com.squareup.okhttp3:okhttp:4.2.2")
        //implementation("io.ktor:ktor-client-cio:$ktor")
        implementation("io.ktor:ktor-client-serialization-jvm:$ktor")
        implementation("io.ktor:ktor-client-gson:$ktor")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.13.0")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2")
    }

    sourceSets["iosMain"].dependencies {
        implementation("io.ktor:ktor-client-ios:$ktor")
        implementation("io.ktor:ktor-client-serialization-native:$ktor")
        implementation("io.ktor:ktor-client-gson:$ktor")
        implementation("org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.13.0")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core-native:1.3.2")
    }

Describe the bug
I'm try to use ktor client to call GET request with body. Apparently all it's good configured for Android/IOS/Common but when the code calls get request appear error of client: 'java.net.SocketTimeoutException: timeout'

LogCat
This is the logcat from Android Studio:

E/AndroidRuntime: FATAL EXCEPTION: main
    java.net.SocketTimeoutException: timeout
        at okio.SocketAsyncTimeout.newTimeoutException(Okio.kt:159)
        at okio.AsyncTimeout.exit$jvm(AsyncTimeout.kt:203)
        at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:163)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:349)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:222)
        at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.kt:210)
        at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:181)
        at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:105)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:82)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.net.SocketException: socket is closed
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:584)
        at okio.InputStreamSource.read(Okio.kt:102)
        at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:349) 
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:222) 
        at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.kt:210) 
        at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:181) 
        at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:105) 
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:82) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184) 
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 
2019-11-06 16:18:16.147 25406-25406/habitatprojects.hubbuildings.dev E/AndroidRuntime: FATAL EXCEPTION: main
    Process: habitatprojects.hubbuildings.dev, PID: 25406
    java.net.SocketTimeoutException: timeout
        at okio.SocketAsyncTimeout.newTimeoutException(Okio.kt:159)
        at okio.AsyncTimeout.exit$jvm(AsyncTimeout.kt:203)
        at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:163)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:349)
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:222)
        at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.kt:210)
        at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:181)
        at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:105)
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:82)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112)
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87)
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184)
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:764)
     Caused by: java.net.SocketException: socket is closed
        at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:584)
        at okio.InputStreamSource.read(Okio.kt:102)
        at okio.AsyncTimeout$source$1.read(AsyncTimeout.kt:159)
        at okio.RealBufferedSource.indexOf(RealBufferedSource.kt:349) 
        at okio.RealBufferedSource.readUtf8LineStrict(RealBufferedSource.kt:222) 
        at okhttp3.internal.http1.Http1ExchangeCodec.readHeaderLine(Http1ExchangeCodec.kt:210) 
        at okhttp3.internal.http1.Http1ExchangeCodec.readResponseHeaders(Http1ExchangeCodec.kt:181) 
        at okhttp3.internal.connection.Exchange.readResponseHeaders(Exchange.kt:105) 
        at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.kt:82) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:37) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) 
        at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) 
        at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184) 
        at okhttp3.RealCall$AsyncCall.run(RealCall.kt:136) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) 
        at java.lang.Thread.run(Thread.java:764) 

To Reproduce
This is my code that calls HttpClient:

  • LoginRequest.kt:
import Tools.Log
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.headers
import io.ktor.client.request.url
import io.ktor.client.response.HttpResponse
import io.ktor.client.response.readText
import io.ktor.http.HttpHeaders
import kotlinx.coroutines.*
import kotlinx.serialization.Serializable

internal expect val Main: CoroutineDispatcher
internal expect val Background: CoroutineDispatcher
internal expect val client: HttpClient

abstract class LoginRequest {

    fun start(url: String){
        GlobalScope.apply {
            try {
                launch(Main) {
                    Log.d("debug", "start()")
                    val response = doLogin(url)
                    val status = response.status
                    val json = response.readText()

                    Log.d("debug", "start() - Response: $status")
                    Log.d("debug", "start() - Json: $json")

                    client.close()
                }
            }
            catch (ex: Exception){
                Log.e("error", "start($url)", ex)
            }
        }
    }

    @Serializable
    data class dataClass(val user_name: String, val user_passwd: String)

    suspend fun doLogin(adress: String): HttpResponse {
        return client.get<HttpResponse>() {
            url(adress)
            headers {
                append(HttpHeaders.ContentType, "application/json")
            }
            body = dataClass("a","a")
        }
    }

    abstract fun setBody(): Any
}

  • HttpClientCore.kt (Android):
import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.features.json.GsonSerializer
import io.ktor.client.features.json.JsonFeature
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers

internal actual val Main: CoroutineDispatcher = Dispatchers.Main
internal actual val Background: CoroutineDispatcher = Dispatchers.Default
internal actual val client = HttpClient(OkHttp) {
    install(JsonFeature){
        serializer = GsonSerializer{
            // Configurable .GsonBuilder
            serializeNulls()
            disableHtmlEscaping()
        }
    }
}
  • HttpClientCore.kt (IOS):
import io.ktor.client.HttpClient
import io.ktor.client.engine.ios.Ios
import io.ktor.client.features.json.JsonFeature
import io.ktor.client.features.json.serializer.KotlinxSerializer
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Runnable
import platform.darwin.dispatch_async
import platform.darwin.dispatch_get_main_queue
import platform.darwin.dispatch_queue_t
import kotlin.coroutines.CoroutineContext

internal actual val Main: CoroutineDispatcher = NsQueueDispatcher(dispatch_get_main_queue())
internal actual val Background: CoroutineDispatcher = Main
internal class NsQueueDispatcher(
        private val dispatchQueue: dispatch_queue_t) : CoroutineDispatcher() {
    override fun dispatch(context: CoroutineContext, block: Runnable) {
        dispatch_async(dispatchQueue) {
            block.run()
        }
    }
}

internal actual val client = HttpClient(Ios){
    install(JsonFeature){
        serializer = KotlinxSerializer()
    }
}

Expected behavior
Client never returns response of server

@merli-dev merli-dev added the bug label Nov 6, 2019
@haneev
Copy link
Contributor

haneev commented Nov 7, 2019

Might be duplicate of #1263

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

2 participants