Skip to content

Race: get_or_create_client can create duplicate clients for same context #77

Description

@ndenev

Summary

get_or_create_client has a check-then-create race for the same context.

Affected code

  • src/kubernetes/client.rs (get_or_create_client)

Root cause

Current flow:

  1. read lock check for existing client
  2. release lock
  3. create client
  4. acquire write lock and insert

Parallel callers for the same context can both miss step 1 and both create clients; last insert wins.

Impact

  • Primarily inefficiency/redundant work (duplicate kube client creation/handshake).
  • Can add startup latency under parallel discovery.
  • Lower correctness risk than discovery overwrite race, but still undesirable.

Proposed fix

  1. Add a second check under write lock before insert (double-checked locking pattern).
  2. Optionally use per-context once/mutex to guarantee single creation path.

Tests to add

  1. Concurrent calls to get_or_create_client for same context should create at most one underlying client.
  2. Verify all callers receive a valid client and no errors.

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions