private static JArray GetThreadTraces(DeadlockException de)
        {
            using var dataTarget = DataTarget.CreateSnapshotAndAttach(Process.GetCurrentProcess().Id);

            var version = dataTarget.ClrVersions[0];
            var runtime = version.CreateRuntime();

            var ret     = new JArray();
            var threads = App.RunningDispatchers.Values.Append(App.BaseDispatcher).Where(d => de.ThreadNames.Contains(d.Thread.Name)).Select(d => d.Thread).Append(PacketAnalyzer.AnalysisThread);

            foreach (var thread in threads)
            {
                if (thread.Name == null)
                {
                    continue;
                }
                var runtimeThread = runtime.Threads.FirstOrDefault(t => t.ManagedThreadId == thread.ManagedThreadId);
                if (runtimeThread != null)
                {
                    ret.Add(new JObject
                    {
                        { "thread_name", thread.Name },
                        { "stack_trace", GetStackTraceClrmd(runtimeThread) }
                    });
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
        public async Task DeadlockDetection_2()
        {
            long baseGrainId = random.Next();
            long bBase       = 100;

            for (int i = 0; i < numIterations; i++)
            {
                long grainId = baseGrainId + i;
                IDeadlockNonReentrantGrain firstGrain = this.fixture.GrainFactory.GetGrain <IDeadlockNonReentrantGrain>(grainId);
                List <Tuple <long, bool> > callChain  = new List <Tuple <long, bool> >();
                callChain.Add(new Tuple <long, bool>(grainId, true));
                callChain.Add(new Tuple <long, bool>(bBase + grainId, true));
                callChain.Add(new Tuple <long, bool>(grainId, true));

                try
                {
                    await firstGrain.CallNext_1(callChain, 1);
                }
                catch (Exception exc)
                {
                    Exception baseExc = exc.GetBaseException();
                    this.fixture.Logger.Info(baseExc.Message);
                    Assert.Equal(typeof(DeadlockException), baseExc.GetType());
                    DeadlockException deadlockExc = (DeadlockException)baseExc;
                    Assert.Equal(callChain.Count, deadlockExc.CallChain.Count());
                }
            }
        }
Exemplo n.º 3
0
        public async Task DeadlockDetection_3()
        {
            long cBase = 200;

            for (int i = 0; i < numIterations; i++)
            {
                long grainId = i;
                IDeadlockReentrantGrain    firstGrain = GrainClient.GrainFactory.GetGrain <IDeadlockReentrantGrain>(grainId);
                List <Tuple <long, bool> > callChain  = new List <Tuple <long, bool> >();
                callChain.Add(new Tuple <long, bool>(cBase + grainId, false));
                callChain.Add(new Tuple <long, bool>(grainId, true));
                callChain.Add(new Tuple <long, bool>(cBase + grainId, false));
                callChain.Add(new Tuple <long, bool>(grainId, true));

                try
                {
                    await firstGrain.CallNext_1(callChain, 1);
                }
                catch (Exception exc)
                {
                    Exception baseExc = exc.GetBaseException();
                    logger.Info(baseExc.Message);
                    Assert.AreEqual(typeof(DeadlockException), baseExc.GetType());
                    DeadlockException deadlockExc = (DeadlockException)baseExc;
                    Assert.AreEqual(callChain.Count, deadlockExc.CallChain.Count());
                }
            }
        }
Exemplo n.º 4
0
            private NodePin LockInternal(NodePin parent, LockType ltype, NodeHandle child, bool ignoreHandleComparison)
            {
                CacheEntry entry = GetCache(child, false);

                LockType locked = NoLock;

                if (ltype == LockType.Read && entry.Lock.TryRead(base.Options.LockTimeout))
                {
                    locked = LockType.Read;
                }
                if (ltype != LockType.Read && entry.Lock.TryWrite(base.Options.LockTimeout))
                {
                    locked = ltype;
                }

                DeadlockException.Assert(locked != NoLock);
                try
                {
                    Node node = entry.Node;
                    if (node == null)
                    {
                        using (new SafeLock <DeadlockException>(entry))
                        {
                            node = entry.Node;
                            if (node == null)
                            {
                                if (!(
                                        Storage.TryGetNode(child.StoreHandle, out node, NodeSerializer) &&
                                        node != null &&
                                        ignoreHandleComparison?true:node.StorageHandle.Equals(entry.Handle.StoreHandle)))
                                {
                                    LoggerManager.Instance.IndexLogger.Error("BPlusTree", "FileId Mismatch, Node:" + node.StorageHandle.ToString() + ", Entry:" + entry.Handle.StoreHandle.ToString());
                                    throw new InvalidNodeHandleException();
                                }


                                Node old = Interlocked.CompareExchange(ref entry.Node, node, null);
                                Assert(null == old, "Collision on cache load.");
                            }
                        }
                    }
                    return(new NodePin(child, entry.Lock, ltype, locked, entry, node, null));
                }
                catch
                {
                    if (locked == LockType.Read)
                    {
                        entry.Lock.ReleaseRead();
                    }
                    else if (locked != NoLock)
                    {
                        entry.Lock.ReleaseWrite();
                    }
                    throw;
                }
            }
Exemplo n.º 5
0
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                NodeWithLock nlck = new NodeWithLock(null, LockFactory.Create());

                handle.SetCacheEntry(nlck);
                refobj = nlck;
                bool acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(nlck.Lock);
            }
Exemplo n.º 6
0
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                CacheEntry entry = GetCache(child, false);

                LockType locked = NoLock;

                if (ltype == LockType.Read && entry.Lock.TryRead(base.Options.LockTimeout))
                {
                    locked = LockType.Read;
                }
                if (ltype != LockType.Read && entry.Lock.TryWrite(base.Options.LockTimeout))
                {
                    locked = ltype;
                }

                DeadlockException.Assert(locked != NoLock);
                try
                {
                    Node node = entry.Node;
                    if (node == null)
                    {
                        using (new SafeLock <DeadlockException>(entry))
                        {
                            node = entry.Node;
                            if (node == null)
                            {
                                InvalidNodeHandleException.Assert(
                                    Storage.TryGetNode(child.StoreHandle, out node, NodeSerializer) &&
                                    node != null &&
                                    node.StorageHandle.Equals(entry.Handle.StoreHandle)
                                    );
                                Node old = Interlocked.CompareExchange(ref entry.Node, node, null);
                                Assert(null == old, "Collision on cache load.");
                            }
                        }
                    }
                    return(new NodePin(child, entry.Lock, ltype, locked, entry, node, null));
                }
                catch
                {
                    if (locked == LockType.Read)
                    {
                        entry.Lock.ReleaseRead();
                    }
                    else if (locked != NoLock)
                    {
                        entry.Lock.ReleaseWrite();
                    }
                    throw;
                }
            }
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                ILockStrategy lck;

                if (!child.TryGetCache(out lck))
                {
                    using (_lock.Write(base.Options.LockTimeout))
                    {
                        if (!_list.TryGetValue(child.StoreHandle, out lck))
                        {
                            _list.Add(child.StoreHandle, lck = LockFactory.Create());
                            child.SetCacheEntry(lck);
                        }
                    }
                }

                bool success;

                if (ltype == LockType.Read)
                {
                    success = lck.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    success = lck.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(success);
                try
                {
                    Node node;
                    success = Storage.TryGetNode(child.StoreHandle, out node, NodeSerializer);
                    Assert(success && node != null);

                    return(new NodePin(child, lck, ltype, ltype, lck, node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        lck.ReleaseRead();
                    }
                    else
                    {
                        lck.ReleaseWrite();
                    }
                    throw;
                }
            }
Exemplo n.º 8
0
            protected override NodePin Lock(NodePin parent, LockType ltype, NodeHandle child)
            {
                NodeWithLock nlck;

                if (!child.TryGetCache(out nlck))
                {
                    child.SetCacheEntry(nlck = new NodeWithLock(null, LockFactory.Create()));
                }

                bool acquired;

                if (ltype == LockType.Read)
                {
                    acquired = nlck.Lock.TryRead(base.Options.LockTimeout);
                }
                else
                {
                    acquired = nlck.Lock.TryWrite(base.Options.LockTimeout);
                }
                DeadlockException.Assert(acquired);
                try
                {
                    if (nlck.Node == null)
                    {
                        using (new SafeLock <DeadlockException>(nlck, base.Options.LockTimeout))
                            Storage.TryGetNode(child.StoreHandle, out nlck.Node, NodeSerializer);
                    }

                    Assert(nlck.Node != null);
                    return(new NodePin(child, nlck.Lock, ltype, ltype, nlck, nlck.Node, null));
                }
                catch
                {
                    if (ltype == LockType.Read)
                    {
                        nlck.Lock.ReleaseRead();
                    }
                    else
                    {
                        nlck.Lock.ReleaseWrite();
                    }
                    throw;
                }
            }
            public override ILockStrategy CreateLock(NodeHandle handle, out object refobj)
            {
                ILockStrategy lck;

                using (_lock.Write(base.Options.LockTimeout))
                {
                    if (!_list.TryGetValue(handle.StoreHandle, out lck))
                    {
                        _list.Add(handle.StoreHandle, lck = LockFactory.Create());
                        handle.SetCacheEntry(lck);
                    }
                }

                refobj = null;
                bool acquired = lck.TryWrite(base.Options.LockTimeout);

                DeadlockException.Assert(acquired);
                return(lck);
            }
        public void TestDeadlockExceptionSerialization()
        {
            void ThrowDeadlockException() => throw new DeadlockException(nameof(TestDeadlockExceptionSerialization), new InvalidOperationException("foo"));

            DeadlockException deadlockException = null;

            try { ThrowDeadlockException(); }
            catch (DeadlockException ex) { deadlockException = ex; }

            var formatter = new BinaryFormatter();
            var stream    = new MemoryStream();

            formatter.Serialize(stream, deadlockException);

            stream.Position = 0;
            var deserialized = (DeadlockException)formatter.Deserialize(stream);

            deserialized.Message.ShouldEqual(deadlockException.Message);
            deserialized.StackTrace.ShouldEqual(deadlockException.StackTrace);
            deserialized.InnerException.Message.ShouldEqual(deadlockException.InnerException.Message);
        }
Exemplo n.º 11
0
#pragma warning disable 618
        private static JObject /*Task<JObject>*/ GetThreadTraces(DeadlockException de)
        {
            using var dataTarget = DataTarget.CreateSnapshotAndAttach(Process.GetCurrentProcess().Id);

            var version = dataTarget.ClrVersions[0];
            var runtime = version.CreateRuntime();

            var ret     = new JObject();
            var threads = App.RunningDispatchers.Values.Append(App.BaseDispatcher).Where(d => de.ThreadNames.Contains(d.Thread.Name)).Select(d => d.Thread).Append(PacketAnalyzer.AnalysisThread);

            foreach (var thread in threads)
            {
                if (thread?.Name == null)
                {
                    continue;
                }
                var runtimeThread = runtime.Threads.FirstOrDefault(t => t.ManagedThreadId == thread.ManagedThreadId);
                if (runtimeThread != null)
                {
                    ret[thread.Name] = GetStackTraceClrmd(runtimeThread);
                }
                //ret[thread.Name] = GetStackTrace(thread).ToString();
            }
            //App.RunningDispatchers.Values.Append(App.BaseDispatcher).ToList().ForEach(d =>
            //{
            //    var t = d.Thread;
            //    t.Suspend();
            //    ret[t.Name] = new StackTrace(t, false).ToString();
            //    t.Resume();
            //});

            //if (PacketAnalyzer.AnalysisThread == null) return ret;
            //PacketAnalyzer.AnalysisThread.Suspend();
            //ret["Analysis"] = new StackTrace(PacketAnalyzer.AnalysisThread, false).ToString();
            //PacketAnalyzer.AnalysisThread.Resume();

            return(ret);
        }
Exemplo n.º 12
0
 public DeadlockWindow(DeadlockException ex)
 {
     DeadlockException = ex;
     InitializeComponent();
 }