Skip to content

Conversation

@vexx32
Copy link
Collaborator

@vexx32 vexx32 commented Mar 25, 2020

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

vexx32 added 2 commits March 25, 2020 15:31
+ Add GetHostEntryWithCancel method and _cancel member.

+ Add cancellation for DNS lookups.
@ghost ghost assigned daxian-dbw Mar 25, 2020
@vexx32 vexx32 added WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module and removed WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module labels Mar 25, 2020
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
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.

Copy link
Collaborator Author

@vexx32 vexx32 Mar 26, 2020

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

Copy link
Member

@daxian-dbw daxian-dbw Mar 27, 2020

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

Copy link
Collaborator Author

@vexx32 vexx32 Mar 26, 2020

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
Collaborator Author

vexx32 commented Mar 27, 2020

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. ^^

@vexx32
Copy link
Collaborator Author

vexx32 commented Mar 27, 2020

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

@vexx32 vexx32 force-pushed the TestConnectionQuietness branch from 322c943 to 0e3665b Compare March 27, 2020 02:52
+ 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.
@vexx32 vexx32 force-pushed the TestConnectionQuietness branch from 0e3665b to 1088377 Compare March 27, 2020 02:58
…onnection.Tests.ps1

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

private static byte[]? s_DefaultSendBuffer;

private CancellationTokenSource _dnsLookupCancel = new CancellationTokenSource();
Copy link
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
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 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 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 iSazonov added this to the 7.1.0-preview.2 milestone Mar 28, 2020
@iSazonov iSazonov merged commit dfe9955 into PowerShell:master Mar 28, 2020
@ghost
Copy link

ghost commented Apr 23, 2020

🎉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