# Buggy code
def getByteStream(self, fileinfo):
    message = slskmessages.SlskMessage()

    stream = (
        bytes([1])
        + message.packObject(fileinfo[0])
        + message.packObject(NetworkLongLongType(fileinfo[1]))
    )
    if fileinfo[2] is not None:
        try:
            msgbytes = b""
            msgbytes += message.packObject("mp3") + message.packObject(3)
            msgbytes += (
                message.packObject(0)
                + message.packObject(NetworkIntType(fileinfo[2][0]))
                + message.packObject(1)
                + message.packObject(NetworkIntType(fileinfo[3]))
                + message.packObject(2)
                + message.packObject(NetworkIntType(fileinfo[2][1]))
            )
            stream += msgbytes
        except Exception:
            log.addwarning(
                _(
                    "Found meta data that couldn't be encoded, possible corrupt file: '%(file)s' has a bitrate of %(bitrate)s kbs, a length of %(length)s seconds and a VBR of %(vbr)s"
                    % {
                        "file": fileinfo[0],
                        "bitrate": fileinfo[2][0],
                        "length": fileinfo[3],
                        "vbr": fileinfo[2][1],
                    }
                )
            )
            stream += message.packObject("") + message.packObject(0)
    else:
        stream += message.packObject("") + message.packObject(0)

    return stream


# 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 getByteStream(self, fileinfo):
    message = slskmessages.SlskMessage()

    stream = bytearray()
    stream.extend(
        bytes([1])
        + message.packObject(fileinfo[0])
        + message.packObject(NetworkLongLongType(fileinfo[1]))
    )
    if fileinfo[2] is not None:
        try:
            msgbytes = bytearray()
            msgbytes.extend(message.packObject("mp3") + message.packObject(3))
            msgbytes.extend(
                message.packObject(0)
                + message.packObject(NetworkIntType(fileinfo[2][0]))
                + message.packObject(1)
                + message.packObject(NetworkIntType(fileinfo[3]))
                + message.packObject(2)
                + message.packObject(NetworkIntType(fileinfo[2][1]))
            )
            stream.extend(msgbytes)
        except Exception:
            log.addwarning(
                _(
                    "Found meta data that couldn't be encoded, possible corrupt file: '%(file)s' has a bitrate of %(bitrate)s kbs, a length of %(length)s seconds and a VBR of %(vbr)s"
                    % {
                        "file": fileinfo[0],
                        "bitrate": fileinfo[2][0],
                        "length": fileinfo[3],
                        "vbr": fileinfo[2][1],
                    }
                )
            )
            stream.extend(message.packObject("") + message.packObject(0))
    else:
        stream.extend(message.packObject("") + message.packObject(0))

    return stream