Skip to content

Error using CBOR encoder: unpack requires a buffer of 4 bytes #236

@BrendanSimon

Description

@BrendanSimon

I am using the CBOR encoder and once saw the following error reported in exception dump

  File: "/usr/local/lib/python3.7/dist-packages/persistqueue/serializers/cbor2.py", line 64, in load
    length = length_struct.unpack(fp.read(4))[0]
struct.error: unpack requires a buffer of 4 bytes

Seems to be when reading the 4 bytes (32-bit integer) length, but presumably the read fails for some reason (none or not enough bytes to read).
I don't believe this should be possible (not unless something else has deleted the file).

The same code logic exists for other codecs (e.g. msgpack).

Should there be some safe guard exception handling when reading the length and also the data?

Image

NOTE: this occurred when calling my clear() method from a sub-class. See #237

    pq.clear()
from persistqueue import (
    Queue as PersistQueue,
    Empty as PersistQueueEmpty,
    Full as PersistQueueFull,
    )

class MyPersistQueue(PersistQueue):
    """
    Customised persistqueue.Queue
    """

    def clear(self) -> None:
        """
        Clear all items from queue.

        persistqueue.Queue does not have a native `clear()` method.
        """

        while True:
            try:
                self.get_nowait()
            except PersistQueueEmpty:
                break

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions