Repository navigation
Description
Describe the bug
KubernetesSandboxClient builds a new fabric8 KubernetesClient for every create() and resume() call, and never closes it.
buildSdkClient() calls resolveClient(), which falls back to new KubernetesClientBuilder().build() whenever the options do not carry an explicit kubernetesClient or kubernetesConfig. The resulting SDK SandboxClient is a local variable; its close() is never called, so the fabric8 client it owns is never closed either.
This leaks one fabric8 client per sandbox acquisition: HTTP connection pool, executor threads, watch resources and file descriptors. It is especially visible for long-running agents because every call after the first goes through resume() and therefore builds another client. The leak exists independently of the claim lifecycle fix in #4251, but keeping claims alive across calls makes the per-call resume() path more common.
To Reproduce
Use the default Kubernetes client options (no injected KubernetesClient / kubernetesConfig), and acquire sandboxes repeatedly:
KubernetesFilesystemSpec spec = new KubernetesFilesystemSpec()
.namespace("default")
.warmPoolName("my-pool");
KubernetesSandboxClient client =
(KubernetesSandboxClient) spec.toSandboxContext().getClient();
for (int i = 0; i < 100; i++) {
Sandbox sandbox = client.resume(state); // or client.create(spec, null, null)
sandbox.shutdown();
}Each create/resume calls buildSdkClient() -> resolveClient() -> new KubernetesClientBuilder().build(). Observe the JVM thread/FD count growing with the number of calls, or mock KubernetesClientBuilder and count build() invocations.
Expected behavior
- A
KubernetesSandboxClientshould build at most one internally owned fabric8KubernetesClientand reuse it for allcreate/resumecalls. - A caller-supplied
KubernetesClient(or one built from a caller-suppliedConfig) should remain caller-owned and never be closed by the sandbox client. - The lightweight SDK
SandboxClientwrapper may still be created per call; the heavyweightKubernetesClientmust not. - If the client owns the fabric8 client, it should have a way to close it once at application/agent shutdown, not on every per-call
shutdown().
Error messages
No exception is thrown; the symptom is resource growth (threads, connections, FDs) and eventual pressure on the process. A thread dump typically shows repeated fabric8/OkHttp executor and connection-pool resources.
Environment
- AgentScope-Java Version:
2.1.0-SNAPSHOT(upstream/mainc1d482900) - Java Version: 17
- OS: macOS / Linux
- Kubernetes: agent-sandbox
extensions.agents.x-k8s.io/v1beta1 - fabric8 Kubernetes Client:
7.8.0
Additional context
Current logic
// KubernetesSandboxClient.create() / resume()
SandboxClient sdkClient = buildSdkClient(merged);private SandboxClient buildSdkClient(KubernetesSandboxClientOptions opts) {
KubernetesClient kc = resolveClient(opts);
SandboxConnectionConfig connectionConfig = toConnectionConfig(opts);
return new SandboxClient(connectionConfig, kc, ...);
}private static KubernetesClient resolveClient(KubernetesSandboxClientOptions merged) {
if (merged.getKubernetesClient() != null) {
return merged.getKubernetesClient();
}
if (merged.getKubernetesConfig() != null) {
return new KubernetesClientBuilder().withConfig(merged.getKubernetesConfig()).build();
}
return new KubernetesClientBuilder().build();
}Relevant locations:
KubernetesSandboxClient.java:211—buildSdkClient()creates the SDK wrapper and resolves a clientKubernetesSandboxClient.java:322—resolveClient()builds a new fabric8 client when none is suppliedKubernetesSandbox.java:65—shutdown()only closes the per-sandbox connection; it never closes the fabric8 clientSandboxClient(SDK) constructor — when no client is passed,ownsClient = true;close()is the only place that closes the underlyingKubernetesClient
Why this is not fixed by simply sharing the SDK SandboxClient
The SDK SandboxClient wrapper contains a mutable LinkedHashMap<String, Sandbox> activeSandboxes and is not thread-safe. Sharing that wrapper across concurrent sessions would introduce map races. The right split is:
- share the fabric8
KubernetesClient(thread-safe, heavyweight); - keep the SDK
SandboxClientwrapper per call, or make its map thread-safe before sharing it.
Proposed fix
Minimal:
- Add a lazily initialized
KubernetesClientfield toKubernetesSandboxClient, reused byresolveClient()for the default path. - Return a caller-supplied
KubernetesClientunchanged and do not close it. - For a per-call
kubernetesConfigthat differs from the client template, either build a separate client for that call or cache by config; do not silently reuse a client built from a different config. - Do not close the shared client from
KubernetesSandbox.shutdown(); per-call shutdown must only close the per-sandbox connection. - Optionally make
KubernetesSandboxClientAutoCloseableand close the internally owned client once at application/agent shutdown. If no close hook is added, the cached client lives for the lifetime of theKubernetesSandboxClientinstead of leaking one per call.
Regression tests:
- Two
resolveClient()/create()calls with the default options return/build the sameKubernetesClientinstance. - A supplied
KubernetesClientis returned as-is and not closed. - A per-call
kubernetesConfigoverride does not reuse a client built from a different config.
Related
- [Bug]: Kubernetes sandbox deletes its SandboxClaim on every call, breaking resume and adding a 30s stall #4251 — claim lifecycle / resume bug (fixed by fix(sandbox-kubernetes): keep claim alive across calls so resume works #4252); this issue is about the fabric8 client lifetime, not the claim.
- [Bug]: KubernetesSandboxClient ignores per-call timeout overrides #4264 — per-call timeout options are not merged; same
KubernetesSandboxClientOptionsarea, but a separate defect.
Activity