Clone markups node fails during custom module cleanup

I use this code to clone a ClosedCurveMarkups node

# Clone the node
itemIDToClone = shNode.GetItemByDataNode(dataNode)
clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemIDToClone)
clonedNode = self.shNode.GetItemDataNode(clonedItemID)

In all other instances, the node clones without issue. When I invoke this code to clone and save the node after the user closes the application, however, it fails, with clonedItemID always returning a 0. Is there a way to resolve or circumvent this?

I’ve created a curve node and ran this:

shNode = slicer.vtkMRMLSubjectHierarchyNode.GetSubjectHierarchyNode(slicer.mrmlScene)
itemIDToClone = shNode.GetItemByDataNode(dataNode)
clonedItemID = slicer.modules.subjecthierarchy.logic().CloneSubjectHierarchyItem(shNode, itemIDToClone)
clonedNode = shNode.GetItemDataNode(clonedItemID)
print(clonedItemID)
print(clonedNode)

It cloned the curve node currectly. clonedItemID was valid (non-zero). clonedNode was valid. Saved and loaded the scene. The original and the cloned curve both loaded correctly. So, I don’t see any issues. Maybe self.shNode in your code contains some old subject hierarchy node.