Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-453

GridFS.new_file fails to raise FileExists with a manual _id

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 2.4.2
    • Affects Version/s: None
    • Component/s: None
    • None

      When using GridFS.new_file with a manually-specified, pre-existing _id, and safe=True on the database connection, FileExists is not raised. This happens when trying to create the same file with non-empty contents.

      db = Connection('localhost', safe=True).some_db
      f = GridFS(db).new_file(_id='some id')
      f.write('some content')
      f.close() # file is created correctly
      f = GridFS(db).new_file(_id='some id') # does not raise FileExists
      f.write('some content')
      f.close() # raises DuplicateKeyError instead of FileExists

      Reason:

      GridIn.close() calls GridIn.__flush(). This in turn eventually calls self._chunks.insert(chunk). With safe=True on the connection, the chunk insert raises DuplicateKeyError, which is not caught.

      This will also happen on the f.write(content) call if the content causes GridIn's buffer to exceed the chunk size (because it will flush and raise the same DuplicateKeyError).

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            tdterry Travis Terry
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: