TorchScript segfault when using recursive custom classes


Following the comments in this question, I was trying to create a class that has attributes of the same class type as shown below:

import torch
from typing import Dict, List, Optional

class JSONValue(object):
    def __init__(self):
        self.value = torch.jit.annotate(Optional[JSONValue], None)

class TestModule(torch.jit.ScriptModule):
    def forward(self, input):
        # type: (JSONValue) -> JSONValue
        return input

m = TestModule()"")

But this code results in a segfault with message Segmentation fault: 11. Any thoughts on what could be wrong?


We do not actually currently support recursive class definitions. I didn’t remember that in the previous thread (will comment there as well). improves the error message in this case.

We will likely support it soon (before the next release) but for now it will not work.

1 Like