Don't write DNS resolution errors on Test-Connection -Quiet#12204
Don't write DNS resolution errors on Test-Connection -Quiet#12204iSazonov merged 11 commits intoPowerShell:masterfrom
Conversation
+ Add GetHostEntryWithCancel method and _cancel member. + Add cancellation for DNS lookups.
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
| It 'does not emit errors for an unresolvable address when using -Quiet' { | ||
| Test-Connection -Quiet -TargetName "fakeHost" -Count 1 | Should -BeFalse | ||
| } |
There was a problem hiding this comment.
The test name is seem not right.
There was a problem hiding this comment.
I'll give it another pass, I missed a couple things here, it seems.
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1
Outdated
Show resolved
Hide resolved
Co-Authored-By: Ilya <[email protected]>
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1
Outdated
Show resolved
Hide resolved
…onnection.Tests.ps1 Co-Authored-By: Ilya <[email protected]>
| protected override void StopProcessing() | ||
| { | ||
| _sender?.SendAsyncCancel(); | ||
| _dnsLookupCancel?.Cancel(); |
There was a problem hiding this comment.
_dnsLookupCancel is never null
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
| } | ||
| catch (PipelineStoppedException p) | ||
| { | ||
| throw p; |
There was a problem hiding this comment.
You should use throw; here.
Maybe a conditional catch is better?
catch (Exception ex) when ((ex as PipelineStoppedException) == null)
{
....
}
There was a problem hiding this comment.
Didn't know that was a thing. Is there a significant difference?
There was a problem hiding this comment.
Which one you are asking about?
for the throw, it will keep the original stack trace; while throw p will rewrite the stack trace.
for the conditional catch block, it's slightly efficient because it avoids looking for a handler for the PipelineStoppedException. Although the efficiency doesn't matter here, it looks more concise :)
There was a problem hiding this comment.
Poked at this one for a bit and changed my mind a couple times, but it looks like the conditional catch block is about the same as just doing a general catch with an if statement when it comes down to it. Separate catch block seems a little tidier.
There was a problem hiding this comment.
I'm fine keeping the catch (PipelineStoppedException p) block, as long as you change throw p to throw;.
The main benefit of conditional exception is to avoid unnecessary search of exception handlers, and of course, avoid running a handler.
| return true; | ||
| } | ||
|
|
||
| private IPHostEntry GetHostEntryWithCancel(string targetNameOrAddress) |
There was a problem hiding this comment.
Maybe name the method similarly to SendCancellablePing? GetCancellableHostEntry? 😄
Also, a minor thing, I recommend to pass in the cancellation token as parameter.
There was a problem hiding this comment.
No worries, I'll sort it out. 🙂
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
|
I think that should address all your comments @daxian-dbw. 🙂 Let me know if I misunderstood anything; I'm still pretty new to fiddling with cancellation tokens and whatnot. ^^ |
|
🤔 realizing that returning false from this method is still gonna be a bit janky. Need to handle it and actually write false values back in the other methods too... Hm. That's okay, shouldn't be too tricky to manage. EDIT: I think that ought to sort it out. Let me know if you have any concerns with how I'm handling it. 😊 |
322c943 to
0e3665b
Compare
+ Since we're not emitting errors for these cases, we need to make sure that we give the users an appropriate -Quiet response. + In most cases, this is just false. -MtuSize is a special case that typically outputs integers, so write -1 to indicate failure to connect there.
0e3665b to
1088377
Compare
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1
Outdated
Show resolved
Hide resolved
…onnection.Tests.ps1 Co-Authored-By: Ilya <[email protected]>
|
|
||
| private static byte[]? s_DefaultSendBuffer; | ||
|
|
||
| private CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource(); |
There was a problem hiding this comment.
From Codacy:
| private CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource(); | |
| private readonly CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource(); |
There was a problem hiding this comment.
Huh, I thought I... ah, I must have taken it out again at some point. Thanks!
…nt/TestConnectionCommand.cs Co-Authored-By: Ilya <[email protected]>
|
🎉 Handy links: |
PR Summary
We don't need to write a DNS resolution error if the user uses
-Quiet; it's the same as aFalseresult in that case and can be treated the same.Also, currently we're not able to respond to StopProcessing() between pings due to the
Dns.GetHostEntry()method blocking. Fix is to implement aGetHostEntryWithCancel()method that factors in a cancellation token.PR Context
Resolves #12147
Resolves #11160
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.