Skip to content

Concurrent Instantiation bypasses Cache across Threaded Forks #2065

Description

@yuxin00j

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions