y-crdt / ypy

Python bindings to y-crdt

Home Page:https://ypy.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Parent points to a block which is not a shared type

anujism opened this issue · comments

We are using ypy in a project and in some cases we are getting following error:

thread '<unnamed>' panicked at 'Defect: parent points to a block which is not a shared type', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/yrs-0.12.2/src/block.rs:1128:30

and once this error comes, the document gets corrupted and starts throwing following error:

pyo3_runtime.PanicException: Couldn't get item's parent

Here is how I am processing the message from clients (this is copied from ypy-websocket and modified for our use case):

async def process_message(self, message: bytes, ydoc: Y.YDoc):
        if message[0] != YMessageType.SYNC:
            return
        message_type = message[1]
        msg = message[2:]
        if message_type == YSyncMessageType.SYNC_STEP1:
            state = read_message(msg)
            update = Y.encode_state_as_update(ydoc, state)      **# this is where the error occurs.**
            reply = create_sync_step2_message(update)
            await self.send_message(reply)
        elif message_type in (YSyncMessageType.SYNC_STEP2, YSyncMessageType.SYNC_UPDATE):
            update = read_message(msg)
            # Ignore empty updates (see https://github.com/y-crdt/ypy/issues/98)
            if update == b"\x00\x00":
                return
            try:
                Y.apply_update(ydoc, update)
            except:  # noqa: E722
                # here i am ignoring errors from Rust.
                logger.exception("Bad message - not applying and storing in redis")
            else:
                return update

Before the ypy==0.5.5 the error used to happen only on Y.apply_update(ydoc, update) but now it has started happening inside Y.encode_state_as_update(ydoc, state) also.

I tried to debug it lot but couldn't identify the exact problem. If it helps I am using Lexicaljs as my frontend which generates these updates and I am storing them in redis (also merging updates every 5 minutes when there is no client connected).

This was the last update which might have caused the shared type error:

b'\x01\x01\xea\xe9\x89!:\x84\xea\xe9\x89!9\x01n\x00'

Following is the state and message which caused bad parent afterwards:
state: b'\x00'
message: b'\x00\x00\x01\x00'

PS: https://github.com/y-crdt/ypy-websocket/blob/main/ypy_websocket/yutils.py#L46 - read_message in above code.