I'm using PnP.PowerShell 1.3.21-nightly. I'm provisioning 50 communication sites through a tenant template ( Invoke-PnPTenantTemplate), then am looping through these sites and applying different site templates (Invoke-PnPSiteTemplate):
The behavior is not consistent, for example, I'd get the following errors when I run scripts the first time on site A, B, G, H, Z.. it fails, I try running the scripts again, it fails this time on sites C, F, E, W.. am sure it's not the site templates because when I try the templates one by one, everything works fine.
1) A task was canceled

2) Another error am getting is this one:

The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing.
Sometimes it also says:
The request was canceled due to the configured HttpClient.Timeout of 200 seconds elapsing.
3) I've been also getting access denied errors when trying to apply a site template.
@derhallim : you're raising a good point about the http timeouts, I'll look into this
@derhallim: I've just pushed https://github.com/pnp/pnpframework/commit/de9dd6ee71946d31e4b50d0abb4047fca6898c41 allowing you to configure a higher http timeout for the httpclient created by PnP framework. Might not solve all issues but hope this helps. Would recommend doing a Get-PnPException after a failure to better understand what went wrong.
Also note that there might be throttling in play or an API hiccup from the SharePoint side
I suspect it might be related to throttling as when I ran the script with only 5 sites at the same time with 20 seconds pause between each Invoke-PnPSiteTemplate, it seems to work just fine. As far as I know if you run Invoke-PnPSiteTemplate 50 times in a row, they shouldn't be executed asynchronously, right? I'm adding a 20 seconds pause now and running batches of 5 sites at a time to see if this would fix these issues.
things are executed sequentially, throttling can be detected by running Fiddler and check server responses
Confirmed it's due to throttling. Thanks @jansenbe