Skip to content

Conversation

@nxtn
Copy link
Contributor

@nxtn nxtn commented Feb 14, 2020

PR Summary

Fixes list enumeration and removal.

PR Context

A collection shall not be modified during enumeration.

PR Checklist

Copy link
Collaborator

@vexx32 vexx32 Feb 14, 2020

Choose a reason for hiding this comment

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

Is there a reason you're enumerating from the end to the beginning here?

Also, I don't see a real need to use a for loop here. For loops are nice, but tend to create confusing and less readable code on the whole.

Perhaps an alternative solution would be to attempt to stop each service, then filter out the stopped services with one of the List filter methods after the foreach loop is finished?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason you're enumerating from the end to the beginning here?

Enumerating from the end tends to move fewer items.

Also, I don't see a real need to use a for loop here. For loops are nice, but tend to create confusing and less readable code on the whole.

Sounds good. Updated to use RemoveAll (and TrueForAll for another method BTW).

Perhaps an alternative solution would be to attempt to stop each service

The services have been requested to stop before this filter method.

Copy link
Collaborator

@vexx32 vexx32 left a comment

Choose a reason for hiding this comment

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

Looks good to me overall! One minor thing you might want to double check just to be sure it won't cause any issues, but it doesn't seem like it should break anything as far as I can see.

Thanks for sorting this out! 😊

If there's a scenario you know was previously broken, it might be a good idea to add a test to ensure that scenario is completely fixed with this change and does not regress later on. 🙂

/// False if not all dependent services are stopped
/// </returns>
private bool HaveAllDependentServicesStopped(ICollection<ServiceController> dependentServices)
private bool HaveAllDependentServicesStopped(ServiceController[] dependentServices)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure what the possible impact of changing the method signature here would be, but given it's a private method anyway, it's probably safe to do as long as the code calling the method is still OK to call it with this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method is only called from one place, hence I believe it's separated to provide better readability.

@nxtn
Copy link
Contributor Author

nxtn commented Feb 15, 2020

If there's a scenario you know was previously broken, it might be a good idea to add a test to ensure that scenario is completely fixed with this change and does not regress later on.

It makes sense to cover these code paths. The tests will require a custom service, which refuses to accept the stop command during testing but accepts it when uninstalled...

I don't want to tackle that at this moment. :(

@vexx32
Copy link
Collaborator

vexx32 commented Feb 15, 2020

That does sound rather complicated, indeed.

The fix seems logically sound, though, so unless there's a more straightforward route to testing it, that might have to come later. 🙂

@iSazonov
Copy link
Collaborator

@NextTurn Please recover PR template to original and add a reference to an issue with a discussion.

@nxtn
Copy link
Contributor Author

nxtn commented Feb 15, 2020

Please recover PR template to original and add a reference to an issue with a discussion.

Opened #11858

Copy link
Collaborator

@iSazonov iSazonov left a comment

Choose a reason for hiding this comment

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

LGTM with one minor comment.

services.Remove(service);
}
}
services.RemoveAll(service =>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment why we have to use LINQ.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not LINQ. It's a method of List<T>.

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is just better. Please add a comment.

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Mar 13, 2020
@iSazonov iSazonov added this to the 7.1.0-preview.1 milestone Mar 13, 2020
internal void RemoveNotStoppedServices(List<ServiceController> services)
{
// You shall not modify a collection during enumeration.
// https://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to reference StackOverflow. A simple comment is enough that we cannot use an enumerator.

@iSazonov
Copy link
Collaborator

@TravisEz13 @daxian-dbw The PR is ready to merge.

@daxian-dbw daxian-dbw merged commit 175efca into PowerShell:master Mar 26, 2020
@daxian-dbw
Copy link
Member

@NextTurn Thank you for your contribution!

@nxtn nxtn deleted the enum branch March 26, 2020 23:23
@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.

5 participants