Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Occasional ValueError when using CAR.from_bytes #362

Closed
that-damn-cat opened this issue Aug 5, 2024 · 5 comments · Fixed by #363
Closed

Occasional ValueError when using CAR.from_bytes #362

that-damn-cat opened this issue Aug 5, 2024 · 5 comments · Fixed by #363

Comments

@that-damn-cat
Copy link

Using the example code for CAR decoding firehose commits...

https://atproto.blue/en/latest/atproto_firehose/index.html

from atproto import FirehoseSubscribeReposClient, parse_subscribe_repos_message
from atproto import CAR, models

firehoseClient = FirehoseSubscribeReposClient()

def on_message_handler(message) -> None:
    commit = parse_subscribe_repos_message(message)
    # we need to be sure that it's commit message with .blocks inside
    if not isinstance(commit, models.ComAtprotoSyncSubscribeRepos.Commit):
        return

    car = CAR.from_bytes(commit.blocks)

# Start the firehose
firehoseClient.start(on_message_handler)

Will occasionally result in the following error:

ValueError: Failed to read CAR header. Invalid uvarint

With this traceback...

Traceback (most recent call last):
  File "[...]\.venv\Lib\site-packages\atproto_firehose\client.py", line 141, in _process_message_frame
    self._on_message_callback(frame)
  File "[...]\firehoseScrape.py", line 12, in on_message_handler
    car = CAR.from_bytes(commit.blocks)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "[...]\.venv\Lib\site-packages\atproto_core\car\car.py", line 51, in from_bytes
    header, blocks = libipld.decode_car(data)
                     ^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: Failed to read CAR header. Invalid uvarint
@that-damn-cat
Copy link
Author

Happens about once every million or so commits from the firehose, so it's fairly rare. I've seen it 3 times so far.

Set up a catch in code for it and am waiting for it to happen again so I can share the blocks that cause it.

@MarshalX
Copy link
Owner

MarshalX commented Aug 5, 2024

My guess is CAR.from_bytes(None). blocks sometimes are empty somehow...

@emilyhunt
Copy link

emilyhunt commented Aug 5, 2024

My guess is CAR.from_bytes(None). blocks sometimes are empty somehow...

Yeah, I think this is the issue. I think it can be a blank binary string sometimes - although it's out of spec for the AT Protocol, sometimes commits have empty blocks.

I handle if for the astronomy feeds with an extra check you could put before CAR.from_bytes(commit.blocks), like:

if not commit.blocks:
    return
car = CAR.from_bytes(commit.blocks)

@MarshalX
Copy link
Owner

MarshalX commented Aug 5, 2024

sounds like we need to fix the examples :)

@that-damn-cat
Copy link
Author

Yep, that's what I ended up catching was an empty block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants