Title: Allow for nested ThreadJobs by resetting the pool object · Issue #59 · PowerShell/ThreadJob · GitHub
Open Graph Title: Allow for nested ThreadJobs by resetting the pool object · Issue #59 · PowerShell/ThreadJob
X Title: Allow for nested ThreadJobs by resetting the pool object · Issue #59 · PowerShell/ThreadJob
Description: Summary of the new feature / enhancement ThreadJobs become a problem if you want to use them throughout a module; you have to be careful that you never create a ThreadJob within something else running within a ThreadJob, because it has a...
Open Graph Description: Summary of the new feature / enhancement ThreadJobs become a problem if you want to use them throughout a module; you have to be careful that you never create a ThreadJob within something else runn...
X Description: Summary of the new feature / enhancement ThreadJobs become a problem if you want to use them throughout a module; you have to be careful that you never create a ThreadJob within something else runn...
Opengraph URL: https://github.com/PowerShell/ThreadJob/issues/59
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Allow for nested ThreadJobs by resetting the pool object","articleBody":"### Summary of the new feature / enhancement\n\nThreadJobs become a problem if you want to use them throughout a module; you have to be careful that you never create a ThreadJob within something else running within a ThreadJob, because it has a global queue.\n\nIf you are executing any number of tasks over your throttle limit, and then start a new job within one of those, they can deadlock because the pool is full.\n\nSo something as simple as this will never return:\n\n```\n$jobs = 1..5 | %{ \n Start-ThreadJob { \n Start-Sleep -Seconds 1\n Start-ThreadJob { \n Start-Sleep -Seconds 1\n } | Wait-Job | Receive-Job\n } -Throttle 5\n}\n$jobs | Wait-Job\n```\n\nIdeally you should have the option to avoid this.\n\n### Proposed technical implementation details (optional)\n\nIf you add a Restart-ThreadJob cmdlet that will reset the pool, you can now control when you want to create a fresh pool. This guards against that kind of deadlock.\n\n```\n$jobs = 1..5 | %{ \n Start-ThreadJob { \n Start-Sleep -Seconds 1\n Restart-ThreadJob\n Start-ThreadJob { \n Start-Sleep -Seconds 1\n } | Wait-Job | Receive-Job\n } -Throttle 5\n}\n$jobs | Wait-Job\n```\n\nWorks. Of course, you now have 1 pool of 5 and another 5 pools of 5 (the default), but it doesn't hang. You get to control it.\n\n```\ndiff --git a/src/Microsoft.PowerShell.ThreadJob.psd1 b/src/Microsoft.PowerShell.ThreadJob.psd1\nindex 7b127d4..342009b 100644\n--- a/src/Microsoft.PowerShell.ThreadJob.psd1\n+++ b/src/Microsoft.PowerShell.ThreadJob.psd1\n@@ -44,7 +44,7 @@ number of jobs drops below the throttle limit.\n PowerShellVersion = '5.1'\n \n # Cmdlets to export from this module\n-CmdletsToExport = 'Start-ThreadJob'\n+CmdletsToExport = 'Start-ThreadJob', 'Restart-ThreadJob'\n \n # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.\n PrivateData = @{\ndiff --git a/src/code/Microsoft.PowerShell.ThreadJob.cs b/src/code/Microsoft.PowerShell.ThreadJob.cs\nindex c967e9d..ff45cf9 100644\n--- a/src/code/Microsoft.PowerShell.ThreadJob.cs\n+++ b/src/code/Microsoft.PowerShell.ThreadJob.cs\n@@ -142,6 +142,31 @@ namespace Microsoft.PowerShell.ThreadJob\n #endregion\n }\n \n+ [Cmdlet(VerbsLifecycle.Restart, \"ThreadJob\")]\n+ public sealed class RestartThreadJobCommand : PSCmdlet\n+ {\n+ #region Overrides\n+\n+ protected override void BeginProcessing()\n+ {\n+ base.BeginProcessing();\n+\n+ ThreadJob.Reset();\n+ }\n+\n+ protected override void ProcessRecord()\n+ {\n+ base.ProcessRecord();\n+ }\n+\n+ protected override void EndProcessing()\n+ {\n+ base.EndProcessing();\n+ }\n+\n+ #endregion\n+ }\n+\n public sealed class ThreadJobSourceAdapter : JobSourceAdapter\n {\n #region Members\n@@ -491,6 +516,8 @@ namespace Microsoft.PowerShell.ThreadJob\n \n private static ThreadJobQueue s_JobQueue;\n \n+ private static object _syncObject = new object();\n+\n #endregion\n \n #region Properties\n@@ -508,10 +535,18 @@ namespace Microsoft.PowerShell.ThreadJob\n \n #region Constructors\n \n+ public static void Reset()\n+ {\n+ lock (_syncObject)\n+ {\n+ s_JobQueue = new ThreadJobQueue(5);\n+ }\n+ }\n+\n // Constructors\n static ThreadJob()\n {\n- s_JobQueue = new ThreadJobQueue(5);\n+ Reset();\n if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))\n {\n Assembly assembly = typeof(PSObject).Assembly;\n@@ -806,7 +841,10 @@ namespace Microsoft.PowerShell.ThreadJob\n /// \u003cparam name=\"throttleLimit\">\u003c/param>\n public static void StartJob(ThreadJob job, int throttleLimit)\n {\n- s_JobQueue.EnqueueJob(job, throttleLimit);\n+ lock (_syncObject)\n+ {\n+ s_JobQueue.EnqueueJob(job, throttleLimit);\n+ }\n }\n \n /// \u003csummary>\n```\n\nIf you want to implement it in some better way, go ahead. This is just something.\n\n","author":{"url":"https://github.com/codykonior","@type":"Person","name":"codykonior"},"datePublished":"2026-02-21T14:52:54Z","url":"https://github.com/PowerShell/ThreadJob/issues/59"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:631561e4-48ff-a673-55bf-612544d2d82f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8A00:2D5588:3E030:524B6:6AFCD8B6 |
| html-safe-nonce | cc47c2f832ee5d7c1b3d5321a02f57271a2b24678c02e6835e9ba9aa7d049a1c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QTAwOjJENTU4ODozRTAzMDo1MjRCNjo2QUZDRDhCNiIsInZpc2l0b3JfaWQiOiI5OTYxODkyMjkxMjM2ODg2MzAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | f415836c92e3a28dcc67be7b2f55d098d5e4da8bf85b8681543bc6f9e8f72c62 |
| hovercard-subject-tag | issue:3972162861 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/ThreadJob/59/issue_layout |
| twitter:image | https://opengraph.githubassets.com/20f6b16a94bbce690d558bc910467afd94bc2e293e7018e55076131be613177a/PowerShell/ThreadJob/issues/59 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/20f6b16a94bbce690d558bc910467afd94bc2e293e7018e55076131be613177a/PowerShell/ThreadJob/issues/59 |
| og:image:alt | Summary of the new feature / enhancement ThreadJobs become a problem if you want to use them throughout a module; you have to be careful that you never create a ThreadJob within something else runn... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | codykonior |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5b7bc7e41e0a604bbdb5035c7ed9620c2d28b9c8a18747182e2262a75abfd91 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/ThreadJob git https://github.com/PowerShell/ThreadJob.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 265675369 |
| octolytics-dimension-repository_nwo | PowerShell/ThreadJob |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 265675369 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/ThreadJob |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ff5022cc3aa767e6115451d9c322dd975506d40e |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width