filesystem_spec icon indicating copy to clipboard operation
filesystem_spec copied to clipboard

fix: Make Cached metaclass instance instantiation thread-safe and fork-safe

Open yuxin00j opened this issue 4 months ago • 4 comments

Fix #2065

Scope

This PR tackles a race condition in the _Cached metaclass.

The Problem

There were three distinct overlapping lock gaps around system forks:

  1. Unprotected Instantiation: The super().__call__ instantiation process inside _Cached was entirely unprotected, allowing concurrent cache misses to spin up disjoint raw instances independently.
  2. Post-fork Cache Clearing Race: When a process forks, os.getpid() != cls._pid attempts to clear the inherited cache. When 10 new threads simultaneously wake up post-fork and evaluate this, they concurrently bypass the cache logic.
  3. Ghost Tokenization: token = tokenize(cls, cls._pid, ...) historically evaluated before any locking boundary. The very first thread running post-fork evaluated tokenize() utilizing the lingering parent's PID before updating cls._pid inside the lock. Subsequence threads would then evaluate tokenize() natively under the child's PID, resulting in permanently ghosted, duplicated Cache hashes for identically parameterized filesystems.

The Solution

  • Double-Checked Locking: Re-instated a standard _instantiation_lock in _Cached.__call__ that strictly gates the underlying filesystem instantiation and cache updates. We also updated clear_instance_cache() to respect this exact lock.
  • Encapsulated Fork Boundaries: Moved the os.getpid() != cls._pid cache clearing evaluation strictly inside the boundaries of the lock. Furthermore, we explicitly re-initialize cls._instantiation_lock = threading.RLock() across forks so child processes do not natively inherit and permanent-deadlock on parent-locked states.
  • Deterministic Tokenizing: Altered tokenize() targeting to natively utilize os.getpid() rather than evaluating across cls._pid, ensuring deterministic key hashes pre-and-post fork regardless of strict thread scheduling.
  • Testing: Added test_instance_cache_concurrency, test_clear_instance_cache_concurrency, and test_fork_deadlock heavily simulating asynchronous thread congestion across cache misses, explicit cache-clear injections, and native OS forks to guarantee locking determinism.

yuxin00j avatar Jul 09 '26 10:07 yuxin00j

Hi @martindurant, could you take a look at this PR please? Thanks.

yuxin00j avatar Jul 10 '26 13:07 yuxin00j

To check: there is no way to un-register a function passed via register-at-fork, nor even a way to list them. Is there any way the function can get registered multiple times?

martindurant avatar Jul 16 '26 19:07 martindurant

A thorny problem you are solving! Can we really not just say "don't use fork"?

This pattern exists in major upstream libraries. Fixing the race condition ensures we enforce only one instance per process, allowing for safe, efficient resource sharing after a fork.

yuxin00j avatar Jul 17 '26 03:07 yuxin00j

To check: there is no way to un-register a function passed via register-at-fork, nor even a way to list them. Is there any way the function can get registered multiple times?

Re-instantiating the lock and calling .clear() should be idempotent.

yuxin00j avatar Jul 17 '26 03:07 yuxin00j