Skip to content

ResponseCache has no file-level locking for concurrent access #14

@jtdub

Description

@jtdub

Summary

ResponseCache in cache.py reads and writes JSON cache files without any file-level locking. In multi-process scenarios (common in network automation managing many devices), two processes could race on the same cache file, producing corrupt reads or lost writes.

Current Behavior

  • get() opens and reads JSON without locking
  • set() opens and writes JSON without locking
  • No fcntl.flock() or equivalent mechanism

Impact

  • Corrupt cache reads leading to json.JSONDecodeError (caught, but cache miss)
  • Lost writes where one process overwrites another's cached response
  • Unlikely but possible partial reads of in-progress writes

Suggested Fix

Consider using fcntl.flock() (Unix) or a cross-platform file locking library like filelock for atomic read/write operations on cache files. Alternatively, consider using an in-memory cache (like cachetools) with an optional persistence layer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions