Skip to content

BlobWriter close() behaviour #884

@cadnce

Description

@cadnce

Hi

The fileio.BlobReader/BlobWriter behaviour regarding multiple calls to close() is inconsistent both within the python-storage module and compared to the stdlib - https://docs.python.org/3/library/io.html#io.IOBase.close / "As a convenience, it is allowed to call this method more than once; only the first call, however, will have an effect."

Whilst I understand this is not how you would use this, this should allow you to reproduce.

from google.cloud.storage import Client
from google.cloud.storage.fileio import (BlobReader, BlobWriter)

blob = Client().bucket(BUCKET_NAME).blob(BLOB_NAME)
reader = BlobReader(blob=blob)
reader.close()
reader.close() # No exception

writer = BlobWriter(blob=blob, ignore_flush=True)
writer.close()
writer.close() # Exception

#Traceback (most recent call last):
#  File "<stdin>", line 1, in <module>
#  File "lib/python3.10/site-packages/google/cloud/storage/fileio.py", line 426, in close
#    self._checkClosed()  # Raises ValueError if closed.
#  File "lib/python3.10/site-packages/google/cloud/storage/fileio.py", line 433, in _checkClosed
#    raise ValueError("I/O operation on closed file.")
#ValueError: I/O operation on closed file.

Whilst I understand that it might not be good practice to call close multiple times, and instead handle state properly. However some libraries are built around the idea that they can just call close when they want as they assume they are handling standard file-like objects per the python docs for file-like objects.

A slightly more realistic example is:

blob = Client().bucket(BUCKET_NAME).blob(BLOB_NAME)
with blob.open('wb') as raw_file:
    with io.BufferedWriter(raw_file) as buffered_file:  # When this closes it closes the parent.
      buffered_file.write('hello'.encode())

I would like to see on the first call to close() the upload would be finished/closed and subsequent calls would be no-ops.

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/python-storage API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions