Skip to content

cyclic references cause infinite loop in .serialize() #62

@mivanit

Description

@mivanit

Details

See tests/unit/json_serialize/serializable_dataclass/test_serializable_dataclass.py, test_cyclic_references.

In cases with cyclic references, we should at minimum throw an exception -- in an ideal world, we could have some way of encoding references to an already-serialized object, but this sounds much harder.

This test below reproduces the issue, hanging forever. Currently will be skipped.

def test_cyclic_references():
    """Test handling of cyclic references"""
    @serializable_dataclass
    class Node(SerializableDataclass):
        value: str
        next: Optional['Node'] = serializable_field(default=None)
    
    # Create a cycle
    node1 = Node("one")
    node2 = Node("two")
    node1.next = node2
    node2.next = node1
    
    # Ensure we can serialize without infinite recursion
    serialized = node1.serialize()
    loaded = Node.load(serialized)
    assert loaded.value == "one"
    assert loaded.next.value == "two"

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions