java: Use of "async" calls eventually causes StackoverflowErrors due to unbounded growth of networkInterceptors in OkHttpClient
I mentioned this in #467 but I thought it was worth opening a new issue for visibility.
Using the generated “async” methods, such as CoreV1Api.listPodForAllNamespacesAsync(..), with an ApiCallback will eventually cause a StackoverflowError because the generated client code adds a new networkInteceptor onto the ApiClient’s list of networkInterceptors for every single call and never cleans up the list.
For example, listPodForAllNamespacesAsync creates a ProgressListener when callback != null. When the call stack reaches listPodForAllNamespacesCall, this series of generated code does:
if(progressListener != null) {
apiClient.getHttpClient().networkInterceptors().add(new com.squareup.okhttp.Interceptor() {
...
});
}
networkInterceptors() is a plain java.util.List which grows unbounded in size. The “interceptor chain” concept in OkHttpClient recurses through all of the interceptors in the list, and if the list of interceptors becomes long enough it will eventually cause a StackOverflowError:
Uncaught exception in thread Thread[OkHttp Dispatcher,5,main]
java.lang.StackOverflowError: null
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
at io.kubernetes.client.apis.CoreV1Api$581.intercept(CoreV1Api.java:18426)
at com.squareup.okhttp.internal.http.HttpEngine$NetworkInterceptorChain.proceed(HttpEngine.java:695)
...
The upstream issue for the swagger-codegen is https://github.com/swagger-api/swagger-codegen/issues/7876 and a fix is proposed in https://github.com/swagger-api/swagger-codegen/pull/8053 but has received no attention for 8+ months.
As-is (along with the thread safety issues in #467), the “async” methods in the generated client code here are virtually unusable in any sort of long-lived application or production server.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 18 (5 by maintainers)
Commits related to this issue
- bugfix for StackoverflowError: remove progressListener implementation. see https://github.com/kubernetes-client/java/issues/482 — committed to ifcore/selling-partner-api-models by felixebert a year ago
Oops, the commit from #778 is in
io.kubernetes:client-java:7.0.0, I missed that there were versions newer than5.0.0. Looks like the problematic behavior of adding new networkInterceptors onto the httpclient on each request no longer appears there 👍For the fork, I believe @brendandburns is referring to https://github.com/OpenAPITools/openapi-generator (please refer to Q&A for the reasons behind the fork)
We’ve made a couple of enhancements based on @brendandburns feedback:
We definitely welcome PRs to further improve the Java client generated by OpenAPI Generator.