Skip to content

Commit

Permalink
Refactor RunSpaceOpening to allow for controlled disposal.
Browse files Browse the repository at this point in the history
Moves RunspaceOpening to the ConnectionBase class, so that it has
complete control over the instance.
  • Loading branch information
hangy committed Oct 3, 2022
1 parent a7a4929 commit a8be2e5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ private void CoreClose(bool syncCall)
Monitor.Exit(SyncRoot);
try
{
RunspaceOpening.Wait();
_runspaceOpening.Wait();
}
finally
{
Expand Down Expand Up @@ -685,7 +685,7 @@ public RunspaceEventQueueItem(RunspaceStateInfo runspaceStateInfo, RunspaceAvail
}

// This is to notify once runspace has been opened (RunspaceState.Opened)
internal ManualResetEventSlim RunspaceOpening = new ManualResetEventSlim(false);
private readonly ManualResetEventSlim _runspaceOpening = new ManualResetEventSlim(false);

/// <summary>
/// Set the new runspace state.
Expand Down Expand Up @@ -735,6 +735,16 @@ protected void SetRunspaceState(RunspaceState state)
this.SetRunspaceState(state, null);
}

/// <summary>
/// Set the current runspace state to <see cref="RunspaceState.Opened"/>
/// and signals <see cref="_runspaceOpening"/> - no error.
/// </summary>
protected void SetRunspaceStateOpened()
{
SetRunspaceState(RunspaceState.Opened);
_runspaceOpening.Set();
}

/// <summary>
/// Raises events for changes in runspace state.
/// </summary>
Expand Down Expand Up @@ -1578,5 +1588,48 @@ internal override SessionStateProxy GetSessionStateProxy()
}

#endregion session state proxy

#region IDisposable Members

/// <summary>
/// Set to true when object is disposed.
/// </summary>
private bool _disposed;

/// <summary>
/// Protected dispose which can be overridden by derived classes.
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
try
{
if (_disposed)
{
return;
}

lock (SyncRoot)
{
if (_disposed)
{
return;
}

_disposed = true;
}

if (disposing)
{
_runspaceOpening.Dispose();
}
}
finally
{
base.Dispose(disposing);
}
}

#endregion IDisposable Members
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,7 @@ private void DoOpenHelper()
throw;
}

SetRunspaceState(RunspaceState.Opened);
RunspaceOpening.Set();
SetRunspaceStateOpened();

// Raise the event
RaiseRunspaceStateEvents();
Expand Down Expand Up @@ -1226,11 +1225,6 @@ protected override void Dispose(bool disposing)
_jobManager = null;
_jobRepository = null;
_runspaceRepository = null;
if (RunspaceOpening != null)
{
RunspaceOpening.Dispose();
RunspaceOpening = null;
}

// Dispose the event manager
if (this.ExecutionContext != null && this.ExecutionContext.Events != null)
Expand Down

0 comments on commit a8be2e5

Please sign in to comment.