libmagic throws thread error when used with flask...

Issue #47 resolved
Daniel Fairhead
created an issue

Hi, I tried using eyeD3 with flask, following the basic eyeD3 advice from the docs, and it threw the following error:

File "/home/daniel/src/radio-playlist/.virtualenv/lib/python2.7/site-packages/eyed3/core.py", line 76, in load
    mtype = guessMimetype(path)
  File "/home/daniel/src/radio-playlist/.virtualenv/lib/python2.7/site-packages/eyed3/utils/__init__.py", line 78, in guessMimetype
    mime = magic_func(filename)
  File "/home/daniel/src/radio-playlist/.virtualenv/lib/python2.7/site-packages/eyed3/utils/__init__.py", line 61, in magic_func
    return _magic.from_file(path)
  File "/home/daniel/src/radio-playlist/.virtualenv/lib/python2.7/site-packages/magic.py", line 73, in from_file
    self._thread_check()
  File "/home/daniel/src/radio-playlist/.virtualenv/lib/python2.7/site-packages/magic.py", line 81, in _thread_check
    raise Exception('attempting to use libmagic on multiple threads will '
Exception: attempting to use libmagic on multiple threads will end in SEGV.  Prefer to use the module functions from_file or from_buffer, or carefully manage direct use of the Magic class

I've attached a very quick workaround patch, which simply wraps the magic_func() call in guessMimetype in a try/except clause, and so leaves it in the same situation as when magic doesn't work for other reasons.

Comments (14)

  1. Tom Hanson

    Hey guys, not sure if I'm doing something wrong, but I'm still getting this error with eyed3 installed from source.

      File "/home/pi/dev/project/PiCode/pisync/core/music_scan.py", line 94, in run_scan
        audio = eyed3.load(filepath)
      File "/usr/local/lib/python2.7/dist-packages/eyed3/core.py", line 84, in load
        mtype = guessMimetype(path)
      File "/usr/local/lib/python2.7/dist-packages/eyed3/utils/__init__.py", line 81, in guessMimetype
        mime = magic_func(filename)
      File "/usr/local/lib/python2.7/dist-packages/eyed3/utils/__init__.py", line 64, in magic_func
        return _magic.from_file(path)
      File "/usr/local/lib/python2.7/dist-packages/magic.py", line 73, in from_file
        self._thread_check()
      File "/usr/local/lib/python2.7/dist-packages/magic.py", line 81, in _thread_check
        raise Exception('attempting to use libmagic on multiple threads will '
    Exception: attempting to use libmagic on multiple threads will end in SEGV.  Prefer to use the module functions from_file or from_buffer, or carefully
     manage direct use of the Magic class
    

    Any idea how to solve this?

    Thanks!

  2. Travis Shirk repo owner

    The fix is in the changeset referenced above, but it has not been released yet (will be in 0.7.5). Or are you saying that applying the patch does not solve?

    I will try and cut a release this week.

  3. Tom Hanson

    Not specifically flask but threading in general:

    import threading
    import eyed3
    
    def test_function():
      mp3 = eyed3.load('/home/tom/test.mp3')
      print mp3.tag
    
    
    t = threading.Thread(target=test_function)
    t.start()
    

    Due to the check being made by Magic, if you import eyed3 from within the threaded function, it will work fine, but has to reload eyed3 each time.

  4. Travis Shirk repo owner

    Newer versions of python-magic contains the following, not letting eyeD3 to do the locking,

        def _thread_check(self):
            if self.thread != threading.currentThread():
                raise Exception('attempting to use libmagic on multiple threads will '
                                'end in SEGV.  Prefer to use the module functions '
                                'from_file or from_buffer, or carefully manage direct '
                                'use of the Magic class')
    

    Commit d2b30b2 removes the check, so all should be good.

  5. Log in to comment