{"record":{"id":"a66f5ce4ed5cc82c","repo":"spring-projects/spring-ai","slug":"both-or-none-of-maxidleconnections-and-keepaliv-a66f5c","errorCode":null,"errorMessage":"Both or none of `maxIdleConnections` and `keepAliveDuration` must be set, but only one was set","messagePattern":"Both or none of `maxIdleConnections` and `keepAliveDuration` must be set, but only one was set","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"models/spring-ai-openai/src/main/java/org/springframework/ai/openai/http/okhttp/SpringAiOpenAiHttpClient.java","lineNumber":575,"sourceCode":"\t\t\t\t\t\t\ttoHttpResponse(response));\n\t\t\t\t\treturn authed.map(req -> toRequestComputeUrl(req, null)).orElse(null);\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tExecutorService userDispatcherExecutor = this.dispatcherExecutorService;\n\t\t\tboolean ownsDispatcherExecutor = userDispatcherExecutor == null;\n\t\t\tExecutorService dispatcherBase = (userDispatcherExecutor != null) ? userDispatcherExecutor\n\t\t\t\t\t: defaultDispatcherExecutor();\n\t\t\tExecutorService dispatcherExecutor = ContextExecutorService.wrap(dispatcherBase,\n\t\t\t\t\tContextSnapshotFactory.builder().build());\n\t\t\tokBuilder.dispatcher(new Dispatcher(dispatcherExecutor));\n\n\t\t\tif (this.maxIdleConnections != null && this.keepAliveDuration != null) {\n\t\t\t\tokBuilder.connectionPool(new ConnectionPool(this.maxIdleConnections, this.keepAliveDuration.toNanos(),\n\t\t\t\t\t\tTimeUnit.NANOSECONDS));\n\t\t\t}\n\t\t\telse if ((this.maxIdleConnections == null) != (this.keepAliveDuration == null)) {\n\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\t\"Both or none of `maxIdleConnections` and `keepAliveDuration` must be set, but only one was set\");\n\t\t\t}\n\n\t\t\tif (this.sslSocketFactory != null && this.trustManager != null) {\n\t\t\t\tokBuilder.sslSocketFactory(this.sslSocketFactory, this.trustManager);\n\t\t\t}\n\t\t\telse if ((this.sslSocketFactory == null) != (this.trustManager == null)) {\n\t\t\t\tthrow new IllegalStateException(\n\t\t\t\t\t\t\"Both or none of `sslSocketFactory` and `trustManager` must be set, but only one was set\");\n\t\t\t}\n\n\t\t\tif (this.hostnameVerifier != null) {\n\t\t\t\tokBuilder.hostnameVerifier(this.hostnameVerifier);\n\t\t\t}\n\n\t\t\tOkHttpClient okClient = okBuilder.build();\n\t\t\t// Same-host traffic: raise per-host limit to overall request limit. Matches\n\t\t\t// the SDK's tuning at the bottom of `OkHttpClient.Builder.build()`.","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/spring-projects/spring-ai/blob/98a7beda4f29d80a71c5837eb4053b03a93a46f7/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/http/okhttp/SpringAiOpenAiHttpClient.java#L557-L593","documentation":"The SpringAiOpenAiHttpClient builder treats maxIdleConnections and keepAliveDuration as a pair: both or neither must be set to configure a custom connection pool. Setting only one is an invalid builder state and build() throws IllegalStateException.","triggerScenarios":"Calling SpringAiOpenAiHttpClient.builder().maxIdleConnections(5).build() without keepAliveDuration (or the reverse), then invoking build() at SpringAiOpenAiHttpClient.java:575.","commonSituations":"Copy-pasted tuning snippets that set only the pool size, config binding that populates one property but not its companion, or migration from a client that accepted a lone pool-size setting.","solutions":["Set both values together: maxIdleConnections(n).keepAliveDuration(Duration.ofMinutes(5)).","Remove both settings to fall back to OkHttp's default connection pool.","Add a startup self-check that builds the client once and fails fast with a clear message."],"exampleFix":"// before\nSpringAiOpenAiHttpClient.builder().maxIdleConnections(10).build();\n// after\nSpringAiOpenAiHttpClient.builder()\n    .maxIdleConnections(10)\n    .keepAliveDuration(Duration.ofMinutes(5))\n    .build();","handlingStrategy":"validation","validationCode":"if ((maxIdleConnections == null) != (keepAliveDuration == null)) {\n    throw new IllegalStateException(\"Set both maxIdleConnections and keepAliveDuration, or neither\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    client = SpringAiOpenAiHttpClient.builder()\n        .maxIdleConnections(cfg.idle())\n        .keepAliveDuration(cfg.keepAlive())\n        .build();\n} catch (IllegalStateException e) {\n    throw new ClientConfigurationException(\"Invalid pool configuration: \" + e.getMessage(), e);\n}","preventionTips":["Always set pool options as a pair in config code","Bind related properties together in your configuration class","Build the client at startup to fail fast"],"tags":["configuration","builder","connection-pool"],"backgroundTag":"conflicting-config-options","analyzedSha":"98a7beda4f29d80a71c5837eb4053b03a93a46f7","analyzedAt":"2026-09-11T14:15:49.441Z","contentChangedAt":"2026-09-11T14:15:49.441Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}