darkoperator / darkoperator/Posh-SSH

The process cannot access the file .poshssh\hosts.json because it is being used by another process

Open
#494 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PowerShell
Stars
1.1k
Forks
222
PR merge metrics
No merged PRs in 30d

Description

When you run a lot of PowerShell objects concurrently which all use Posh-SSH to access different servers

` runThread = new Thread(() =>
{
if (_stop) return;
List openThrs = new List();

            foreach (var psslog in PSSLogs)
            {
                if (_stop) break;
                if (!cbOneByOne.Checked)
                {

                    filterByLog = string.Empty;
                    Thread ot = new Thread(() =>
                    {
                    if (_stop) return;

                        PowerShell ps = PowerShell.Create();


                    try
                    {




                        PSCommand cmd = ps.Commands.AddScript(
                  		scriptToRun.Script, true);

                            cmd.AddParameter("IP", psslog.Node.IP)
                            .AddParameter("Pass", psslog.Node.Password)
                            .AddParameter("Zip", binFile);

                            if (cbWithDataFile.Checked) {
                                cmd.AddParameter("DataZip", Path.GetFullPath($".\\GenerateData\\Data\\{psslog.Node.IP}.zip"));
                            }

                            cmd.AddParameter("ErrorAction", "Stop")
                            .AddParameter("Verbose");

                            ps.Runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "stop");

                            psslog.Ps = ps;

                            ps.Streams.Error.DataAdded += Error_DataAdded;
                            ps.Streams.Verbose.DataAdded += Verbose_DataAdded;
                            ps.Streams.Progress.DataAdded += Progress_DataAdded;
                            ps.Streams.Warning.DataAdded += Warning_DataAdded;
                            ps.Streams.Information.DataAdded += Information_DataAdded;

                            ps.Streams.Error.EnumeratorNeverBlocks = true;
                            ps.Streams.Verbose.EnumeratorNeverBlocks = true;
                            ps.Streams.Progress.EnumeratorNeverBlocks = true;
                            ps.Streams.Debug.EnumeratorNeverBlocks = true;
                            ps.Streams.Information.EnumeratorNeverBlocks = true;
                            ps.Streams.Warning.EnumeratorNeverBlocks = true;

                            ps.Invoke(null, psslog.psObjects);
                        }
                        catch (Exception ex)
                        {

                        }
                        finally
                        {
                            ps.Streams.Error.DataAdded -= Error_DataAdded;
                            ps.Streams.Verbose.DataAdded -= Verbose_DataAdded;
                            ps.Streams.Progress.DataAdded -= Progress_DataAdded;
                            ps.Streams.Warning.DataAdded -= Warning_DataAdded;
                            ps.Streams.Information.DataAdded -= Information_DataAdded;

                            ps.Dispose();
                           
                        }
                    });
                    ot.Start();
                    openThrs.Add(ot);

                    while (PSSLogs.Where(w => w.Ps != null && w.Ps.Streams.Progress.Count>0 && !w.Ps.Streams.Progress.Any(a=>a.PercentComplete>=100)).Count() >= delayMS)
                    {
                        Thread.Sleep(1000);
                    }
                }
                else
                {
                    if (_stop) break;

                    PowerShell ps = PowerShell.Create();

                    try
                    {
                        ps.Commands.AddScript(
            			scriptToRun.Script, false);

                        ps.AddParameter("IP", psslog.Node.IP);
                        ps.AddParameter("Pass", psslog.Node.Password);
                        ps.AddParameter("Zip", binFile);
                        if (cbWithDataFile.Checked)
                        {
                            ps.AddParameter("DataZip", Path.GetFullPath($".\\GenerateData\\Data\\{psslog.Node.IP}.zip"));
                        }

                        ps.AddParameter("ErrorAction", "Stop");
                        ps.AddParameter("Verbose");

                        ps.Runspace.SessionStateProxy.SetVariable("ErrorActionPreference", "stop");


                        psslog.Ps = ps;

                        ps.Streams.Error.DataAdded += Error_DataAdded;
                        ps.Streams.Verbose.DataAdded += Verbose_DataAdded;
                        ps.Streams.Progress.DataAdded += Progress_DataAdded;
                        ps.Streams.Warning.DataAdded += Warning_DataAdded;
                        ps.Streams.Information.DataAdded += Information_DataAdded;

                        ps.Streams.Error.EnumeratorNeverBlocks = true;
                        ps.Streams.Verbose.EnumeratorNeverBlocks = true;
                        ps.Streams.Progress.EnumeratorNeverBlocks = true;
                        ps.Streams.Debug.EnumeratorNeverBlocks = true;
                        ps.Streams.Information.EnumeratorNeverBlocks = true;
                        ps.Streams.Warning.EnumeratorNeverBlocks = true;


                        ps.Invoke(null, psslog.psObjects);

                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        ps.Streams.Error.DataAdded -= Error_DataAdded;
                        ps.Streams.Verbose.DataAdded -= Verbose_DataAdded;
                        ps.Streams.Progress.DataAdded -= Progress_DataAdded;
                        ps.Streams.Warning.DataAdded -= Warning_DataAdded;
                        ps.Streams.Information.DataAdded -= Information_DataAdded;

				ps.Dispose();
                    }
                }
                Thread.Sleep(scriptToRun.Name == "Install" ? 3000 : 250);
            }
        });
        runThread.Start();`

System.Management.Automation.ActionPreferenceStopException: The running command stopped because the preference variable "ErrorActionPreference" or common parameter is set to Stop: The process cannot access the file 'C:\Users\user1\.poshssh\hosts.json' because it is being used by another process.
at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection1 input, PSDataCollection1 output, PSInvocationSettings settings)
at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, IList`1 output)
at Scripter.Form1.DownloadFile(PSSLog psslog) in E:\user1\Projects\Scripter\Form1.cs:line 906
at Scripter.Form1.<>c__DisplayClass26_1.<btnRun_Click>b__1() in E:\user1\Projects\Scripter\Form1.cs:line 480

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing concurrent Posh-SSH operations and inspect access to .poshssh\hosts.json, focusing on the DownloadFile entry point in Form1.cs at line 906. The report's threaded invocation begins around line 480; done means concurrent operations no longer fail because hosts.json is locked.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.