Skip to content

Don't write DNS resolution errors on Test-Connection -Quiet - #12204

Merged
Ilya (iSazonov) merged 11 commits into
PowerShell:masterfrom
vexx32:TestConnectionQuietness
Mar 28, 2020
Merged

Ilya (iSazonov) merged 11 commits into
PowerShell:masterfrom
vexx32:TestConnectionQuietness

Conversation

@vexx32

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) commented Mar 25, 2020 •

Copy link
Copy Markdown
Collaborator

PR Summary

We don't need to write a DNS resolution error if the user uses -Quiet; it's the same as a False result 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 a GetHostEntryWithCancel() method that factors in a cancellation token.

PR Context

Resolves #12147
Resolves #11160

PR Checklist

+ Add GetHostEntryWithCancel method and _cancel member.

+ Add cancellation for DNS lookups.
Comment on lines +62 to +64
It 'does not emit errors for an unresolvable address when using -Quiet' {
Test-Connection -Quiet -TargetName "fakeHost" -Count 1 | Should -BeFalse
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The test name is seem not right.

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) Mar 26, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I'll give it another pass, I missed a couple things here, it seems.

Co-Authored-By: Ilya <darpa@yandex.ru>
…onnection.Tests.ps1

Co-Authored-By: Ilya <darpa@yandex.ru>
protected override void StopProcessing()
{
_sender?.SendAsyncCancel();
_dnsLookupCancel?.Cancel();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

_dnsLookupCancel is never null

}
catch (PipelineStoppedException p)
{
throw p;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should use throw; here.

Maybe a conditional catch is better?

catch (Exception ex) when ((ex as PipelineStoppedException) == null)
{
    ....
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Didn't know that was a thing. Is there a significant difference?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 :)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

@daxian-dbw Dongbo Wang (daxian-dbw) Mar 27, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe name the method similarly to SendCancellablePing? GetCancellableHostEntry? 😄
Also, a minor thing, I recommend to pass in the cancellation token as parameter.

@vexx32 Rain Sallow (/u/ta11ow) (vexx32) Mar 26, 2020 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No worries, I'll sort it out. 🙂

@vexx32

Copy link
Copy Markdown
Collaborator Author

I think that should address all your comments Dongbo Wang (@daxian-dbw). 🙂

Let me know if I misunderstood anything; I'm still pretty new to fiddling with cancellation tokens and whatnot. ^^

@vexx32

Rain Sallow (/u/ta11ow) (vexx32) commented Mar 27, 2020 •

Copy link
Copy Markdown
Collaborator Author

🤔 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. 😊

+ 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.
…onnection.Tests.ps1

Co-Authored-By: Ilya <darpa@yandex.ru>

private static byte[]? s_DefaultSendBuffer;

private CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

From Codacy:

Suggested change
private CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource();
private readonly CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource();

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Huh, I thought I... ah, I must have taken it out again at some point. Thanks!

…nt/TestConnectionCommand.cs

Co-Authored-By: Ilya <darpa@yandex.ru>
@iSazonov Ilya (iSazonov) changed the title Test-Connection: Don't write DNS resolution errors on -Quiet Don't write DNS resolution errors on Test-Connection -Quiet Mar 28, 2020
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 28, 2020
@iSazonov Ilya (iSazonov) added this to the 7.1.0-preview.2 milestone Mar 28, 2020
@iSazonov
Ilya (iSazonov) merged commit dfe9955 into PowerShell:master Mar 28, 2020
@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test-Connection shows unwanted output when DNS not resolved on Powershell 7.0.0 Test-Connection only responds to stops in between pings

4 participants