question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Opening a client websocket session freezes after many failed attempts

See original GitHub issue

Ktor Version

1.1.2

Ktor Engine Used(client or server and name)

CIO client

JVM Version, Operating System and Relevant Context

jvm 1.8, macOs

Feedback

For my android app, I am trying to make the websocket client reconnect after failure, in case the user has no/bad internet connection. So if calling HttpClient.wss or HttpClient.ws fails I catch the error, delay for a short time and try again… But, the problem is after a certain amount of reconnect attempts (usually between 5 and 30 attempts) calling HttpClient.wss or HttpClient.ws causes the program to stall (no errors are thrown).

I think it is related to ktor improperly closing the websocket session after calling HttpClient.wss or HttpClient.ws fails. I’ve think I have simplified the issue as much as I can. For example by forcing an error by using a bad websocket host address:

fun main() = runBlocking {
    for (i in 0 .. 100) {
        GlobalScope.launch(Dispatchers.Default) {
            connect(i)
        }.join()
    }
    return@runBlocking
}

suspend fun connect(i: Int){
    val client = HttpClient(CIO).config {
        install(WebSockets)
    }
    try {
        client.wss(host = "test.badaddress.org") {
            send(Frame.Text("Hello World"))

            for (message in incoming.map { it as? Frame.Text }.filterNotNull()) {
                println(message.readText())
                break
            }
        }
    } catch (t: Throwable){
    }
    println(i)
    client.close()
}

produces

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

but if you comment out: client.close() in the above program the full program runs producing numbers 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

Additionally, modifying the above program to use a proper websocket host address… like replacing host = "test.badaddress.org" with host = "echo.websocket.org" the program runs as expected producing

Hello World
0
Hello World
1
Hello World
2
Hello World
...
97
Hello World
98
Hello World
99
Hello World
100

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
luca992commented, Feb 18, 2019

Hi @luca992, it looks like the WebSocket freeze is caused by map:

  1. The map wait for WebSocket close
  2. The client closes the connection after exiting from the client block.

Could you try manually close the connection?

@e5l Well, I do not think map is the issue. As that block of code isn’t even called when using a bad address. host = "test.badaddress.org" … To make sure I commented out

/*send(Frame.Text("Hello World"))
for (message in incoming.map { it as? Frame.Text }.filterNotNull()) {
    println(message.readText())
    break
}*/
        

and get the same result

And sorry, what do you mean by manually closing the connection? How would I do that?

1reaction
aventlycommented, Feb 16, 2019

@e5l I don’t know it is the same issue or not (maybe I should write to Coroutines creators). Please, take a look. This code:

while(true) {
        Thread.sleep(1000)
        CoroutineScope(Dispatchers.Default).launch {
            println("started")
            delay(100)
            println("delay ended")
            HttpClient(CIO).close()
            println("created and closed")
        }
    }
    thread {
        Thread.sleep(10000000)
    }

prints:

started
delay ended
created and closed
started
delay ended
created and closed
started
delay ended
created and closed
started
delay ended
created and closed

And that’s it. Then nothing will be printed. Just four iterations and application hangs (number of CPU threads == 4). If you will remove .close() then evething will be fine (except memory leak from engine) and will work until death. Is it related to Ktor or to Coroutines?

Read more comments on GitHub >

github_iconTop Results From Across the Web

websocket closing connection automatically - Stack Overflow
I'm building an application in java that has an embedded websocket server based on jetty. The client is the default websocket implementation in ......
Read more >
Troubleshooting connection issues | Socket.IO
You are trying to reach a plain WebSocket server; The server is not reachable; The client is not compatible with the version of...
Read more >
Troubleshooting connectivity issues between the agent, client ...
The most common reason for the error is that the client or proxy server is unable to resolve the DNS name of the...
Read more >
Vaadin 8 Push issues - Martin Vysny
When the client freezes randomly, it could be that the connection gets broken by proxy, firewall or load balancer. Read below for solutions...
Read more >
WebSocket.close() - Web APIs - MDN Web Docs
The WebSocket.close() method closes the WebSocket connection or connection attempt, if any. If the connection is already CLOSED, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found