# Buggy code
def getFilesIndex(
    self, mtimes, oldmtimes, shareddirs, newsharedfiles, yieldcall=None, progress=None
):
    wordindex = defaultdict(list)
    fileindex = {}
    index = 0
    count = 0

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

        if progress:
            percent = float(count) / len(mtimes)
            if percent <= 1.0:
                GLib.idle_add(progress.set_fraction, percent)

        count += 1

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

        for j in newsharedfiles[virtualdir]:
            indexes = self.getIndexWords(virtualdir, j[0], shareddirs)

            for k in indexes:
                wordindex[k].append(index)

            fileindex[str(index)] = ((virtualdir + "\\" + j[0]),) + j[1:]
            index += 1

        if yieldcall is not None:
            yieldcall()

    return wordindex, fileindex


# 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 getFilesIndex(
    self, mtimes, oldmtimes, newsharedfiles, yieldcall=None, progress=None
):
    wordindex = defaultdict(list)
    fileindex = []
    index = 0
    count = len(mtimes)
    lastpercent = 0.0

    for directory in mtimes:
        virtualdir = self.real2virtual(directory)
        count += 1

        if progress:
            # Truncate the percentage to two decimal places to avoid sending data to the GUI thread too often
            percent = float("%.2f" % (float(count) / len(mtimes) * 0.75))

            if percent > lastpercent and percent <= 1.0:
                GLib.idle_add(progress.set_fraction, percent)
                lastpercent = percent

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

        for j in newsharedfiles[virtualdir]:
            file = j[0]
            fileindex.append((virtualdir + "\\" + file,) + j[1:])

            # Collect words from filenames for Search index (prevent duplicates with set)
            words = set(
                (virtualdir + " " + file)
                .translate(self.translatepunctuation)
                .lower()
                .split()
            )

            for k in words:
                wordindex[k].append(index)

            index += 1

        if yieldcall is not None:
            yieldcall()

    return wordindex, fileindex