[eluser]m4rw3r[/eluser]
Are you sure you won't get any "circular references" with the database keys?
(Eg. node.node_ref -> n, n.object_id -> parent_node, parent_node.descendants ∈ node Makes everything repeat itself).
Also, the get_descendants() return *all* descendants, not only children, which means that your code also iterates grandchildren which already were replaced by a recursive call to this method. Try with get_children() instead.
Another thing may be the fact that if you move nodes in the tree, you can move some nodes which already have been moved (which also can result in an endless loop).