Repository: beetbox/beets

Issue:
importadded: ValueError on trying to import a large library
After migrating my music to a new drive, I'm trying to update the beets library. I've first run `beet update` so beets flushes out the old files, and I'm now running `beet import -AC .` to import the new files. Along the way, I've seen this error show up a couple times, though it is inconsistent in when it shows:

```
Traceback (most recent call last):
  File "/usr/local/bin/beet", line 9, in <module>
    load_entry_point('beets==1.3.17', 'console_scripts', 'beet')()
  File "/usr/local/lib/python2.7/dist-packages/beets/ui/__init__.py", line 1236, in main
    _raw_main(args)
  File "/usr/local/lib/python2.7/dist-packages/beets/ui/__init__.py", line 1226, in _raw_main
    subcommand.func(lib, suboptions, subargs)
  File "/usr/local/lib/python2.7/dist-packages/beets/ui/commands.py", line 966, in import_func
    import_files(lib, paths, query)
  File "/usr/local/lib/python2.7/dist-packages/beets/ui/commands.py", line 943, in import_files
    session.run()
  File "/usr/local/lib/python2.7/dist-packages/beets/importer.py", line 321, in run
    pl.run_parallel(QUEUE_SIZE)
  File "/usr/local/lib/python2.7/dist-packages/beets/util/pipeline.py", line 348, in run
    self.coro.send(msg)
  File "/usr/local/lib/python2.7/dist-packages/beets/util/pipeline.py", line 161, in coro
    task = func(*(args + (task,)))
  File "/usr/local/lib/python2.7/dist-packages/beets/importer.py", line 1401, in manipulate_files
    task.finalize(session)
  File "/usr/local/lib/python2.7/dist-packages/beets/importer.py", line 544, in finalize
    self._emit_imported(session.lib)
  File "/usr/local/lib/python2.7/dist-packages/beets/importer.py", line 570, in _emit_imported
    plugins.send('album_imported', lib=lib, album=self.album)
  File "/usr/local/lib/python2.7/dist-packages/beets/plugins.py", line 459, in send
    result = handler(**arguments)
  File "/usr/local/lib/python2.7/dist-packages/beets/plugins.py", line 124, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/beetsplug/importadded.py", line 106, in update_album_times
    album.added = min(album_mtimes)
ValueError: min() arg is an empty sequence
```

I'm currently running beets 1.3.17, and my configuration is listed below:

```
importfeeds:
    dir: /media/Backup/Music
    relative_to:
    formats: m3u m3u_multi
    absolute_path: no
    m3u_name: imported.m3u
web:
    host: 0.0.0.0
    cors: '*'
convert:
    max_bitrate: 192
    command: ffmpeg -i $source -map_metadata 0 $dest
    extension: mp3
    never_convert_lossy_files: no
    format: mp3
    dest:
    auto: no
    threads: 4
    copy_album_art: no
    tmpdir:

    paths: {}
    pretend: no
    quiet: no
    formats:
        alac:
            command: ffmpeg -i $source -y -vn -acodec alac $dest
            extension: m4a
        aac:
            command: ffmpeg -i $source -y -vn -acodec libfaac -aq 100 $dest
            extension: m4a
        opus: ffmpeg -i $source -y -vn -acodec libopus -ab 96k $dest
        mp3: ffmpeg -i $source -y -vn -aq 2 $dest
        flac: ffmpeg -i $source -y -vn -acodec flac $dest
        ogg: ffmpeg -i $source -y -vn -acodec libvorbis -aq 2 $dest
        wma: ffmpeg -i $source -y -vn -acodec wmav2 -vn $dest
    embed: yes
per_disc_numbering: yes
fetchart:
    auto: no
    minwidth: 0
    sources:
    - coverart
    - itunes
    - amazon
    - albumart
    cautious: no
    google_engine: 001442825323518660753:hrh5ch1gjzm
    maxwidth: 0
    google_key: REDACTED
    cover_names:
    - cover
    - front
    - art
    - album
    - folder
    remote_priority: no
    enforce_ratio: no
embedart:
    auto: no
    compare_threshold: 0
    ifempty: no
    remove_art_file: no
    maxwidth: 0
replaygain:
    backend: gstreamer
    targetlevel: 89
    auto: yes
    overwrite: no
library: ~/.config/beets/library.db

plugins: duplicates importfeeds importadded replaygain embedart fetchart convert edit
directory: /media/Backup/Music
edit:
    itemfields: track title artist album
    albumfields: album albumartist
    ignore_fields: id path
importadded:
    preserve_mtimes: no
duplicates:
    count: no
    full: no
    format: ''
    keys: []
    move: ''
    tag: ''
    path: no
    copy: ''
    tiebreak: {}
    album: no
    strict: no
    checksum: ''
    merge: no
    delete: no
```

I've since disabled the `importadded` plugin so I can just import all my files, but is there a way to fix this in the future? This is running off of an NTFS filesystem, so I don't know if a `stat` command or similar isn't coming back correctly, but I haven't had issues with this in the past.

Let me know if there's any way I can assist.



Discussion:
Hi! Could you please narrow down a case that will let us reproduce the problem? Or is it really, truly random?

Also, could you perhaps validate the NTFS hypothesis by importing from a different volume for comparison?

It seems to be really, truly random: When running that same import command again, it would not consistently fail on any specific album.

I'll need some time to work on copying the files to a new volume, but I'll update this when I get more information.

Well, that's truly mysterious. I'll look into it to see if there's anything obvious, but yes, please do continue posting if you find any more evidence that could help nail it down.

I've got this same problem when importing. After some debugging I came to the conclusion that the task "import_task_start" isn't called if files are imported without copy, move, link, hardlink _and_ without changing the tags.

The result is that the item_mtime dict isn't populated. Changing the "import_task_start" to "import_task_created" seems to work. 

Aha! That could be it. Would you mind opening a pull request with that change so we can try it out?

Resolving patch:
diff --git a/beetsplug/importadded.py b/beetsplug/importadded.py
--- a/beetsplug/importadded.py
+++ b/beetsplug/importadded.py
@@ -30,8 +30,8 @@ def __init__(self):
         self.item_mtime = dict()
 
         register = self.register_listener
-        register('import_task_start', self.check_config)
-        register('import_task_start', self.record_if_inplace)
+        register('import_task_created', self.check_config)
+        register('import_task_created', self.record_if_inplace)
         register('import_task_files', self.record_reimported)
         register('before_item_moved', self.record_import_mtime)
         register('item_copied', self.record_import_mtime)


Test patch: