GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix HTTP status from 409 to 429 for WebCmdlets to get retry interval from Retry-After header. by mkht · Pull Request #19622 · PowerShell/PowerShell

Conversation

@mkht

@mkht mkht commented May 6, 2023

Copy link
Copy Markdown
Contributor

PR Summary

The code that gets the appropriate retry interval from the Retry-After response header is incorrect and is executing with status code 409 when it should be executing with status code 429. This PR will correct that.

The original code was implemented in PR #18717.
This PR will fix #19621 and also correctly resolves #13188 again.

PR Checklist

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge and is not Work in Progress.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

CarloToso reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@mkht
Fix status code to get Retry-After from 409 to 429
3a1b998
@mkht
mkht requested a review from Paul Higinbotham (PaulHigin) as a code owner May 6, 2023 18:03
@pull-request-quantifier-deprecated pull-request-quantifier-deprecated Bot added the Extra Small label May 6, 2023
@ghost ghost assigned Aditya Patwardhan (adityapatwardhan) May 6, 2023
@mkht

mkht commented May 6, 2023

Copy link
Copy Markdown
Contributor Author

This is my first PR for the PowerShell repository, so please let me know if there are any problems.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@CarloToso

CarloToso commented May 6, 2023

Copy link
Copy Markdown
Contributor

Great catch, thank you for your contribution !

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

iSazonov
Ilya (iSazonov) approved these changes May 7, 2023
View reviewed changes
Comment thread ...t.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
// If the status code is 429 get the retry interval from the Headers.
// Ignore broken header and its value.
if (response.StatusCode is HttpStatusCode.Conflict && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable<string> retryAfter))
if (response.StatusCode is HttpStatusCode.TooManyRequests && response.Headers.TryGetValues(HttpKnownHeaderNames.RetryAfter, out IEnumerable<string> retryAfter))

@iSazonov Ilya (iSazonov) May 7, 2023

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

It would be great to cover this by a test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@CarloToso CarloToso May 7, 2023

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ilya (@iSazonov) I can look into it

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label May 7, 2023
@CarloToso CarloToso mentioned this pull request May 8, 2023
Add test for error 429 Retry-After #19623
Closed
6 of 22 tasks
@mkht
Add tests for Retry-After in http 429 status
8848f05
@mkht

mkht commented May 8, 2023

Copy link
Copy Markdown
Contributor Author

Oops, I just added test code, not noticing that CarloToso had already added in another PR.
Should I revert my test code? I will follow the moderator's instructions.

Thanks CarloToso . And sorry I didn't notice your comment. 😥

CarloToso reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@pull-request-quantifier-deprecated pull-request-quantifier-deprecated Bot added Small and removed Extra Small labels May 8, 2023
iSazonov
Ilya (iSazonov) reviewed May 8, 2023
View reviewed changes
Comment thread test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
$jsonResult.SessionId | Should -BeExactly $sessionId
}

