Skip to content

Commit

Permalink
Cleanup: ThreadAbortException is now in .Net Core 3.0 (PowerShell#10230)
Browse files Browse the repository at this point in the history
* Cleanup: ThreadAbortException is now in .Net Core 3.0

* Fix CodeFactor issue

* Remove ThreadAbortException

* Remove ThreadAbortException relared code
  • Loading branch information
iSazonov authored Aug 1, 2019
1 parent c37dd91 commit 973156d
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2023,10 +2023,6 @@ public Job2 Run()
{
ex = e;
}
catch (System.Threading.ThreadAbortException e)
{
ex = e;
}
catch (IOException e)
{
ex = e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,18 +625,11 @@ private void InvokeThreadProc()
SetPipelineState(PipelineState.Failed, ex);
SetHadErrors(true);
}
#if !CORECLR // No ThreadAbortException In CoreCLR
catch (ThreadAbortException ex)
{
SetPipelineState(PipelineState.Failed, ex);
SetHadErrors(true);
}
#endif
// 1021203-2005/05/09-JonN
// HaltCommandException will cause the command
// to stop, but not be reported as an error.
catch (HaltCommandException)
{
// 1021203-2005/05/09-JonN
// HaltCommandException will cause the command
// to stop, but not be reported as an error.
SetPipelineState(PipelineState.Completed);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,6 @@ internal void SetHadErrors(bool status)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the pipeline was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// Pipeline.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -568,9 +565,6 @@ public Collection<PSObject> Invoke()
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the pipeline was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// Pipeline.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down
30 changes: 0 additions & 30 deletions src/System.Management.Automation/engine/hostifaces/PowerShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2258,9 +2258,6 @@ internal void InvokeWithDebugger(
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2320,9 +2317,6 @@ public Collection<PSObject> Invoke()
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2385,9 +2379,6 @@ public Collection<PSObject> Invoke(IEnumerable input)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2447,9 +2438,6 @@ public Collection<PSObject> Invoke(IEnumerable input, PSInvocationSettings setti
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2513,9 +2501,6 @@ public Collection<T> Invoke<T>()
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2580,9 +2565,6 @@ public Collection<T> Invoke<T>(IEnumerable input)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2650,9 +2632,6 @@ public Collection<T> Invoke<T>(IEnumerable input, PSInvocationSettings settings)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2715,9 +2694,6 @@ public void Invoke<T>(IEnumerable input, IList<T> output)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -2789,9 +2765,6 @@ public void Invoke<T>(IEnumerable input, IList<T> output, PSInvocationSettings s
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down Expand Up @@ -4457,9 +4430,6 @@ internal void SetIsNested(bool isNested)
/// because the current CLR permissions do not allow adequate
/// reflection access to a cmdlet assembly.
/// </exception>
/// <exception cref="ThreadAbortException">
/// The thread in which the command was executing was aborted.
/// </exception>
/// <exception cref="RuntimeException">
/// PowerShell.Invoke can throw a variety of exceptions derived
/// from RuntimeException. The most likely of these exceptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,6 @@ public override string ToString()
/// The jump needs to execute both finally blocks, the first one on stack level 4 the
/// second one on stack level 2. So, it needs to jump the first finally block, pop 2 items from the stack,
/// run second finally block and pop another 2 items from the stack and set instruction pointer to label L.
///
/// Goto also needs to rethrow ThreadAbortException iff it jumps out of a catch handler and
/// the current thread is in "abort requested" state.
/// </summary>
internal sealed class GotoInstruction : IndexedBranchInstruction
{
Expand Down Expand Up @@ -302,9 +299,6 @@ internal static GotoInstruction Create(int labelIndex, bool hasResult, bool hasV

public override int Run(InterpretedFrame frame)
{
// Are we jumping out of catch/finally while aborting the current thread?
Interpreter.AbortThreadIfRequested(frame, _labelIndex);

// goto the target label or the current finally continuation:
return frame.Goto(_labelIndex, _hasValue ? frame.Pop() : Interpreter.NoValue, gotoExceptionHandler: false);
}
Expand Down Expand Up @@ -387,15 +381,6 @@ public override int Run(InterpretedFrame frame)
ExceptionHandler exHandler;
frame.InstructionIndex += _tryHandler.GotoHandler(frame, exception, out exHandler);
if (exHandler == null) { throw; }
#if !CORECLR // Thread.Abort and ThreadAbortException are not in CoreCLR.
// stay in the current catch so that ThreadAbortException is not rethrown by CLR:
var abort = exception as ThreadAbortException;
if (abort != null)
{
Interpreter.AnyAbortException = abort;
frame.CurrentAbortHandler = exHandler;
}
#endif
bool rethrow = false;
try
{
Expand Down Expand Up @@ -602,8 +587,6 @@ internal static LeaveExceptionHandlerInstruction Create(int labelIndex, bool has

public override int Run(InterpretedFrame frame)
{
// CLR rethrows ThreadAbortException when leaving catch handler if abort is requested on the current thread.
Interpreter.AbortThreadIfRequested(frame, _labelIndex);
return GetLabel(frame).Index - frame.InstructionIndex;
}
}
Expand Down Expand Up @@ -640,11 +623,7 @@ private LeaveFaultInstruction(bool hasValue)

public override int Run(InterpretedFrame frame)
{
// TODO: ThreadAbortException ?

object exception = frame.Pop();
// ExceptionHandler handler;
// return frame.Interpreter.GotoHandler(frame, exception, out handler);
throw new RethrowException();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ internal sealed class InterpretedFrame
public int StackIndex;
public int InstructionIndex;

// When a ThreadAbortException is raised from interpreted code this is the first frame that caught it.
// No handlers within this handler re-abort the current thread when left.
public ExceptionHandler CurrentAbortHandler;

internal InterpretedFrame(Interpreter interpreter, StrongBox<object>[] closure)
{
Interpreter = interpreter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,9 @@
*
* ***************************************************************************/

#if !CLR2
using System.Collections.Generic;
using System.Linq.Expressions;
#else
using Microsoft.Scripting.Ast;
#endif

using System.Runtime.CompilerServices;
using System.Threading;

using System.Diagnostics;
using System.Collections.Generic;

namespace System.Management.Automation.Interpreter
{
Expand Down Expand Up @@ -113,51 +105,5 @@ public void Run(InterpretedFrame frame)
frame.InstructionIndex = index;
}
}

/// <summary>
/// To get to the current AbortReason object on Thread.CurrentThread
/// we need to use ExceptionState property of any ThreadAbortException instance.
/// </summary>
[ThreadStatic]
internal static ThreadAbortException AnyAbortException = null;

/// <summary>
/// If the target that 'Goto' jumps to is inside the current catch block or the subsequent finally block,
/// we delay the call to 'Abort' method, because we want to finish the catch/finally blocks.
/// </summary>
internal static void AbortThreadIfRequested(InterpretedFrame frame, int targetLabelIndex)
{
var abortHandler = frame.CurrentAbortHandler;
var targetInstrIndex = frame.Interpreter._labels[targetLabelIndex].Index;
if (abortHandler != null &&
!abortHandler.IsInsideCatchBlock(targetInstrIndex) &&
!abortHandler.IsInsideFinallyBlock(targetInstrIndex))
{
frame.CurrentAbortHandler = null;

var currentThread = Thread.CurrentThread;
if ((currentThread.ThreadState & System.Threading.ThreadState.AbortRequested) != 0)
{
Debug.Assert(AnyAbortException != null);

// The current abort reason needs to be preserved.
#if SILVERLIGHT
currentThread.Abort();
#else
currentThread.Abort(AnyAbortException.ExceptionState);
#endif
}
}
}

internal int ReturnAndRethrowLabelIndex
{
get
{
// the last label is "return and rethrow" label:
Debug.Assert(_labels[_labels.Length - 1].Index == RethrowOnReturn);
return _labels.Length - 1;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,6 @@ private JobSourceAdapter GetJobSourceAdapter(JobDefinition definition)
{
ex = e;
}
catch (ThreadAbortException e)
{
ex = e;
}

if (ex != null)
{
Expand Down

0 comments on commit 973156d

Please sign in to comment.