Skip to content

Background jobs (Start-Job) and ForEach-Object -AsJob -Parallel, in combination with Debug-Job, prematurely report being in the Running state, before being ready for debugging #21258

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

Note: Thread jobs (Start-ThreadJob) are not affected (although for them too it must be manually ensured that the job is in the Running state). Curiously, though, the technologically closely related ForEach-Object -AsJob -Parallel is affected too.

  • Debug-Job requires a job to be in the Running (or AtBreakPoint) state in order for debugger invocation to succeed.
  • On startup, background jobs (Start-Job) report Running earlier than expected, causing Debug-Job to fail.
$jb = Start-Job {
  while ($true) { Start-Sleep -Milliseconds 100 }
}

try {

  Write-Verbose -Verbose 'Waiting for job to be in the ''Running'' state...'
  while ($jb.State -notin  'Running') { Start-Sleep -Milliseconds 100 }

  # If you enable this, the call works.
  # Start-Sleep -Milliseconds 500

  Write-Verbose -Verbose 'Starting debugging...'
  $jb | Debug-Job

}
finally {
  if ($jb) { $jb | Remove-Job -Force }
}

Expected behavior

The debugger should be entered.

Actual behavior

Debug-Job fails with the following error:

Debug-Job: /Users/jdoe/script.ps1:10
Line |
 274 |    $jb | Debug-Job
     |          ~~~~~~~~~
     | The provided job and all child jobs were examined but no jobs were found that could be debugged.  In order to debug a job or child job the job must support debugging and
     | also be in a running state.

Note that if you introduce an artificial delay (uncomment the Start-Sleep statement in the code), things work as expected.

Error details

Exception             :
    Type        : System.Management.Automation.PSInvalidOperationException
    ErrorRecord :
        Exception             :
            Type    : System.Management.Automation.ParentContainsErrorRecordException
            Message : The provided job and all child jobs were examined but no jobs were found that could be debugged.  In order to debug a job or child job the job must support
debugging and also be in a running state.
            HResult : -2146233087
        CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
        FullyQualifiedErrorId : InvalidOperation
    TargetSite  :
        Name          : ThrowTerminatingError
        DeclaringType : System.Management.Automation.MshCommandRuntime, System.Management.Automation, Version=7.5.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35
        MemberType    : Method
        Module        : System.Management.Automation.dll
    Message     : The provided job and all child jobs were examined but no jobs were found that could be debugged.  In order to debug a job or child job the job must support
debugging and also be in a running state.
    Source      : System.Management.Automation
    HResult     : -2146233079
    StackTrace  :
   at System.Management.Automation.MshCommandRuntime.ThrowTerminatingError(ErrorRecord errorRecord)
TargetObject          : Microsoft.PowerShell.Commands.DebugJobCommand
CategoryInfo          : InvalidOperation: (Microsoft.PowerShel…nds.DebugJobCommand:DebugJobCommand) [Debug-Job], PSInvalidOperationException
FullyQualifiedErrorId : DebugJobNoDebuggableJobsFound,Microsoft.PowerShell.Commands.DebugJobCommand
InvocationInfo        :
    MyCommand        : Debug-Job
    ScriptLineNumber : 274
    OffsetInLine     : 9
    HistoryId        : 2
    ScriptName       : /Users/jdoe/script.ps1
    Line             : $jb | Debug-Job

    Statement        : Debug-Job
    PositionMessage  : At /Users/jdoe/script.ps1:10 char:9
                       +   $jb | Debug-Job
                       +         ~~~~~~~~~
    PSScriptRoot     : /Users/jdoe
    PSCommandPath    : /Users/jdoe/script.ps1
    InvocationName   : Debug-Job
    CommandOrigin    : Internal
ScriptStackTrace      : at <ScriptBlock>, /Users/jdoe/script.ps1: line 10
                        at <ScriptBlock>, <No file>: line 1

Environment data

PowerShell 7.5.0-preview.1

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs-TriageThe issue is new and needs to be triaged by a work group.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions