Skip to content

Fix for #860 to avoid infinite loop#1064

Merged
merill merged 7 commits intodevfrom
merill-860-infinite-loop
Mar 24, 2026
Merged

Fix for #860 to avoid infinite loop#1064
merill merged 7 commits intodevfrom
merill-860-infinite-loop

Conversation

@merill
Copy link
Copy Markdown
Collaborator

@merill merill commented Mar 16, 2026

Fix infinite loop during Graph API paging (#860)

When paging through large Graph API result sets (e.g., UserRegistrationDetails), if the API returns an error response that still contains @odata.nextLink, the export loop runs indefinitely. This also applies when transient 400 errors (like expired skip tokens) are immediately classified as non-retryable, causing the export to fail without retry.

Changes

4 files changed across 3 areas:

1. Paging loop hardening — Export-ZtGraphEntity.ps1

  • Clear stale results: Set $results = $null at the start of each iteration so a failed API call (that was already retried five times) can't silently reuse the previous page's @odata.nextLink
  • Try-catch around API call: Catches errors from Invoke-ZtRetry, logs the entity name/page/URI, then re-throws so the export is marked as Failed
  • Error-response validation: Detects when Invoke-MgGraphRequest returns error JSON as a valid hashtable (with an error key) without throwing, and terminates the loop
  • Stuck-paging detection: Tracks $previousNextLink and breaks if the same @odata.nextLink is returned on consecutive pages

2. Broader retry classification — Test-ZtRetryableError.ps1

  • Changed from an allowlist (408, 429, 500, 502, 503, 504, 507) to a **denylist

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves resiliency and diagnostics around Microsoft Graph exports by adding explicit error handling/validation and paging-loop safety checks, plus enhancing HTTP status code extraction for retry logic.

Changes:

  • Add try/catch logging and detection of Graph “error” JSON responses during export paging.
  • Add “stuck nextLink” detection to prevent infinite paging loops.
  • Extend Get-ZtHttpStatusCode to parse raw HTTP status lines from exception messages.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/powershell/private/export/Export-ZtGraphEntity.ps1 Adds error logging/validation around Graph responses and adds stuck-paging detection during export.
src/powershell/private/core/Get-ZtHttpStatusCode.ps1 Adds an additional regex strategy to extract 4xx/5xx codes from raw HTTP status lines in exception messages.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/powershell/private/export/Export-ZtGraphEntity.ps1
Comment thread src/powershell/private/export/Export-ZtGraphEntity.ps1
Comment thread src/powershell/private/export/Export-ZtGraphEntity.ps1 Outdated
merill and others added 2 commits March 16, 2026 22:40
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the robustness of Microsoft Graph exports and retry handling by adding better error detection/logging in paging exports, broadening retry behavior, and enhancing HTTP status code extraction to recognize more error message formats.

Changes:

  • Add structured logging and error-response detection to Export-ZtGraphEntity, plus stuck paging detection.
  • Change retry policy to treat most HTTP status codes as retryable except 401/403/404.
  • Extend HTTP status code extraction to parse raw HTTP/x.y <status> status lines and update retry tests accordingly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/powershell/private/export/Export-ZtGraphEntity.ps1 Adds logging, error-response validation, and stuck nextLink detection during paged exports.
src/powershell/private/core/Test-ZtRetryableError.ps1 Changes retryability logic to a denylist (only 401/403/404 are non-retryable).
src/powershell/private/core/Get-ZtHttpStatusCode.ps1 Adds regex strategy to extract status codes from raw HTTP status lines.
code-tests/commands/Invoke-ZtRetry.Tests.ps1 Updates test expectation to retry HTTP 400 errors.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread src/powershell/private/export/Export-ZtGraphEntity.ps1
Comment thread src/powershell/private/export/Export-ZtGraphEntity.ps1
Comment thread src/powershell/private/core/Test-ZtRetryableError.ps1 Outdated
Comment thread src/powershell/private/core/Get-ZtHttpStatusCode.ps1
Comment thread code-tests/commands/Invoke-ZtRetry.Tests.ps1
merill and others added 3 commits March 16, 2026 23:05
@merill merill requested a review from alexandair March 16, 2026 12:16
@merill merill added the ready for review PR is ready for review and merging label Mar 16, 2026
Copy link
Copy Markdown
Collaborator

@alexandair alexandair left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@merill Please, address my feedback.

Comment thread src/powershell/private/core/Test-ZtRetryableError.ps1
@merill merill merged commit 6290231 into dev Mar 24, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for review PR is ready for review and merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants