# Buggy code
def addToShared(self, name):
    """Add a file to the normal shares database"""

    config = self.config.sections
    if not config["transfers"]["sharedownloaddir"]:
        return

    shared = config["transfers"]["sharedfiles"]
    sharedstreams = config["transfers"]["sharedfilesstreams"]
    wordindex = config["transfers"]["wordindex"]
    fileindex = config["transfers"]["fileindex"]

    shareddirs = [path for _name, path in config["transfers"]["shared"]]
    shareddirs.append(config["transfers"]["downloaddir"])

    sharedmtimes = config["transfers"]["sharedmtimes"]

    dir = str(os.path.expanduser(os.path.dirname(name)))
    vdir = self.real2virtual(dir)
    file = str(os.path.basename(name))

    shared[vdir] = shared.get(vdir, [])

    if file not in [i[0] for i in shared[vdir]]:
        fileinfo = self.getFileInfo(file, name)
        shared[vdir] = shared[vdir] + [fileinfo]
        sharedstreams[vdir] = self.getDirStream(shared[vdir])
        words = self.getIndexWords(vdir, file, shareddirs)
        self.addToIndex(wordindex, fileindex, words, vdir, fileinfo)
        sharedmtimes[vdir] = os.path.getmtime(dir)
        self.newnormalshares = True

    if config["transfers"]["enablebuddyshares"]:
        self.addToBuddyShared(name)

    self.config.writeShares()


# 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 addToShared(self, name):
    """Add a file to the normal shares database"""

    config = self.config.sections
    if not config["transfers"]["sharedownloaddir"]:
        return

    shared = config["transfers"]["sharedfiles"]
    sharedstreams = config["transfers"]["sharedfilesstreams"]
    wordindex = config["transfers"]["wordindex"]
    fileindex = config["transfers"]["fileindex"]

    shareddirs = [path for _name, path in config["transfers"]["shared"]]
    shareddirs.append(config["transfers"]["downloaddir"])

    sharedmtimes = config["transfers"]["sharedmtimes"]

    dir = str(os.path.expanduser(os.path.dirname(name)))
    vdir = self.real2virtual(dir)
    file = str(os.path.basename(name))

    shared[vdir] = shared.get(vdir, [])

    if file not in [i[0] for i in shared[vdir]]:
        fileinfo = self.getFileInfo(file, name)
        shared[vdir] = shared[vdir] + [fileinfo]
        sharedstreams[vdir] = self.getDirStream(shared[vdir])
        words = self.getIndexWords(vdir, file, shareddirs)
        self.addToIndex(wordindex, fileindex, words, vdir, fileinfo)
        sharedmtimes[vdir] = os.path.getmtime(dir)
        self.newnormalshares = True

    if config["transfers"]["enablebuddyshares"]:
        self.addToBuddyShared(name)