Skip to content

Commit ca20605

Browse files
committed
fix import deletion race
By moving the duplicate file removal to the manipulate_files coroutine, we ensure that all previous albums are fully moved/copied before trying to delete their duplicate files.
1 parent e91a080 commit ca20605

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

beets/importer.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -768,13 +768,12 @@ def apply_choices(session):
768768

769769
# Delete duplicate files that are located inside the library
770770
# directory.
771+
task.duplicate_paths = []
771772
for duplicate_path in [i.path for i in duplicate_items]:
772773
if session.lib.directory in util.ancestry(duplicate_path):
773-
log.debug(u'deleting replaced duplicate %s' %
774-
util.displayable_path(duplicate_path))
775-
util.remove(duplicate_path)
776-
util.prune_dirs(os.path.dirname(duplicate_path),
777-
session.lib.directory)
774+
# Mark the path for deletion in the manipulate_files
775+
# stage.
776+
task.duplicate_paths.append(duplicate_path)
778777

779778
# Add items -- before path changes -- to the library. We add the
780779
# items now (rather than at the end) so that album structures
@@ -823,6 +822,15 @@ def manipulate_files(session):
823822
if task.should_skip():
824823
continue
825824

825+
# Remove duplicate files marked for deletion.
826+
if task.remove_duplicates:
827+
for duplicate_path in task.duplicate_paths:
828+
log.debug(u'deleting replaced duplicate %s' %
829+
util.displayable_path(duplicate_path))
830+
util.remove(duplicate_path)
831+
util.prune_dirs(os.path.dirname(duplicate_path),
832+
session.lib.directory)
833+
826834
# Move/copy/write files.
827835
items = task.imported_items()
828836
task.old_paths = [item.path for item in items] # For deletion.

docs/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Changelog
1414
* :doc:`/plugins/fetchart`: Look for images in the Cover Art Archive for
1515
the release group in addition to the specific release. Thanks to Filipe
1616
Fortes.
17+
* Fix a race in the importer that could cause files to be deleted before they
18+
were imported. This happened when importing one album, importing a duplicate
19+
album, and then asking for the first album to be replaced with the second.
20+
The situation could only arise when importing music from the library
21+
directory and when the two albums are imported close in time.
1722

1823

1924
1.2.1 (June 22, 2013)

0 commit comments

Comments
 (0)