微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

初始化OkHttpClient时java.lang.StackOverflowError

如何解决初始化OkHttpClient时java.lang.StackOverflowError

我正在尝试从运行在Jetty容器(jetty-distribution-9.4.32.v20200930)中的Web应用程序初始化Okhttpclient。如果我使用OkHtp 3.X(3.14.9),一切运行正常。升级到OkHttp 4.9.0(OkHttp 4.X将实现语言从Java更改为Kotlin)后,我的应用程序无法启动,并出现以下错误

2020-10-29 01:28:44.163:WARN:oejw.WebAppContext:main: Failed startup of context o.e.j.w.WebAppContext@6581dc0a{Spidey,/jettyspideytest-1.0.0-SNAPSHOT,file:///private/tmp/jetty-0_0_0_0-8080-jettyspideytest-1_0_0-SNAPSHOT_war-_jettyspideytest-1_0_0-SNAPSHOT-any-15131256877380373917.dir/webapp/,UNAVAILABLE}{/opt/jetty-distribution-9.4.32.v20200930/webapps/jettyspideytest-1.0.0-SNAPSHOT.war}
java.lang.StackOverflowError
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.classLoader.defineClass1(Native Method)
    at java.base/java.lang.classLoader.defineClass(ClassLoader.java:1016)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/java.net.urlclassloader.defineClass(urlclassloader.java:550)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:458)
    at java.base/java.net.urlclassloader$1.run(urlclassloader.java:452)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.net.urlclassloader.findClass(urlclassloader.java:451)
    at org.eclipse.jetty.webapp.WebAppClassLoader.foundClass(WebAppClassLoader.java:650)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadAsResource(WebAppClassLoader.java:623)
    at org.eclipse.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:535)
    at java.base/java.lang.classLoader.loadClass(ClassLoader.java:521)
    at okhttp3.internal.Util.<clinit>(Util.kt:592)
    at okhttp3.internal.concurrent.TaskRunner.<clinit>(TaskRunner.kt:309)
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:41)
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:47)
    at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:471)
    at com.sitescout.dsp.adstxt.servlets.AdsTxtServlet.init(AdsTxtServlet.java:25)
    at org.eclipse.jetty.servlet.ServletHolder$Wrapper.init(ServletHolder.java:1313)
    at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:602)
    at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:414)
    at org.eclipse.jetty.servlet.ServletHandler.lambda$initialize$0(ServletHandler.java:772)
    at java.base/java.util.stream.sortedOps$SizedRefSortingSink.end(SortedOps.java:357)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
    at java.base/java.util.stream.AbstractPipeline.wrapAndcopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
    at java.base/java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:735)
    at java.base/java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:658)
    at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:796)
    at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:368)
    at org.eclipse.jetty.webapp.WebAppContext.startWebapp(WebAppContext.java:1457)
    at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1422)
    at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:911)
    at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:277)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:524)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
    at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:188)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:517)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:157)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:173)
    at org.eclipse.jetty.deploy.providers.WebAppProvider.fileAdded(WebAppProvider.java:441)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:66)
    at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:784)
    at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:753)
    at org.eclipse.jetty.util.Scanner.scan(Scanner.java:641)
    at org.eclipse.jetty.util.Scanner.doStart(Scanner.java:540)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
    at org.eclipse.jetty.deploy.providers.ScanningAppProvider.doStart(ScanningAppProvider.java:146)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
    at org.eclipse.jetty.deploy.DeploymentManager.startAppProvider(DeploymentManager.java:605)
    at org.eclipse.jetty.deploy.DeploymentManager.doStart(DeploymentManager.java:252)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
    at org.eclipse.jetty.server.Server.start(Server.java:423)
    at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
    at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
    at org.eclipse.jetty.server.Server.doStart(Server.java:387)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
    at org.eclipse.jetty.xml.XmlConfiguration.lambda$main$2(XmlConfiguration.java:1908)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at org.eclipse.jetty.xml.XmlConfiguration.main(XmlConfiguration.java:1857)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.eclipse.jetty.start.Main.invokeMain(Main.java:218)
    at org.eclipse.jetty.start.Main.start(Main.java:491)
    at org.eclipse.jetty.start.Main.main(Main.java:77)

观察

  1. 我只有在使用Jetty时才能得到它。如果我使用Tomcat 8.5,则无法运行 进入这个stackoverflow。
  2. 这只会在我打电话时发生 OkHttpClient.Builder()。如果我不使用生成器并实例化它 使用新的OkHttpClient(),我不会遇到stackoverflow。
  3. 我正在使用Xss256k而不是认的1024k。只是撞上我 stacksize为278k似乎可以解决此问题。但是,这取决于 在OS上。在Debian 9中,278k是较低的阈值。在我本地 运行macOS mojave的计算机可以用258k弥补。

我相信发生这种情况是因为码头正在加载kotlin编译的类并通过递归调用查找它们,堆栈越来越大,最终达到了256k的限制。尽管我通过提高Xss来解决它,但是我发现这种行为很奇怪。因此,感谢您的评论

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。