Golly Gosh Moments

You know that time you realized that you have been doing something the wrong way for a very long time and then finally realize the wrongness. For the sake of the low profanity rating of this blog, let’s call these golly gosh moments, although the Millennials might better understand #FML. Homer just says do’h.

So it’s just a normal day at the office, and I want to see if I can make an IP address lookup to get the approximate geo-location of a website visitor. I find an IP and it starts with 10 and turns out to be part of a private IP range. The next IP is the same. And the next.

To make a long story short, it turned out that we have been saving Heroku IP addresses in our logs instead of the user IP address for all our widget tracking for all of time. Heroku is a proxy, so the actual IP address is in an X-Forwarded-For header. For educational purposes here is how to make an express.js app behave better with a trusted proxy:

// App is an express.js app
app.enable('trust proxy');

// req.ip now contains the correct
// IP address during requests.

A one-liner made a world of difference for the logging. Golly Gosh.

The Notepad trick

There is a good reason Notepad has survived through all iterations of Windows. It is the best tool for clearing formatting!

For example, copying text from a webpage to Word will often keep the formatting (color, size and so on) but copying it to Notepad first and then re-copying it to the other editor clears the formatting completely.

I call this “The Notepad Trick” and I use it all the time so I thought it would be a good idea to pay homage to good ol’ Notepad.

Here is a direct copy between two Google Docs documents:

Google Docs copy text

Notice how the headline is the same size. Sometimes one might not want this. Here is the same copy with Notepad as a middle step:

Copy with Notepad as middle step

The formatting is all cleared, yay!

By the way, the trick is also described elsewhere.

Fixing an IIS deadlock

Found the below post in my drafts section. I should have released it a year ago but better late than never. Maybe someone with a deadlock problem on IIS and .NET 4.0 will find this piece of information useful.

At the place where I am contracting at the moment, we recently (April 2013) went live with an intranet web application that needs to handle about 1000 concurrent users. This is handled by 8 web-servers running Internet Information Services (IIS) 7.5 and ASP.NET 4.0 and in the beginning, all was well.

After running for about a day, we started seeing deadlocks in the IIS worker processes. At its worst, it happened every five minutes. When a worker process deadlocks, it recycles itself and the system could not handle that. A Microsoft support ticket was opened and the problem was eventually solved by adding the following code in a random .cs file in all of the assemblies (i.e. projects) for the application:

struct WorkaroundStruct
    public int myField1;

This magically solved the deadlocks. It turns out that it is a known bug in the .NET 4.0 framework but Microsoft was not going to make a hotfix for it. Hmm.

Deadlocks are always bad but in our case, they were extra bad. All data in the application is fetched through a Windows Communication Foundation (WCF) webservice from a different company. Instantiation of a WCF client can be slow and in our case, it could take 7-9 seconds for one or more of the WCF clients. By the way, while instantiating the client, the CPU goes nuts. Even though we cache the client for each session to improve performance, when the IIS enters a deadlock and resets, all 1000 users have to instantiate a new WCF client and the servers could simply not handle the CPU load.

It is funny what weird issues we run into with technology.

Unix tools on Windows

The other day, I set out on a journey to get many of the wonderful Unix tools running on Windows in something that resembles a terminal. In case you did not know, you can come a very long way by installing msysgit. It includes a terminal called Git Bash and all the common Unix tools such as sed, grep, awk, perl, find and so on. It also includes an ssh client and curl. I have been using this for about a year now and it is quite convenient when you are forced to work on a Windows machine. I know Windows has Powershell but… I just do not like it.

Git Bash