This raises: "sqlite3.ProgrammingError: Cannot operate on a closed database." happening in the declare_datatype function. I guess it is because w1 is still a part of or2.WORLDS and world.graph.read_only is False.
I worked around that problem by setting read_only to True after closing the world. Maybe this could be integrated directly into the close()-method of the World class. An alternative could be to remove this world from or2.WORLDS on closing. I tried both approaches, but the second sporadically still caused exceptions in my case.