# Buggy code
def getFilesStreams(
    self, mtimes, oldmtimes, oldstreams, newsharedfiles, rebuild=False, yieldcall=None
):
    streams = {}

    for directory in list(mtimes.keys()):
        virtualdir = self.real2virtual(directory)

        if self.hiddenCheck({"dir": directory}):
            continue

        if not rebuild and directory in oldmtimes:
            if mtimes[directory] == oldmtimes[directory]:
                if os.path.exists(directory):
                    # No change
                    try:
                        streams[virtualdir] = oldstreams[virtualdir]
                        continue
                    except KeyError:
                        log.addwarning(
                            _("Inconsistent cache for '%(vdir)s', rebuilding '%(dir)s'")
                            % {"vdir": virtualdir, "dir": directory}
                        )
                else:
                    log.adddebug(
                        _("Dropping missing directory %(dir)s") % {"dir": directory}
                    )
                    continue

        streams[virtualdir] = self.getDirStream(newsharedfiles[virtualdir])

        if yieldcall is not None:
            yieldcall()

    return streams


# Traceback (KeyError)
Traceback (most recent call last):
File "/usr/local/lib/python3.7/shelve.py", line 111, in __getitem__
value = self.cache[key]
KeyError: 'Klassiek\\Jean Sibelius\\Two Serenades for Violin and Orchestra, Op.69'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/pynicotine/gtkgui/frame.py", line 2039, in _RescanFinished
self.np.shares.sendNumSharedFoldersFiles()
File "/usr/local/lib/python3.7/site-packages/pynicotine/shares.py", line 105, in sendNumSharedFoldersFiles
sharedfiles = sum([len(x) for x in list(conf["transfers"][shared_db].values())])
File "/usr/local/lib/python3.7/_collections_abc.py", line 762, in __iter__
yield self._mapping[key]
File "/usr/local/lib/python3.7/shelve.py", line 113, in __getitem__
f = BytesIO(self.dict[key.encode(self.keyencoding)])
File "/usr/local/lib/python3.7/shelve.py", line 70, in closed
raise ValueError('invalid operation on closed shelf')
ValueError: invalid operation on closed shelf

# Fixed code
def getFilesStreams(
    self, mtimes, oldmtimes, oldstreams, newsharedfiles, rebuild=False, yieldcall=None
):
    streams = {}

    for folder in mtimes:
        virtualdir = self.real2virtual(folder)

        if self.hiddenCheck({"dir": folder}):
            continue

        if not rebuild and folder in oldmtimes:
            if mtimes[folder] == oldmtimes[folder]:
                if os.path.exists(folder):
                    # No change
                    try:
                        streams[virtualdir] = oldstreams[virtualdir]
                        continue
                    except KeyError:
                        log.addwarning(
                            _("Inconsistent cache for '%(vdir)s', rebuilding '%(dir)s'")
                            % {"vdir": virtualdir, "dir": folder}
                        )
                else:
                    log.adddebug(
                        _("Dropping missing directory %(dir)s") % {"dir": folder}
                    )
                    continue

        streams[virtualdir] = self.getDirStream(newsharedfiles[virtualdir])

        if yieldcall is not None:
            yieldcall()

    return streams