question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scriptblock exceptions are not returned

See original GitHub issue

The following yields no output: Start-ThreadJob {throw "foo"} | Receive-Job -Wait

If you use Start-Job instead the exception is returned as a non-terminating error. Is there any way Start-ThreadJob could do the same?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
PaulHigincommented, Jan 26, 2018

@ajansveld Thanks for pointing this out! The reason this terminating error isn’t reported is because, unfortunately, PowerShell doesn’t currently provide a public or protected accessible way to report it as part of the job state change. I was aware of this when writing the code but neglected to include a TODO comment and mention this in the release notes. https://github.com/PaulHigin/PSThreadJob/blob/master/ThreadJob/ThreadJob/PSThreadJob.cs#L207

The only way I can see to fix this is to modify PowerShell API to provide protected class access to setting job state along with an optional exception (which would be the terminating exception). This is currently provided as internal access but derived classes such as ThreadJob cannot access the API.

I am sure we can get this change into PSCore (https://github.com/powershell/powershell) but then we would have compatibility issues with Windows PowerShell. But I guess we can have different versions of ThreadJob targeted to different versions of PowerShell.

There is another problem similar to this that I did mention in the release notes. And that is support for processing a $using: variable is also internal access to PowerShell, and so ThreadJob cannot support the using variable without first modifying PowerShell. I’ll create an issue for this as well.

0reactions
PaulHigincommented, Jun 21, 2018

I was able to fix this by using Job2 and implementing JobSourceAdapter.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scriptblock exceptions are not returned in object-events
When waiting for the job with Wait-Job (like suggested in this answer) the exception returns correctly, but I need a non-blocking approach. If ......
Read more >
About PowerShell Scriptblock non work problem
Hi All, I created PowerShell went to auto download adobe reader and auto install, but always show exitcode:87 My Script: ...
Read more >
Error message not helpful when missing scriptblock #498
The message that I got about an ampersand looks like some code fails to handle a very predictable exception. Can we please fix...
Read more >
Invoke-Command ScriptBlock issue
In summary I want to know how I can use variables in script block and such that command can be ... Exception] {...
Read more >
Powershell Script returning status code
When running an action PowerShell run script block it is always passing and not returning the status code in case of exception.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found