Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server stops with errors "Collection was modified after..." #1257

Closed
nightroman opened this issue Mar 25, 2024 · 2 comments · Fixed by #1258
Closed

Server stops with errors "Collection was modified after..." #1257

nightroman opened this issue Mar 25, 2024 · 2 comments · Fixed by #1258
Labels
Milestone

Comments

@nightroman
Copy link

Describe the Bug

Server stops with the error "Collection was modified after the enumerator was instantiated."

Steps To Reproduce

Steps to reproduce the behavior:

2024-03-25-server-stops.zip

(1) Start the attached server.ps1, rather trivial one route server.

(2) In the opened browser keep refreshing the page with brief processes details.
Alternatively, run the attached loop.ps1 which sends similar requests in a loop.

(3) At some point, sometimes soon or after a while, the server stops with errors like:

Test-PodeRestartPressed: C:\Program Files\WindowsPowerShell\Modules\Pode\2.9.0\Public\Core.ps1:194
Line |
 194 |  … t.IsCancellationRequested) -or (Test-PodeRestartPressed -Key $key)) {
     |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Collection was modified after the enumerator was instantiated.

Errors may be at different locations and about different keys tested.

Expected Behavior

Ideally, the error should not happen, with its reasons avoided.

If this is not possible errors should be caught and ignored.

Platform

  • OS: Windows 11
  • Browser: Chrome
  • Versions:
    • Pode: 2.9.0
    • PowerShell: 7.4.1
@mdaneri
Copy link
Contributor

mdaneri commented Mar 25, 2024

It's a bug due to the way Powershell deals with array and piping.

We have to make the Write-PodeTextResponse and Write-PodeHtmlResponse "piping compliance"

Try this one

Start-PodeServer {
    Add-PodeEndpoint -Address localhost -Port 8081 -Protocol Http
    #working
    Add-PodeRoute -Path '/processes'  -Method Get -ScriptBlock {
        $r = Get-Process | .{ process { if ($_.WS -gt 100mb) { $_ } } } |
            Select-Object Name, @{e = { [int]($_.WS / 1mb) }; n = 'WS' } |
            Sort-Object WS -Descending

        Write-PodeHtmlResponse -Value $r  -StatusCode 200
    }
    #not working
    Add-PodeRoute -Path '/processesPipedNotWorking'  -Method Get -ScriptBlock {
        Get-Process | .{ process { if ($_.WS -gt 100mb) { $_ } } } |
            Select-Object Name, @{e = { [int]($_.WS / 1mb) }; n = 'WS' } |
            Sort-Object WS -Descending | Write-PodeHtmlResponse  -StatusCode 200
    }

    #this is like the previous one
    Add-PodePage -Name NotWorkingProcesses -ScriptBlock {
        Get-Process | .{ process { if ($_.WS -gt 100mb) { $_ } } } |
            Select-Object Name, @{e = { [int]($_.WS / 1mb) }; n = 'WS' } |
            Sort-Object WS -Descending
    }

    Start-Process http://localhost:8081/processes
}

@mdaneri
Copy link
Contributor

mdaneri commented Mar 25, 2024

I found the real problem, and it's not related to the piping issue.
image

I'm going to open a bug for the piping issues with arrays

@Badgerati Badgerati linked a pull request Mar 29, 2024 that will close this issue
@Badgerati Badgerati added this to the 2.10.0 milestone Mar 29, 2024
Badgerati added a commit that referenced this issue Mar 29, 2024
Fix #1257 Collection was modified after..
@github-project-automation github-project-automation bot moved this from Backlog to Done in 🚀 Pode Roadmap Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

3 participants