* fix(network): name a blob directory for who is asking, and collect what stops …being named
The on-disk cache was named for a hash of the whole of `client_kwargs`, which
mixes the two kinds of thing that go in there. An `Authorization` header decides
what a server sends back; a timeout, a proxy and a User-Agent decide nothing
about it. The default User-Agent carries the version number, so every release
renamed the directory, began again from an empty cache, and left the previous one
behind where no key this version can produce would ever name it. One developer
machine: 129 directories under 98 distinct hashes, 4.3 GB in all, 115 MB of it
reachable -- and 1.4 GB under `ttl-INFINITE-*`, which is a provider saying those
bytes never change.
So the directory is separated by the TTL and by the credential, and by nothing
else; the layout is named (`v2-`) so that anything of the previous shape can be
told apart by its name rather than guessed at from its age. Those are reclaimed
on the first cached download of a process. So is a directory a credential named
that has since rotated -- Met Office mints a three-day JWT -- aged out at a month
by a marker touched whenever something asks for the directory, since the blobs
cannot answer that question: an archive read on every run and written on none has
file times as old as the day it was fetched. Only a directory a credential names
is ever aged out; reclaiming a shared one for looking idle would throw away the
main cache rather than a leftover.
The in-memory registry key is deliberately unchanged and still separates on
everything a filesystem is built from, transport settings included: `register`
runs only for a key that is new, so whatever that key omits, the first caller in
a thread decides for every later one (GH-1947).
And a blob its own TTL has already made useless is now dropped, once per
directory per process. The obvious version of that is destructive, which is why
it came back out of GH-1954: `CacheExpiry.INFINITE` is `False`, `int(False)` is
`0`, and fsspec reads an expiry of zero as "every entry is expired" -- it removes
them all and then `rmtree`s the directory. So a TTL that is not a positive number
is not swept at all; the expiry is passed explicitly rather than left to fsspec's
fallback; the directory is marked swept before the attempt rather than after,
since `clear_expired` raises for a half-written entry and a sweep retried on every
registration would fail every download for that TTL for the life of the process;
and the lock is held across the sweep rather than around the bookkeeping, which
is what keeps a `download_files` thread pool from writing rows the sweep's own
snapshot would then drop and orphan.
Fixes GH-1959
Fixes GH-1955
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(network): read a credential header whatever shape it is given in
Two findings from reviewing the commit before this one, both failing in the
dangerous direction.
`client_kwargs["headers"]` reaches aiohttp either as a mapping or as an iterable
of pairs, and both readers of "is there a credential here" assumed a mapping. A
pair list therefore answered no: two API keys shared one blob directory, and a
failure carrying the header went unscrubbed into the retry log `stamina` writes
-- the exposure GH-1954 exists to close. One reader now, shared by the two that
have to agree about the answer, and a shape it cannot read counts as carrying a
credential rather than as carrying none. Scrubbing a failure that held none costs
a traceback; not scrubbing one that did puts the header in a log.
And `_CACHE_LAYOUT_VERSION` was not self-cleaning, though its comment said it
was: the matcher took only the shape before `v2`, so the next bump would strand
every directory this one wrote -- the same unbounded leak as GH-1959, deferred
one release. Any layout but the current one is reclaimed now, in both directions,
so alternating between two versions costs a re-download rather than directories
neither of them will ever collect.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(network): pin the blob-directory pattern to the TTLs that actually name one
Third finding of the review. `_BLOB_CACHE_DIR` took `[A-Z_]+` for the TTL name, so
a directory named for a `CacheExpiry` member carrying a digit would not have read
as a blob directory at all, and would never have been reclaimed on a later layout
bump. No member has one today, which is what made it a trap rather than a bug.
The character class is widened, but the fix that matters is the test: it walks the
enum and asserts that every directory `_cache_path` actually writes -- shared and
credentialed, for every TTL there is -- is one the reclaim recognises. A member
that escapes the pattern now fails loudly instead of leaking a directory quietly.
The class stays a class rather than becoming the enum, because the directories
worth reclaiming are the ones older builds wrote, whose TTLs may since have been
renamed or removed. So it is deliberately wider than today's enum and cannot be
pinned by a test against it; what the test pins is the other direction.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(network): hold one lock over everything that opens or deletes a cache file
CI on Windows, which is the only place this shows:
PermissionError: [Errno 13] Permission denied:
'...\fsspec\v2-ttl-FIVE_MINUTES\cache'
at fsspec/implementations/cache_metadata.py:51
The lock covered the sweep but not the *building* of a caching filesystem, and
building one reads the directory's metadata file that the sweep and the reclaim
delete. On POSIX an unlink leaves the open handle readable, so the race is
invisible and every run on Linux and macOS passed; on Windows the builder gets
the error, raised nowhere near the code that caused it.
So the two locks become one, held across building, sweeping, marking and
reclaiming -- everything that opens or deletes a file under `cache_dir/fsspec`.
All of it is once-per-key or once-per-process work, so there is nothing to
contend for.
This also corrects a claim the docstring made: "every path to a download in this
directory passes through here first" was not true, because the path through
`WholeFileCacheFileSystem(...)` reached the directory before the lock. The test
added with this asserts that invariant directly rather than the platform
behaviour, and fails on macOS against the old arrangement.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* fix(network): read a credential for what it stands for, and separate on what can change a body
Twelve findings from review, each reproduced before it was fixed. Three matter.
`SecretStr` prints as `**********`, and `Settings.auth` has held credentials as
`SecretStr` since GH-1937 -- so `str(value)` mapped every secret to one value and
two API keys hashed alike:
key-A -> '-1eb77008'
key-B -> '-1eb77008' same directory: True
Worse, that hash also names the in-memory filesystem, so the second caller was
handed the first caller's filesystem, built with the first caller's
`Authorization` header. That is GH-1947 again, for credentials.
Headers given as an iterator were read to name the directory, which emptied them:
the request reached aiohttp with none, and the scrubber was told there was no
credential on a request that carried one. An iterator counts as unreadable now.
And `_CREDENTIAL_HEADERS` was doing two jobs with opposite failure costs. Written
for redaction, where missing a name costs a log line; reused to partition the
cache, where it costs one caller's body being handed to another. Since
`WD_FSSPEC_CLIENT_KWARGS` is public, `Accept-Language: de` and `en` shared a
directory. The cache separates on everything but a list of headers that cannot
change a body, so an unknown header costs a miss rather than a wrong answer.
The rest: `_IDENTITY_CACHE_DIR` was left at `[A-Z_]+` when `_BLOB_CACHE_DIR` was
widened, so a rotated credential under a TTL name carrying a digit would never be
aged out -- both are built from one fragment now. The size walk shared a `try`
with the removal, so a file vanishing under it left the one directory actually
being churned as the one never reclaimed. The log reported candidates rather than
removals. A missing marker condemned a directory the marker was introduced to
protect. Superseded layouts were removed with no grace, though the default cache
dir is shared by every install for a user and an older one may be mid-download.
Two comments asserted things the code does not do, one of them naming a function
this branch renamed. And met.no Frost's shallow copy wrote its auth header into
the dict `Settings` holds, which this branch made load-bearing.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* docs(changelog): describe the change this PR makes, not the passes that made it
Three review passes each appended an entry for what that pass fixed, and none
went back to the earlier ones. So the section carried three entries all fixing
"two API keys shared one blob directory" -- a narrative of my corrections rather
than a description of the change -- and the first of them still described a
design the third had replaced:
- "separated by the TTL and by who is asking" where the cache now separates on
every header that can change a body, `Accept-Language` among them;
- "only a directory a credential names is ever aged out", where a suffix can come
from any of those headers now;
- "the in-memory registry key is deliberately unchanged", which flatly
contradicts the entry saying that key was fixed because two `SecretStr`
credentials hashed alike in it.
The three credential entries are one entry, and the three claims are what the
code does. `tests/test_changelog.py` checks structure, not consistency, so
nothing here was going to catch any of it.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>