fsspec / fsspec/filesystem_spec

Using blockcache with local breaks, because the `LocalFileOpener.closed` property doesn't have a setter

Open
#1,456 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.4k
Forks
490
Avg merge
2d 3h
Merged PRs (30d)
38

Description

Trying to use the blockcache filesystem with a local filesystem as a target raises an error if you try to close a file manually. Here's a reproduction:

from tempfile import TemporaryDirectory

from fsspec import AbstractFileSystem, filesystem

local: AbstractFileSystem = filesystem("local")
f = local.open("file.txt", mode="w")
f.write("some data\n")
f.close()

with TemporaryDirectory(prefix="cache", dir=".", ignore_cleanup_errors=True) as cache_dir:
    cache: AbstractFileSystem = filesystem(
        "blockcache", fs=local, cache_storage=cache_dir
    )
    f = cache.open("file.txt")
    print(f.read())
    f.close()

The last .close() raises an error:

Traceback (most recent call last):
  File "C:\Users\Antonio.Dourado\workspace\project\fsspec-bug.py", line 16, in <module>
    f.close()
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\implementations\cached.py", line 365, in <lambda>
    f.close = lambda: self.close_and_update(f, close)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\implementations\cached.py", line 436, in <lambda>
    return lambda *args, **kw: getattr(type(self), item).__get__(self)(
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\implementations\cached.py", line 392, in close_and_update
    f.closed = True
    ^^^^^^^^
AttributeError: property 'closed' of 'LocalFileOpener' object has no setter

AbstractBufferedFile does define closed as a writable property, so, maybe LocalFileOpener should too?

In case anyone wonders, or if this looks dumber than I think, I'm using blockcache + local for testing. My plan is to use blockcache in my production code, taking an arbitrary filesystem as a parameter. Then in production I use abfs, and for testing, local.

Also, not sure if this can be in here, or maybe should be a different issue, but throwing dirfs in the mix breaks things even earlier. It looks like the reason is because DirFileSystem just uses AbstractBufferedFile directly, but AbstractBufferedFile doesn't implement _fetch_range, and blockcache will call this for reading. Here's a(nother) reproduction:

from tempfile import TemporaryDirectory

from fsspec import AbstractFileSystem, filesystem

with TemporaryDirectory(
    prefix="content-", dir=".", ignore_cleanup_errors=True
) as tmp_dir:
    local: AbstractFileSystem = filesystem("local")
    tmp = filesystem("dir", path=tmp_dir, fs=local)
    f = tmp.open("file.txt", mode="w")
    f.write("some data\n")
    f.close()

    with TemporaryDirectory(
        prefix="cache-", dir=".", ignore_cleanup_errors=True
    ) as cache_dir:
        cache: AbstractFileSystem = filesystem(
            "blockcache", fs=tmp, cache_storage=cache_dir
        )
        f = cache.open("file.txt")
        print(f.read())
        f.close()

And the output:

Traceback (most recent call last):
  File "C:\Users\Antonio.Dourado\workspace\project\fsspec-bug.py", line 21, in <module>
    print(f.read())
          ^^^^^^^^
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\spec.py", line 1844, in read
    out = self.cache._fetch(self.loc, self.loc + length)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\caching.py", line 136, in _fetch
    self.cache[sstart:send] = self.fetcher(sstart, send)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Antonio.Dourado\workspace\project\.venv\Lib\site-packages\fsspec\spec.py", line 1822, in _fetch_range
    raise NotImplementedError
NotImplementedError

Environment info:

  • Python: 3.11.6
  • fsspec: 2023.12.1
  • OS: Windows 10 Enteprise, x64

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing both examples, then inspect the traceback locations in fsspec/implementations/cached.py, fsspec/spec.py, and fsspec/caching.py. Check how blockcache closes wrapped files and fetches ranges through local and dir filesystems. Done means the local target closes successfully and the dir filesystem can be read through blockcache without NotImplementedError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.