REST API "/pagehierarchy/copy" causing multiple copies to be created

In a connect app, I am copying the pages of one space (a template) to another (user created) space upon user request and I am getting multiple copies of 1 particular page tree in the hierarchy. I’ve also noticed that if I don’t cap the wait time for the process to complete at 30-60 seconds, I can be waiting for a very long time, even though I clearly see pages have been copied very quickly. When I check all of the long tasks, I notice some have never completed and still look like they are trying to complete, even after hours (maybe longer) of not doing this particular task, and all of them are the copies for this particular page tree to other previously created (and possibly deleted) spaces. Any idea what can be going on here? I only copy each page once in the code below.

The steps I take:

  1. Get the subpages of the template space at the root level via:

/rest/api/space/TEMPLATESPACE/content
with options: ‘depth’:‘root’,“expand”: “children.page”

  1. Iterate over the template subpages and call:

‘/rest/api/content/’ + template_subpages[i].id + ‘/pagehierarchy/copy’,
with options:
{
“copyAttachments”: true,
“copyPermissions”: true,
“copyProperties”: true,
“copyLabels”: true,
“destinationPageId”: project_space_homepage.id,
}

  1. Process the response to get the long task id and wait for it to complete via:

    let copy_done = false;

    while(!copy_done)
    {
    let long_task_status = JSON.parse(data.body);
    var data = await AP.request(
    {
    url: long_task.links.status,
    error: function(error)
    {
    console.log("Error getting project homepage ", error)
    return;
    }
    });

    if(long_task_status.percentageComplete >= 100)
      copy_done = true;
    

    }

Here’s and example of what is created:

Confluence_Error

For anyone else who may run into this issue, this only seems to be a problem for me when ‘/pagehierarchy/copy’ has option “copyProperties”: true. If this is false, the copy happens as intended.