如何使用Visual Studio调试器(.net编程)知道哪个线程拥有锁?

4

我只是使用简单的锁来管理一些共享数据。

有没有办法知道哪个线程获取了锁?

基本上,已经有人获取了锁并且没有释放它。因此,所有后续操作都会在获取锁时被阻塞并超时。

我有点陷入困境,因为从调试器中可以看到它只是停留在锁定状态,因为某个人已经获取了它,并且在"断点"之后查看了 "调试器 + Windows + 线程" - 没有线程进入锁定状态。

它不显示任何进入关键部分的线程。

有可能有人拿了锁并且该线程已被中止。但我期望即使线程已被中止,锁也会被释放。我的期望是错的吗?


这是我的类 - 基本上它是一个 PowerShell Cmd 运行程序,可以从多个线程执行命令:

internal abstract class PowerShellCommandRunner : IDisposable
{
    #region Fields
    protected object m_syncObject = new object();
    private PSSession m_psSession = null;
    private Runspace m_runspace = null;
    #endregion

    #region Constructor
    public PowerShellCommandRunner(ExchangeApplicationSystem system)
    {
        if (null == system)
        {
            throw new ArgumentNullException("ExchangeApplicationSystem");
        }
        this.ExchangeApplicationSystem = system;
        this.PSCredential = this.ExchangeApplicationSystem.Credential.GetPSCredential();
    }
    #endregion

    #region Properties


    internal ExchangeApplicationSystem ExchangeApplicationSystem
    {
        get;
        private set;
    }

    public PSCredential PSCredential
    {
        get;
        protected set;
    }

    private bool IsNotInitializedOrInvalidRunspace(Runspace runspace)
    {
        bool flag = (null == runspace) //not initialized
            || (null == runspace.RunspaceStateInfo) //not state info (defensive)
            || (runspace.RunspaceStateInfo.State == RunspaceState.Broken) //runspace state is broken
            || (null != runspace.RunspaceStateInfo.Reason); //there is an exception
        return flag;
    }

    private bool NeedToCreatePsSession
    {
        get
        {
            bool flag = (null == this.m_psSession)
                || this.IsNotInitializedOrInvalidRunspace(this.m_psSession.Runspace);
            return flag;
        }
    }

    internal Runspace Runspace
    {
        get
        {
            lock (this.m_syncObject)
            {
                if (this.IsNotInitializedOrInvalidRunspace(this.m_runspace))
                {
                    if (null != this.m_runspace)
                    {
                        //already have one runspace - close it, before we create another one
                        this.CloseRunspace();
                    }
                    this.m_runspace = RunspaceFactory.CreateRunspace();
                    this.m_runspace.Open();
                }
                return this.m_runspace;
            }
        }
    }
    #endregion

    #region Methods

    internal IEnumerable<PSObject> Execute(string cmd, params object[] argumentList)
    {
        if (string.IsNullOrEmpty(cmd))
        {
            throw new ArgumentNullException("cmd");
        }
        return this.Execute(new Command(cmd), argumentList);
    }

    /// <summary>
    /// Sub-classes can do their own specific implementation to create ps-sessions
    /// The base class simply performs primitive oepratiosn like managing them.
    /// </summary>
    /// <returns></returns>
    internal abstract PSSession GetPSSession();

    /// <summary>
    /// Gets the pssession and if reuired updates it as well
    /// </summary>
    /// <returns></returns>        
    private PSSession GetAndUpdateManagePSSessionInfRequired()
    {
        //Note: we dont need to lock as the callers (Exceute methods) will be acquiring the lock before executing the code
        //but, just locking it again as the locks are re-entrant
        lock (this.m_syncObject)
        {
            if (this.NeedToCreatePsSession)
            {
                if (null != this.m_psSession
                    && (null != this.m_runspace))
                {
                    //if ps-session exists, remove it from runspace
                    this.RemovePsSessionFromRunspace();
                    //Yes, there can be a case where some one already have a reference to the remove session
                    //that's ok, as the operation simply throws
                    //And subsequently they will be releasing and re-using the new one.
                    this.m_psSession = null;
                }
                //now, open a new session (requesting for a new session from subclasses)
                this.m_psSession = this.GetPSSession();
                Debug.Assert(null != this.m_psSession);
            }
            return this.m_psSession;
        }
    }

