filesystem_spec icon indicating copy to clipboard operation
filesystem_spec copied to clipboard

Concurrent Instantiation bypasses Cache across Threaded Forks

Open yuxin00j opened this issue 4 months ago • 0 comments

Description

When running gcsfs in highly concurrent environments—specifically utilizing multiprocessing processes alongside internal multithreading—the underlying fsspec cache logic cannot handle when multiple threads try to instantiate concurrently.

If a Python process forks (e.g., via multiprocessing.Pool), the fsspec child process clears its inherited instance cache (os.getpid() != cls._pid). However, because this cache-clearing check and instantiation process was not thread-safe:

  1. All threads evaluate the newly empty cache post-fork.
  2. They bypass the cache concurrently.
  3. Every single overlapping thread instantiates a new, fully independent GCSFileSystem instance and overwrites the local cache.

This leads to significant redundant filesystem instantiations and associated overhead due to multiple parallel clients standing up unnecessarily.

Proposed Solution

We have developed a complete patch and will be landing a Pull Request in fsspec that introduces:

  1. Double-checked threading.Lock locking directly inside _Cached.__call__.
  2. Safe boundaries for os.getpid() != cls._pid post-fork cache clearing.
  3. Fixing tokenize() dependencies on PIDs prior to evaluating the cache.

yuxin00j avatar Jul 10 '26 08:07 yuxin00j