It "Invoke-WebRequest respects the Retry-After header value in 429 status" {

@iSazonov Ilya (iSazonov) May 8, 2023

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Can you confirm the tests fail before the change?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mkht mkht May 8, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I've already tested it. Before the change (current master branch) these tests failed.

Executing script F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1

  Describing Invoke-WebRequest tests
WARNING: This module is use-at-your-own-risk.
It exists to test PowerShell Core builds and is not supported by Microsoft.
Please report any issues at https://github.com/rjmholt/SelfSignedCertificate.
Certificate written to C:\Users\mkht\AppData\Local\Temp\ServerCert.pfx
WARNING: Parameter 'EmailAddress' is obsolete. The email name component is deprecated by the PKIX standard
Certificate written to C:\Users\mkht\AppData\Local\Temp\ClientCert.pfx

    Context Invoke-WebRequest retry tests
      [+] Invoke-WebRequest can retry - specified number of times - error 304 2.15s
      [+] Invoke-WebRequest can retry - specified number of times - error 400 3.07s
      [+] Invoke-WebRequest can retry - specified number of times - error 599 1.02s
      [+] Invoke-WebRequest can retry - specified number of times - error 404 2.04s
      [+] Invoke-WebRequest can retry - when retry count is higher than failure count 2.03s
      [+] Invoke-WebRequest should fail when failureCount is greater than MaximumRetryCount 1.04s
      [+] Invoke-WebRequest can retry with POST 1.02s
      [-] Invoke-WebRequest respects the Retry-After header value in 429 status 6.05s
        Expected the actual value to be less than 2.9, but got 6.0393409.
        2179:             $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
        at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 2179
      [-] Invoke-WebRequest ignores the Retry-After header value NOT in 429 status 8.05s
        Expected the actual value to be less than 2.9, but got 8.0427231.
        2200:             $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
        at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 2200

  Describing Invoke-RestMethod tests

    Context Invoke-RestMethod retry tests
      [+] Invoke-RestMethod can retry - specified number of times - error 304 2.04s
      [+] Invoke-RestMethod can retry with POST 1.02s
      [-] Invoke-RestMethod respects the Retry-After header value in 429 status 6.03s
        Expected the actual value to be less than 2.9, but got 6.0209983.
        4111:             $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
        at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 4111
      [-] Invoke-RestMethod ignores the Retry-After header value NOT in 429 status 8.05s
        Expected the actual value to be less than 2.9, but got 8.0350285.
        4132:             $stopWatch.Elapsed.TotalSeconds | Should -BeLessThan 2.9
        at <ScriptBlock>, F:\PowerShell\test\powershell\Modules\Microsoft.PowerShell.Utility\WebCmdlets.Tests.ps1: line 4132

Tests completed in 128.49s
Tests Passed: 557, Failed: 4, Skipped: 4, Pending: 34, Inconclusive: 0 

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@CarloToso

CarloToso commented May 8, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

mkht Don't revert your changes, I'll close my PR. I think you should use the -Verbose redirect method from my PR instead of using System.Diagnostics.Stopwatch. Sometime the tests take more time than intended and so they could fail randomly

mkht reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mkht
Change success/failure judgment to verbose message instead of elapsed…
4a1d067
… time
@pull-request-quantifier-deprecated pull-request-quantifier-deprecated Bot added Extra Small and removed Small labels May 8, 2023
@pull-request-quantifier-deprecated

pull-request-quantifier-deprecated Bot commented May 8, 2023

Copy link
Copy Markdown

This PR has 46 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +45 -1
Percentile : 18.4%

Total files changed: 2

Change summary by file extension:
.cs : +1 -1
.ps1 : +44 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mkht

mkht commented May 8, 2023

Copy link
Copy Markdown
Contributor Author

I followed CarloToso advice and changed the method of judging the test to using verbose messages. I confirmed that the test failed with the pre-modified code and succeeded with the modified code.

CarloToso reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

daxian-dbw
Dongbo Wang (daxian-dbw) approved these changes May 8, 2023
View reviewed changes

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks mkht for the contribution!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 9502931 into PowerShell:master May 8, 2023
@mkht
mkht deleted the fix-http429-retryafter branch May 9, 2023 13:37
@ghost

Deleted user (ghost) commented Jun 29, 2023

Copy link
Copy Markdown

🎉v7.4.0-preview.4 has been released which incorporates this pull request.:tada:

Handy links:

  • Release Notes
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced Jun 29, 2023
Invoke-RestMethod and Invoke-WebRequest do not respect Retry-After header in 429 responses. #13188
Closed
Retries in WebCmdlet does not respect Retry-After header in 429 status #19621
Closed
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
@mkht
Fix HTTP status from 409 to 429 for WebCmdlets to get retry interval …
1691c9f
…from Retry-After header. (PowerShell#19622)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@iSazonov Ilya (iSazonov) iSazonov approved these changes
@daxian-dbw Dongbo Wang (daxian-dbw) daxian-dbw approved these changes
@PaulHigin Paul Higinbotham (PaulHigin) Awaiting requested review from PaulHigin
+1 more reviewer
@CarloToso CarloToso CarloToso left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@adityapatwardhan Aditya Patwardhan (adityapatwardhan)

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Retries in WebCmdlet does not respect Retry-After header in 429 status Invoke-RestMethod and Invoke-WebRequest do not respect Retry-After header in 429 responses.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@mkht @CarloToso @daxian-dbw @iSazonov @adityapatwardhan
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.