Skip to content

[Bug]: KubernetesSandboxClient leaks a new fabric8 KubernetesClient on every create/resume #4267

Open

Description

@iluv7

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 KubernetesSandboxClient should build at most one internally owned fabric8 KubernetesClient and reuse it for all create/resume calls.
  • A caller-supplied KubernetesClient (or one built from a caller-supplied Config) should remain caller-owned and never be closed by the sandbox client.
  • The lightweight SDK SandboxClient wrapper may still be created per call; the heavyweight KubernetesClient must 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/main c1d482900)
  • 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 client
  • KubernetesSandboxClient.java:322 — resolveClient() builds a new fabric8 client when none is supplied
  • KubernetesSandbox.java:65 — shutdown() only closes the per-sandbox connection; it never closes the fabric8 client
  • SandboxClient (SDK) constructor — when no client is passed, ownsClient = true; close() is the only place that closes the underlying KubernetesClient

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 SandboxClient wrapper per call, or make its map thread-safe before sharing it.

Proposed fix

Minimal:

  • Add a lazily initialized KubernetesClient field to KubernetesSandboxClient, reused by resolveClient() for the default path.
  • Return a caller-supplied KubernetesClient unchanged and do not close it.
  • For a per-call kubernetesConfig that 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 KubernetesSandboxClient AutoCloseable and close the internally owned client once at application/agent shutdown. If no close hook is added, the cached client lives for the lifetime of the KubernetesSandboxClient instead of leaking one per call.

Regression tests:

  • Two resolveClient()/create() calls with the default options return/build the same KubernetesClient instance.
  • A supplied KubernetesClient is returned as-is and not closed.
  • A per-call kubernetesConfig override does not reuse a client built from a different config.

Related

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions