Hi,
I found that using WebClient for specific websites is causing increasing number of live threads which "hangs" in memory.
You can reproduce this bug for next website: http://lmktec.co.uk
I attached thread chart from visualvm before using webclient, after using webclient for geting one page from that website and after getting 20 different pages from that website.
Before there were 62 live threads, after getting one page it were 70 live threads, after 20 - more than 300 hundred, after 100 pages - more than 1 thousend.
The threads stay in memory forever, and can be killed only after tomcat restart.
Here are WebClient options that I an using:
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getCookieManager().setCookiesEnabled(true);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setTimeout(PAGE_LOAD_TIMEOUT); // 20 sec
webClient.setJavaScriptTimeout(JAVASCRIPT_TIMEOUT); // 10 sec
webClient.getOptions().setRedirectEnabled(true);
webClient.getOptions().setCssEnabled(false);
I am "cleaning resources" using next code:
webClient.getJavaScriptEngine().shutdown();
webClient.close();
webClient.getCache().clear();
Note: this bug is not reproducible if you will turn off the javascript.
I assume that this bug is reproducible for websites which are using websockets. Maybe WebClient opens websocket connections and didn't close them, but just listening to channel?
I've also encountered this leak on 2.18. It's quite easy to reproduce in fact, just run WebSocketTest from the testsuite, it will leak many threads for each browser that supports websockets. The problem is that the WebSocket class does WebSocketClient.connect, which ends up creating a QueueThreadPool. This threadpool will not be closed until application shutdown.
Here's an example stack trace of the threads that are generated and never die (I end up with thousands of them):
I tried to address it by calling client.stop(); from onWebSocketClose() but apprently this is not always called.
Diff:
Thanks for reporting, fixed in SVN.
You can get latest snapshot or build [1] after it succeeds.
[1] https://ci.canoo.com/teamcity/viewLog.html?buildTypeId=HtmlUnit_FastBuild&buildId=lastSuccessful&tab=artifacts
Sorry but it's still happenning on 2.19, maybe more than before. It happens with http://www.hirschdruck.de/ for example. Is there by any chance an easy way to disable websocket support without shutting down the whole javascript engine while these leaks are fixed?
Last edit: Marc 2016-01-07
Currently, the WebSocket are closed, when the HtmlPage.cleanUp() is called.
Hi,
Unfortunately I cannot get any more thread dumps from this issue because I'm currently using a browser in which websockets are disabled. I tried to set up a reproductible test case with websockets and i wasn't able to reproduce the leak. So feel free to close this issue for the time being.
Great, please advise if you face any further issue.