Title: An attempt to modify a collection while it is being enumerated doesn't always result in an error · Issue #20181 · PowerShell/PowerShell · GitHub
Open Graph Title: An attempt to modify a collection while it is being enumerated doesn't always result in an error · Issue #20181 · PowerShell/PowerShell
X Title: An attempt to modify a collection while it is being enumerated doesn't always result in an error · Issue #20181 · PowerShell/PowerShell
Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerShell 5.1 and PowerShell. Steps to reprodu...
Open Graph Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
X Description: Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/20181
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"An attempt to modify a collection while it is being enumerated doesn't always result in an error","articleBody":"### Prerequisites\r\n\r\n- [X] Write a descriptive title.\r\n- [X] Make sure you are able to repro it on the [latest released version](https://github.com/PowerShell/PowerShell/releases)\r\n- [X] Search the existing issues.\r\n- [X] Refer to the [FAQ](https://github.com/PowerShell/PowerShell/blob/master/docs/FAQ.md).\r\n- [X] Refer to [Differences between Windows PowerShell 5.1 and PowerShell](https://learn.microsoft.com/powershell/scripting/whats-new/differences-from-windows-powershell).\r\n\r\n### Steps to reproduce\r\n\r\nAttempting modification of a collection while it is being enumerated should result in an _error_:\r\n* `An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.`\r\n\r\nThis error isn't consistently surfaced - note that this is a regression from Windows PowerShell:\r\n\r\n```powershell\r\n# OK: Reports an error for the attempt to modify the collection being enumerated.\r\n'--- foreach'\r\n$l = [System.Collections.Generic.List[string]] @('one', 'two')\r\nforeach ($e in $l) { $l.Remove($e) }\r\n\r\n# !! Fails QUIETLY in PS CORE as of v7.4.0-preview.5\r\n'--- pipeline'\r\n$l = [System.Collections.Generic.List[string]] @('one', 'two')\r\n$l | ForEach-Object { $l.Remove($_) }\r\n```\r\n\r\n**Important**: The quiet failure only occurs when the code is executed _via a script file_; if you paste the last two statements into an _interactive_ console, the error does surface.\r\n\r\n**[_Update_: NOT PowerShell's fault - see [below](https://github.com/PowerShell/PowerShell/issues/20181#issuecomment-1699515501)]** The following WinForms example affects Windows PowerShell too (both interactively and in a script):\r\n\r\n```powershell\r\nusing namespace System.Windows.Forms\r\nAdd-Type -Assembly System.Windows.Forms\r\n\r\n$f = [Form]::new()\r\n$f.Controls.AddRange((\r\n [CheckBox]::new(),\r\n [CheckBox]::new()\r\n))\r\n\r\n# !! Should fail with an error message, but quietly aborts after the first .Remove() call.\r\n$f.Controls | ForEach-Object { $f.Controls.Remove($_) }\r\n\r\n$f.Controls.Count # !! still nonzero\r\n```\r\n\r\n\r\n\r\n\r\n\r\n### Expected behavior\r\n\r\nFirst snippet:\r\n\r\n* Both the `foreach` and the `ForEach-Object` statement should report the expected error.\r\n\r\nSecond snippet:\r\n\r\n* The expected error should occur.\r\n\r\n\r\n### Actual behavior\r\n\r\nFirst snippet:\r\n\r\n* The `ForEach-Object` statement doesn't report an error and quietly aborts the pipeline after the first `.Remove()` call\r\n\r\nSecond snippet:\r\n\r\n* The expected error doesn't occur; quietly aborts the pipeline after the first `.Remove()` call.\r\n\r\n### Error details\r\n\r\n_No response_\r\n\r\n### Environment data\r\n\r\n```powershell\r\nPowerShell 7.4.0-preview.5\r\n```\r\n\r\n\r\n### Visuals\r\n\r\n_No response_","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2023-08-29T22:29:22.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":5},"url":"https://github.com/20181/PowerShell/issues/20181"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:79fb1d2a-805d-d558-d933-33a38e8a1539 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EAF2:2EC865:3FBCA:52500:6A5582EB |
| html-safe-nonce | 0536f220e0803483f2d86700ae8b7b3633111b1d4f5f36f6b663cbbb8ef8b04c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQUYyOjJFQzg2NTozRkJDQTo1MjUwMDo2QTU1ODJFQiIsInZpc2l0b3JfaWQiOiIyNDA0OTg2MjgzNzExODkwMTU1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 93e24e466baa82733875387c125cfbafc3118fc9215956647be449032a80f5c4 |
| hovercard-subject-tag | issue:1872587198 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/20181/issue_layout |
| twitter:image | https://opengraph.githubassets.com/445c597ff502fcc4758b6c51dc95fed31c7180d025b27af2e8632ef852f3af61/PowerShell/PowerShell/issues/20181 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/445c597ff502fcc4758b6c51dc95fed31c7180d025b27af2e8632ef852f3af61/PowerShell/PowerShell/issues/20181 |
| og:image:alt | Prerequisites Write a descriptive title. Make sure you are able to repro it on the latest released version Search the existing issues. Refer to the FAQ. Refer to Differences between Windows PowerSh... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5665c84831ed9ac4fb79519c16c9c5580ba8092fb8bb6e3e72972ec7197348e |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 3215163789b3dc58dba97bd78a30c20c949f477f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width