Invoke-Parallel - Its runspace pool on $Host hides the warnings and progress of the caller from API hosts

Abierto
#10,793 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
powershell
Área
backend

Línea de trabajo

Start with private/functions/flowcontrol/Invoke-Parallel.ps1, especially the runspace pool creation around line 450 and the EndInvoke handling around lines 293-298. Read the linked #10789 fix and compare options B and C, since the issue says a maintainer decision is needed before coding. Reproduce the stream behavior with a PowerShell API host and an isolated runspace; done means the chosen behavior preserves caller warnings and verbose messages and has coverage for a real caller such as Update-ServiceStatus.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

@potatoqualitee this one needs your decision before anyone writes code.

The problem

private/functions/flowcontrol/Invoke-Parallel.ps1 line 450 creates its runspace pool on the host of the caller:

$runspacepool = [runspacefactory]::CreateRunspacePool(1, $Throttle, $sessionstate, $Host)

This is the defect that #10789 fixes in Read-DbaBackupHeader and Start-/Stop-DbaDbEncryption (#10776, section 1). Once a thread of such a pool has run, the warnings, verbose messages and progress records of the calling pipeline no longer reach the streams of a host created through the PowerShell API: automation hosts, SDK hosts, notebooks, and our own tests that run a command in a runspace of its own. Output and errors still arrive. In a console nobody notices, because the records go to the console UI. The repro in #10776 shows it with ten lines and no dbatools; it behaves the same on 5.1 and 7.6.

The callers of Invoke-Parallel:

Caller When it runs in parallel
Install-DbaInstance (line 913) two or more computers
Update-DbaInstance (line 718) two or more computers
Start-/Stop-/Restart-DbaService through private Update-ServiceStatus (line 278) always
Invoke-DbaPfRelog (line 456) -Multithread

Through an API host this loses, for example, the progress bar of Invoke-Parallel itself and the warnings of Install-DbaInstance after the installations. Update-ServiceStatus is hit hardest: its threads return the results of the service actions, and the caller writes the warning for every service that failed ("($target) $($_.Service.Message)", line 281). So a script that restarts services through an API host never sees which ones failed.

Why this one is not a one-line fix

In #10789 the threads write nothing but output and errors, so the pool simply lost $Host there. Here some threads write more. Install-DbaInstance and Update-DbaInstance run Invoke-DbaAdvancedInstall and Invoke-DbaAdvancedUpdate in them, which draw progress bars and warn through Stop-Function, and so can the relog script of Invoke-DbaPfRelog. Today a console user sees those warnings and bars live, because they go through $Host. The service threads of Update-ServiceStatus do not import dbatools and only return objects, so for them dropping $Host loses nothing.

Invoke-Parallel forwards only the errors of a finished thread (lines 293-298, Write-Error -ErrorRecord). Its warnings, verbose messages, information and progress stay in the thread's streams. A lab probe in October (5.1 and 7.6, reading the screen buffer of a real console) showed:

  • Pool on $Host: the console shows the warnings, verbose messages and Write-Host of the threads, and the API caller loses its later warnings and verbose messages.
  • Pool without a host (CreateRunspacePool($sessionstate) plus SetMaxRunspaces($Throttle); there is no overload with minimum, maximum and session state but no host): the console shows nothing of the threads, and the caller keeps all its streams.
  • In both variants the thread's own Streams.Warning, Streams.Verbose and Streams.Progress are filled, so they can be forwarded.

Options

A. Keep $Host. Nothing changes for console users. API hosts keep losing the caller's warnings, verbose messages and progress after the first thread ran.

B. No host, and forward the thread's streams when the thread ends. Next to the errors, Invoke-Parallel writes the thread's warnings, verbose messages and information records in the caller's pipeline right after EndInvoke. All messages arrive, in every host. But warnings come when the computer is done instead of live, and the progress bars of the threads are no longer shown. The bar of Invoke-Parallel itself ("Installing SQL Server ... on n computers") stays. Small change, in one place.

C. No host, and forward while the threads run. Invoke-Parallel already polls its threads every SleepTimer milliseconds. On each round it would read the records each running thread added since the last round and write them in the caller's pipeline: warnings with Write-Warning, progress with Write-Progress. This is nearly live in every host. But it is more code, and the progress Ids need a mapping per thread: the threads draw with Write-ProgressHelper and Id 0 (or their own), so two threads would overwrite each other's bars and the bar of Invoke-Parallel. That is the Id ownership question of #10548.

My recommendation is B now. It closes the gap for API hosts, and nothing is lost except the live bars of the threads. C can follow with #10548, once bar ownership is settled. If the live bars of parallel installs matter to you, C right away.

Testing

As in #10789: run the command through [powershell]::Create() in a runspace of its own, and check that a warning written after the threads have run reaches Streams.Warning. Update-ServiceStatus is the cheapest real caller, because it needs no installation media: a service action that fails makes the caller warn after the threads ran. With B or C, also check that a warning written inside a thread arrives, for example with Invoke-DbaPfRelog -Multithread and a file it cannot read.


This text was created by Claude and reviewed by Andreas Jordan.

Lenguaje dominante
PowerShell
Estrellas
2.9k
Forks
869
Merge medio
2 d 4 h
PR fusionados (30 d)
38

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de dataplat/dbatools

Todos los issues de dataplat/dbatools

Issues similares

Más issues de Backend & API Design

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.