    internal IEnumerable<PSObject> Execute(Command cmd, params object[] argumentList)
    {
        if (null == cmd)
        {
            throw new ArgumentNullException("cmd");
        }
        lock (this.m_syncObject)
        {
            //Pipelines cannot be executed concurrently, so serialize it                
            OperationProgressReporter.Report(string.Format("Executing the following PowerShell Command: {0}", cmd.ToString()));
            return this.Runspace.ExecuteCommand(cmd, this.GetAndUpdateManagePSSessionInfRequired(), argumentList);
        }
    }

    internal IEnumerable<PSObject> Execute(ScriptBlock sb, params object[] argumentList)
    {
        if (null == sb)
        {
            throw new ArgumentNullException("scriptblock");
        }
        lock (this.m_syncObject)
        {
            //Pipelines cannot be executed concurrently, so serialize it                
            OperationProgressReporter.Report(string.Format("Executing the following PowerShell Command: {0}", sb.ToString()));
            return this.Runspace.ExecuteCommand(sb, this.GetAndUpdateManagePSSessionInfRequired(), argumentList);
        }
    }

    private void RemovePsSessionFromRunspace()
    {
        //not intended to call before acquiring a lock 
        //(For ex: either while closing runspace or while getting rid of old session and getting a new one (GetAndUpdateManagePSSessionInfRequired)
        //but locking it as the locks are re-entrant (defensive)
        lock (this.m_syncObject)
        {
            if ((null != this.m_psSession)
                && (null != this.m_runspace))
            {
                try
                {
                    string errorMsg = null;
                    this.m_runspace.RemovePsSessionFromRunspace(this.m_psSession, out errorMsg);
                    if (!string.IsNullOrEmpty(errorMsg))
                    {
                        FxTracing.TraceError(TraceEventId.GeneralError, errorMsg);
                    }
                }
                catch (Exception ex)
                {
                    ExceptionManager.GeneralExceptionFilter(ex);
                    OperationProgressReporter.Report(OperationProgressMessageLevel.Verbose,
                        string.Format("<DEBUG> Unable to remove PSsession from runspace in '{0}'", this.ExchangeApplicationSystem));
                }
            }
        }
    }

    private void CloseRunspace()
    {
        //lcok it
        lock (this.m_syncObject)
        {
            //check again to make sure only one thread enters
            if (null != this.m_runspace)
            {
                try
                {
                    //if a ps-session is created, remove it
                    if (null != this.m_psSession)
                    {
                        //remove the pssession from runspace
                        this.RemovePsSessionFromRunspace();
                    }
                    //then close the runspace
                    this.m_runspace.Close();
                }
                catch (Exception ex)
                {
                    //swallow
                    ExceptionManager.GeneralExceptionFilter(ex);
                    Debug.Fail(ex.Message);
                }
                finally
                {
                    //finally, set the runspace to null
                    //Yes, the runspace can be set to null while another thread can have a reference to old runspace
                    //its ok as the operation simply fail with invalid runspace state exception (most likely)
                    //And when they retry they get the updated runspace or get a new one.
                    //same appraoch as managing ps-session
                    this.m_runspace = null;
                }
            }
        }
    }
    #endregion

    #region IDisposable
    public void Dispose()
    {
        this.CloseRunspace();
    }
    #endregion
}

你能分享一下代码吗? - Mzf
请提供有关您的线程模型和锁的更多信息。最好提供代码。 - Mare Infinitus
1
你期望锁会被释放,即使持有它的线程被中止,这是合理的。任何未执行的 finally 块都会在线程被中止之前执行(请参阅 http://msdn.microsoft.com/en-us/library/ty8d3wta.aspx)。请注意,如果调用 .Abort() 的线程持有其他线程 finally 区域所需资源的锁,则可能会导致死锁。 - Kirill Shlenskiy
你显然对这段代码进行了相当深入的思考。但有一件事情引起了我的注意(这只是纯粹的猜测):你正在从许多由锁保护的代码区域调用 OperationProgressReporter.Report。这将潜在地执行一些外部代码。如果该代码恰好向另一个同步上下文分派同步消息,而该同步上下文反过来调用同一 PowerShellCommandRunner 实例上的某个方法或属性访问器(命中同一把锁),那么就会发生死锁。 - Kirill Shlenskiy
3个回答

3
也许这会有所帮助:
  1. 启用非托管代码调试(项目属性 > 调试 > 勾选“启用非托管代码调试”)
  2. 在某个位置设置断点
  3. 当执行断点时,在Immediate窗口中键入:.load sos,然后键入!SyncBlk -a

2

1
这是一个相当老的问题... 但我发现在VS 2022中,平行堆栈窗口将显示如下内容:

Parallel Stacks window showing locking thread

非常方便。谢谢VS团队!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接