Repository navigation
Closed
Description
I am currently sharing the Java 8 HTTP client to create both the WebSocket client and the HTTP client. I make a single request with the HTTP client followed by connecting to a WebSocket and sending messages periodically. Eventually (always around 400 seconds later), everything hangs and CPU usage is locked at 100%.
This doesn't occur if:
- I create a separate client and WebSocket client using
.simple[IO] - I don't make the initial request with the client
- I request other URLs with the client
Since it doesn't happen with every URL, I'm assuming it has something to do with the endpoint I'm hitting. It gives the following response:
Response(
status=200,
headers=Headers(
cf-cache-status: DYNAMIC,
cf-ray: 57d4a1eb4eaaa062-SLC,
connection: keep-alive,
content-type: application/json,
date: Wed, 01 Apr 2020 19:14:20 GMT,
expect-ct: max-age=604800,
report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct",
server: cloudflare,
set-cookie: <REDACTED>,
set-cookie: <REDACTED>,
strict-transport-security: max-age=31536000; includeSubDomains,
transfer-encoding: chunked,
via: 1.1 google,
x-envoy-upstream-service-time: 26,
x-ratelimit-bucket: 41f9cd5d28af77da04563bcb1d67fdfd,
x-ratelimit-limit: 2,
x-ratelimit-remaining: 1,
x-ratelimit-reset: 1585768466,
x-ratelimit-reset-after: 5
)
)
{
"url": "wss://gateway.discord.gg",
"shards": 1,
"session_start_limit": {
"total": 1000,
"remaining": 992,
"reset_after": 23572680
}
}
I tried to put together a minimized example of what I'm seeing here:
import cats.effect._
import cats.implicits._
import fs2.Stream
import java.net.http.HttpClient
import org.http4s.client.jdkhttpclient._
import org.http4s.client.jdkhttpclient.WSFrame._
import org.http4s._
import org.http4s.headers._
import org.http4s.implicits._
import scala.concurrent.duration._
object Main extends IOApp {
override def run(args: List[String]): IO[ExitCode] = {
IO(HttpClient.newHttpClient).map(client => (JdkHttpClient[IO](client), JdkWSClient[IO](client))).flatMap {
case (client, wsClient) =>
client.expect[String](Request[IO](uri = uri"https://discordapp.com/api/gateway/bot", headers = Headers.of(headers))) >> connection(wsClient)
.compile
.drain
.as(ExitCode.Success)
}
}
// Removed since it's private info
val token: String = ???
val headers =
Authorization(Credentials.Token("Bot".ci, token))
def connection(wsClient: WSClient[IO]) =
Stream
.resource(wsClient.connectHighLevel(WSRequest(uri"wss://echo.websocket.org")))
.flatMap { connection =>
printResponses(connection).concurrently(
Stream
.awakeEvery[IO](1.seconds)
.evalMap(time => connection.send(Text(s"${time.toSeconds} seconds")))
)
}
def printResponses(connection: WSConnectionHighLevel[IO]): Stream[IO, Unit] = {
connection.receiveStream
.collect {
case Text(data, _) => data
}
.evalMap(s => IO(println(s)))
}
}This prints out:
1 seconds
2 seconds
3 seconds
4 seconds
5 seconds
6 seconds
7 seconds
8 seconds
...
393 seconds
394 seconds
395 seconds
396 seconds
397 seconds
And then it hangs and CPU usage is stuck at 100%
Metadata
Metadata
Assignees
Labels
No labels
Activity
Billzabob commentedon Apr 1, 2020
I was able to narrow this down even further. It seems to just be related to the client. The following code also hangs and pegs CPU at 100% after about 460 seconds:
[-]Deadlock occurs when sharing JDK client `JdkHttpClient` and `JdkWSClient`[/-][+]Deadlock eventually occurs in `JdkHttpClient` for certain requests[/+]ChristopherDavenport commentedon Apr 1, 2020
Since this is associated in some way with the JDK, which JDK is being used?
Billzabob commentedon Apr 1, 2020
@ChristopherDavenport I'm using JDK 11
Specifically:
Billzabob commentedon Apr 1, 2020
Any idea what it could be about this particular response that causes this? I tried testing this against a few other URLs and wasn't able to reproduce the issue.
amesgen commentedon Apr 1, 2020
How do I create such a token? I created a discord "application", added a bot and got a token, but I get 401 unauthorized.
amesgen commentedon Apr 1, 2020
I can confirm that increased cpu usage does not happen on e.g.
https://discordapp.com(noapi/gateway/bot).Can you test if this happens with #191 ? It is the only thing I can think of right now.
Billzabob commentedon Apr 1, 2020
I'm not sure why you're getting a 401. It's been a while since I created my bot but I think that's all I did. I just ran it using
java.net.http.HttpClientdirectly and it never froze. I also am running it with #191 right now and so far it's made it further than before so maybe that's the problem. I'll leave it running and let you know if it ever hangs 👍Billzabob commentedon Apr 1, 2020
It's hung again :( After running for 770 seconds it stopped and once again I'm seeing 100% CPU usage.
amesgen commentedon Apr 1, 2020
Ah, I thought I had to change
"Bot".ci, now it works. Now staring at CPU usage...amesgen commentedon Apr 1, 2020
It has been running for 1300 seconds and I noticed nothing suspicious about CPU or memory usage. I am on master, not on #191.
My Java version:
Can you test it with this version?
amesgen commentedon Apr 1, 2020
Maybe JDK-8221395 is the reason?
Billzabob commentedon Apr 1, 2020
I was hopeful. I tried it with that JDK but it's still freezing up for me :( This time it did at 430 seconds.
amesgen commentedon Apr 1, 2020
Can you turn on logging?
amesgen commentedon Apr 2, 2020
Uh, now it froze for me, after 4010 seconds (accidentally cancelled the first run after ~1800 seconds).
19 remaining items
amesgen commentedon Apr 3, 2020
There really is not much code left in comparison to the (non-freezing) plain Java version, I will try to create a minimal example tomorrow.
amesgen commentedon Apr 3, 2020
Ok, "good" news: Both the freeze and the
too many concurrent streamsissue are not http4s-related.Freezes
Code (HTTP 1.1)
Also without fs2, only cats-effect as a dependency:
Code without fs2 (HTTP 1.1)
If I use HTTP 2 instead of HTTP 1.1 in the second example, I can reproduce the
too many concurrent streamsissue,so this issue is definitely unrelated to #207sry, I am forgetting what I was testing. Actually, I also changed the body handler fromBodyHandlers.ofStringtoBodyHandlers.ofPublisherin order to not close the connections properly, so it is actually expected to reproduce thetoo many concurrent streamsissue... Sry for the confusion.Code without fs2 (HTTP 2 and intended connection leakage)
In addition to the synchronous "plain Java" version above, here also a version with
sendAsync.Code without fs2 (HTTP 2)
Both HTTP 1.1 and HTTP 2 work fine, without any freezes.
amesgen commentedon Apr 3, 2020
This GH issue is already pretty big, and we have two (probably unrelated subissues), and none of them are related to http4s or http4s-jdk-http-client directly. Do we want to move the discussion elsewhere?
amesgen commentedon Apr 3, 2020
Ha, I just got a freeze with this "plain Java" code:
Code
Now this seems to indicate that the freeze issue is indeed a Java 11 issue.
@Billzabob Can you confirm this?
Billzabob commentedon Apr 3, 2020
Yep! That froze for me too after 1280 seconds.
Billzabob commentedon Apr 4, 2020
I tried modifying your example and changing
BodyHandlers.ofPublisher()toBodyHandlers.discarding()and now it's been going for 2850 seconds without hanging. Think it could have something to do with that? I'll leave it running to make sure. I know nothing aboutjava.util.concurrent.Flowthough.amesgen commentedon Apr 4, 2020
Yes, that is still confusing me:
BodyPublishers.ofPublisherlike in the "intended connection leakage" example from here, it results in atoo many concurrent streamsexception. This seems very reasonable.BodyPublishers.ofPublisherwith the plain Java version, we get a freeze (I confirmed that it still is the SSL issue). This is weird, I would expect antoo many concurrent streamsexception.But when using an "eager"
BodyPublisherlikediscardingorofString, the cats-effect version does freeze (see here), although plain Java seems to work fine.Billzabob commentedon Apr 4, 2020
Wow. That is extremely confusing. This type of stuff is such a pain to track down. Especially since it takes minutes or hours to determine if it's going to freeze or not.
lhns commentedon Aug 9, 2020
I think I'm also having this issue with my simple one-file http-retry-proxy microservice:
https://github.com/LolHens/http-wait/blob/cd87c443e76812dbecaeae9b7c51f5004f6aa181/src/main/scala/de/lolhens/httpwait/Main.scala
I am not using websockets, just simple http requests. I run this as a graal native-image in a docker container.
This runs fine sometimes for a few days and sometimes for weeks but now and then it just locks up and I also get 100% cpu on the container.
Maybe this info helps to narrow it down. For now I will try switching to another client backend.
amesgen commentedon Aug 9, 2020
Thanks for reminding me of this issue @LolHens
Note that this issue is not caused by http4s-jdk-http-client, but rather by some weird interaction of cats-effect and TLSv1.3 stuff (maybe cats-effect only increases the likelihood of this TLS bug to occur). src
There two easy workarounds (also see our docs):
lhns commentedon Aug 9, 2020
Thanks for the quick info! I will try disabling TLSv1.3 then.
workaround for http4s#200
workaround for http4s#200
workaround for #200 (#260)