#Google Analytic Tracker


Feb 7, 2018

Deadlock Again

Three years ago, our QA team was testing our latest web services (WCF). What they discovered was that after making some web service requests, the web service would no stop responding. Also, after each request, the web service memory usage would go up.  However, no one has a clue on why the web service would stop responding. So on that morning, I spent an hour to figure out what was going on.

Great, it is another deadlock!

In the world of software development, multi-threaded programming is probably one of the best strategies making your software performance scale. However, troubleshooting deadlock is far from trivial. The more complicated the software, the higher the chance you run into deadlocks if you choose to do multi-thread programming.

Chances are, even if you think you were writing a single thread program,  the frameworks or libraries that your code calls could execute their instructions on another thread.

There are many guidelines on the internet shows you how to avoid deadlocks.  Notice I said the word "guidelines," there aren't any specific pattern or algorithm that we can copy to ensure your multi-thread program never run into deadlocks. Every application is different, and you need to understand how your code is executed to troubleshoot deadlock issues.

Deadlock Scenarios

I am sure there are many ways your application get a deadlock. Here are the common ones that I often encountered.

Single Application internal deadlock

  • Deadlock occurs within the application code itself. This is the classic deadlock that you probably learned from your computer science course.

2018-02-08 21_21_13-Thead Deadlock Diagram.vsdx - Visio Professional

Single Application Lock Combine with Table Access Deadlock

  • Application Thread 1 performed Insert/Update on database TableA, but has not commit yet, because it was waiting to access a memory lock. Meanwhile, Thread 2 has acquired this memory lock, but it is waiting to on TableA to be unlocked.
  • To make this scenario more complicated, any database tables that have foreign key constraints linking to TableA could potentially cause a deadlock in chained relationships.

2018-02-08 21_38_22-Thead Deadlock Diagram.vsdx - Visio Professional

Single Application’s multiple DB Transactional deadlock

  • Application Thread 1 performed insert/update on database TableB, and then waiting for access to TableA. Meanwhile, Thread 2 has already locked on TableA, and is waiting for TableB to unlock.
  • Note that this occurs only when an application uses multiple database connections or database transactions with different threads. There is no application memory lock involved either.

2018-02-08 21_45_10-Thead Deadlock Diagram.vsdx - Visio Professional

Multiple Applications DB Transactional deadlock

  • Application1 performed insert/update on database TableA, and it tried to access TableB, but Application2 already has a lock on TableB, but it is also stuck on waiting for TableA.

2018-02-08 21_51_46-Thead Deadlock Diagram.vsdx - Visio Professional

Tips to Avoid Deadlocks

  • Pick the Right Framework
    Try to use framework that does not require you to worry about locks that fits your application’s needs.
    For example, LMAX Disruptor uses ring buffer to avoid using locks, and yet it provides high message process throughput.
  • Avoid Waiting on Locks Forever!
    For example, in C#, instead of using lock(), or Monitor.Enter(), try using the following locks that allow you to stop waiting:


  • Use the Framework in a Thread-Safe Manner.
    This is a reminder for the developers to always check how to use a framework properly.  Most of the library operations that we are not thread safe.
    For example, did you know that if you use the same Entity Framework context object is thread-safe when access by multiple threads?
  • Choose to use a Thread-Safe Class
    For example, pick ConcurrentDictionary<TKey, TValue> over the standard Dictionary<TKey, TValue>.
  • Follow Coding Standard and Best Practice
    For example, in the project that I worked on, developers are required to a custom Data Access Manager that can detect possible deadlock scenario if multiple threads access the same database transaction object, or a thread has more than one opened database transactions. It would then throw an exception in debug mode.
  • Test, Analysis, Fix, and Test Again
    There are many ways to hit a deadlock. So make sure you test your code before putting it to production.

Lastly, remember there is always a trade off when you pick one design over the other design. Always consider about what are the priority of your software characteristics before you pick the design.

Jan 2, 2015

Web Browser Got Hijacked by www.2345.com

I rarely write a blog these days due to my busy family life. So, this would be my first blog for 2015, and it is not related to programming. Yesterday I ran into a very annoying problem. I am not sure when it had happened because this happens on my parent's PC, but when whenever I start a web browser (IE, and Chrome), it always redirect me to www.2345.com.

I knew the homepage is hijacked, but how! Most likely my parents accidentally installed something when they are looking at some China's website. I just want to get rid of it. To ensure the PC isn't infected with any viruses, I ran the typical MS Security Essential, and IObit Advance System Care to see if the issue can be removed automatically. None of these tools fixed it.

After Googling a bit, I found this YAC has a guide on how to manually fix it. I even installed YAC but it didn't fix the issue.

I suspect a Windows service or some installed Chinese software is causing that.  After digging around and try to uninstall a number of toolbars and Chinese software, not only it didn't fix the program. I notice additional software where installed:
I found the following in c:\Users\MyUserName\AppData\Roaming" after sorting the file by last Create Date:

ËÙÀËÊäÈë·¨ (probably in Big 5 encoding)

In addition, I found these suspicious folder:

Afterward, something did change after I restarted my computer. I notice a Chinese PingYin (速浪输入法) input keyboard running on my system. In addition, there is a file call SogouPinyin.local (related to 搜狗拼音输入法) sitting in my c:\Users\MyUserName\AppData\Roaming, and this is what the file contains:



It is definitely something related to the issue that I am facing. In fact, looking at all these weird naming .exe, it seems very suspicious the computer is infected with malwares. Not sure how this configuration file is access, but it is likely being use when the input keyboard is initialized. So removed the Chinese input.

In addition, I manually deleted the AppData/Roaming folders to see if these files continue to be accessed somehow. So far the system seems to be running fine.

Afterward, I manually delete the www.2345.com links argument from the Chrome and IE shortcut icons, because they were altered.

So far, this seems to have fixed the issue. I still have to figure out how to remove the keyboard layout entry from Windows.

This may not be a virus or malware, but it definitely can lead someone to visit unsafe web pages.

Oct 25, 2013

Setting up TFPT Search

The TFS Power Tool comes with a very useful changeset search tool. This tool is not in Visual Studio 2012, maybe it will be in Visual Studio 2013 or in the feature.

Since every time when I install a new OS, I forget how to set one up. So here is how to do it.

  1. On the desktop, create a shortcut.
  2. Browse for the following exe:
  3. "C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\TFPT.EXE" searchcs"
    1. Target field "C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools\TFPT.EXE" searchcs"
    2. Start in: "C:\Program Files (x86)\Microsoft Team Foundation Server 2012 Power Tools"
  4. Make sure the following path is in your System Environment Variable Path:
    "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"
  5. Note: Make sure your Server Name has NO SPACE, use %20 for space, this throw me off many times because the search tool will able to find the changeset, but when you try to open the changeset details, it won't show anything. Most like because when it invoke tf.exe, it doesn't warp the server name argument parameter with quotations.

Download TFS Power Tools from Official Microsoft Download Center