fsspec / filesystem_spec

A specification that python filesystems should adhere to.
BSD 3-Clause "New" or "Revised" License
995 stars 352 forks source link

Context not preserved when open file with compression, leading exception to be ignored #1672

Open heiseish opened 5 days ago

heiseish commented 5 days ago

Context

Details

Reproducible code

import fsspec

def foo():
  with fsspec.open('s3://non-access-bucket/non-access-data.gz', mode = 'wb', compression = 'gzip') as f:
      f.write(b'hello world')

foo()

The above code snippet will happily run (assuming s3 credentials are setup properly etc) and function exits successfully, but later when the file object destructor get called, we will see the following exception

Exception

Exception ignored in: <funciton AbstractBufferredFile.__del__ at 0x7bc52f060040>
...
self.close
...
self.flush(force=True)
... snip ...
PermissionError: Access Denied

I have not tried with any other FS yet but I reckon as long as we can trigger the error during flush/close, we should be able to reproduce the error.

Temporary work around

Relevant library code

            f = self._open(
                path,
                mode=mode,
                block_size=block_size,
                autocommit=ac,
                cache_options=cache_options,
                **kwargs,
            )
            if compression is not None:
                from fsspec.compression import compr
                from fsspec.core import get_compression

                compression = get_compression(path, compression)
                compress = compr[compression]
                f = compress(f, mode=mode[0]) # Here
martindurant commented 1 day ago

This sounds like a genuine bug. I know there are methods to chain contexts, but probably there is a simple fix available by patching the close method of the compression file-like object.

This kind of thing doesn't show up in tests because: