<p dir="auto">The following script reliably causes a deadlock (or perhaps hanging for some other reason) on my machine.  Not sure if it is a pytorch bug or a librosa bug.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sys
from urllib.request import urlopen
from urllib.error import URLError
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
import librosa

# I am using the ImageFolder dataset to load audio data.
# To do this, I rename all my WAV files to end in .jpg
# so ImageFolder will think they are images and then
# load them with librosa.  Note: it is important that there
# are no actual image files under ./data!

# The program hangs after printing 
#
#    trying to load ./data/foo/file.jpg
#
# However, if bug_condition is set to False, or if either
# fixes_bug1 or fixes_bug2 is set to True, then it does
# not hang.

bug_condition = True
fixes_bug1 = False
fixes_bug2 = False

if bug_condition:
    try:
        urlopen('http://localhost:8097')
    except URLError:
        pass
    else:
        sys.exit('please retry without a server running on port 8097')

if fixes_bug1:
    # Note: ./data/foo/file.jpg must be a valid WAV file.
    librosa.load('./data/foo/file.jpg')

def audio_loader(filename):
    print('trying to load', filename)
    audio = librosa.load(filename, sr=16000)[0]
    print('loaded', filename)
    return audio

data = ImageFolder(root='./data', loader=audio_loader)
loader = DataLoader(data, num_workers=(0 if fixes_bug2 else 1))

for x, y in loader:
    print('OK')"><pre class="notranslate"><code class="notranslate">import sys
from urllib.request import urlopen
from urllib.error import URLError
from torchvision.datasets import ImageFolder
from torch.utils.data import DataLoader
import librosa

# I am using the ImageFolder dataset to load audio data.
# To do this, I rename all my WAV files to end in .jpg
# so ImageFolder will think they are images and then
# load them with librosa.  Note: it is important that there
# are no actual image files under ./data!

# The program hangs after printing 
#
#    trying to load ./data/foo/file.jpg
#
# However, if bug_condition is set to False, or if either
# fixes_bug1 or fixes_bug2 is set to True, then it does
# not hang.

bug_condition = True
fixes_bug1 = False
fixes_bug2 = False

if bug_condition:
    try:
        urlopen('http://localhost:8097')
    except URLError:
        pass
    else:
        sys.exit('please retry without a server running on port 8097')

if fixes_bug1:
    # Note: ./data/foo/file.jpg must be a valid WAV file.
    librosa.load('./data/foo/file.jpg')

def audio_loader(filename):
    print('trying to load', filename)
    audio = librosa.load(filename, sr=16000)[0]
    print('loaded', filename)
    return audio

data = ImageFolder(root='./data', loader=audio_loader)
loader = DataLoader(data, num_workers=(0 if fixes_bug2 else 1))

for x, y in loader:
    print('OK')
</code></pre></div>
<p dir="auto">the bug is described at <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="223226628" data-permission-text="Title is private" data-url="https://github.com/pytorch/examples/issues/148" data-hovercard-type="issue" data-hovercard-url="/pytorch/examples/issues/148/hovercard" href="https://github.com/pytorch/examples/issues/148">pytorch/examples#148</a>. I just wonder if this is a bug in PyTorch itself, as the example code looks clean to me. Also, I wonder if this is related to <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="217418696" data-permission-text="Title is private" data-url="https://github.com/pytorch/pytorch/issues/1120" data-hovercard-type="issue" data-hovercard-url="/pytorch/pytorch/issues/1120/hovercard" href="https://github.com/pytorch/pytorch/issues/1120">#1120</a>.</p>