[2.6.3-java] Thread leak

62 views
Skip to first unread message

Sascha Thiel

unread,
Aug 23, 2017, 2:48:47 AM8/23/17
to Play Framework


Hey,

After a few reloads of a page my application crashes. I recognized that it creates tons of thread which seem not to be freed up again.

In Visual VM I got the following graph. After each reloads the remaining thread count is a little higher and also after a while it does not get freed up again.

In the thread list, it looks like this.


I, unfortunately, do not have a single clue where I should start to search for the issue. I hope you can point me into the right direction.


Thanks in advance.

Sascha

Sascha Thiel

unread,
Aug 23, 2017, 3:40:26 AM8/23/17
to Play Framework
I additionally found out, that after each reload, two more AsyncHttpClient and two more of those pool-xxx-thred-xx gets created, see picture below.


Sascha Thiel

unread,
Aug 23, 2017, 4:40:14 AM8/23/17
to Play Framework
OK, I found the issue. Thanks for listening. :-D

Koen De Groote

unread,
Aug 23, 2017, 2:24:09 PM8/23/17
to Play Framework
I'm interested in what the cause of this could be, if you wouldn't mind sharing.

I'm not a Play developer, so I would probably not have been of help looking into it, but still, it's interesting.

Sascha Thiel

unread,
Aug 24, 2017, 3:48:42 AM8/24/17
to Play Framework
Hey, 

It was a super stupid mistake on my side.
I created a proxy to serve avatars from gravatar like this.
try (final AsyncHttpClient asyncHttpClient = new DefaultAsyncHttpClient()) {
final BoundRequestBuilder requestBuilder = asyncHttpClient.prepareGet(avatarUrl);
final Future<Response> featureResponse = requestBuilder.execute();
final Response response = featureResponse.get();
final InputStream responseBodyAsStream = response.getResponseBodyAsStream();

return ok(responseBodyAsStream).as("image/jpg");
} catch (Exception e) {
return notFound();
}

And in the previous version I forgot to close the AsyncHttpClient. So whenever an avatar was loaded, two threads kept running. The explanation to you helped me finding the bug. :-D

Bests,
Sascha

Koen De Groote

unread,
Aug 24, 2017, 2:21:51 PM8/24/17
to Play Framework
Thanks for elaborating.

It's strange though, I would expect the ok() method to close off all threads that got started when the route was called.

Will Sargent

unread,
Aug 24, 2017, 10:29:21 PM8/24/17
to Play Framework
You're supposed to inject a WSClient, and let Play handle the thread management under normal circumstances.


"Note: If you create a WSClient manually then you must call client.close() to clean it up when you’ve finished with it. Each client creates its own thread pool. If you fail to close the client or if you create too many clients then you will run out of threads or file handles -— you’ll get errors like “Unable to create new native thread” or “too many open files” as the underlying resources are consumed."
Reply all
Reply to author
Forward
0 new messages