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

オープン
#10,793 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
powershell
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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

主要言語
PowerShell
スター
2.9k
フォーク
869
平均マージ
2日 4時間
マージ済み PR(30日)
38

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dataplat/dbatools のほかの issue

dataplat/dbatools の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。