S h o r t S t o r i e s

// Tales from software development

Archive for June 2010

What to do when a Windows Service hangs during startup

leave a comment »

I have a Windows Service that queries a database during startup and found that after deploying the final release version it failed to start. Worse still, the status in the Services console was shown as ‘Starting’ and it couldn’t be stopped or started again. The service’s log file was also locked.

After running the query interactively I realised that a configuration change in the database was causing it to fail. This was easily fixed but I still had the problem of the hung service.

It was beginning to look like the server would have to be rebooted but quick search on the internet suggested the following:

1. Use ‘sc queryex <service-name>’ to get the process id (PID) of the service.

2. Use ‘taskkill /pid <pid> /F’ to terminate the service process.

Written by Sea Monkey

June 17, 2010 at 8:00 pm

Posted in Environments

Tagged with

Stop messing about with StyleCop!

leave a comment »

StyleCop was developed by Jason Allor at Microsoft and was originally an internal-only tool. I worked on a project at Microsoft that was eventually outsourced and one of the problems that arose was that StyleCop had to be removed from the build system that we handed over because Microsoft would not allow it ‘to go out of the building’.

Ironically, very shortly after this, someone at Microsoft changed their mind and StyleCop was made publically available on Microsoft’s web site but rebranded as Microsoft Source Analysis. This caused a lot of confusion and it wasn’t long before it was renamed back to StyleCop causing almost as much confusion again.

Now StyleCop is in the process of being moved off Microsoft’s web site to the CodePlex web site. Currently, if you want a version of StyleCop that will work with Visual Studio 2005 then you need to get 4.3.3.0 from Microsoft’s web site but if you want the latest beta for Visual Studio 2010 then you’ll need to get it off CodePlex.

StyleCop is one of the best code tools available and doesn’t deserve this kind of mismanagement. Someone at Microsoft needs to get a grip and to stop messing about it. Maybe this is what has finally happened and, hopefully, CodePlex will be a stable home for the tool but it really needs to support Visual Studio 2005 as well as 2008 and 2010.

Written by Sea Monkey

June 14, 2010 at 8:00 pm

Posted in Comment, Development

Tagged with ,

The RAF Sector Clock

leave a comment »

With the 70th anniversary of the start of the Battle of Britain I thought it was a good time to write about something related that’s provides an insight into how the RAF’s command and control system worked: the RAF Sector Clock.

RAF Sector Clock

The Sector Clock is a standard RAF clock of the 1930s and 1940s made by FW Elliot but it has a modified face that shows the space between each hour as a different coloured triangle. Only three colours, red, blue, and yellow, are used and they alternate around the circumference of the face. The clock was used in the sector control rooms where each day’s aircraft movements were plotted on a map table.

As each piece of information about Luftwaffe movements arrived it was added to the plot table using a wooden block that showed the approximate number of enemy aircraft, their height, and a colour that corresponded to the colour currently indicated by the minute hand of the Sector Clock.

This allowed the controllers to immediately identify how old the information on the table was but the most interesting aspect of this method was that as the minute hand of the clock entered a each coloured triangle, any plots on the table with the corresponding colour were removed. Any plots on the board with the same colour must have been placed at least 10 minutes earlier.

The principle was a simple enough: out of date information is worse than no information at all. It’s interesting that this principle was established before the computer age and what we think of as the era of information theory.

Written by Sea Monkey

June 11, 2010 at 8:00 pm

Posted in Comment

Tagged with

MySQL Connector/NET hangs on MySQLDataReader.Read()

with one comment

This is a curious problem that I haven’t had the time to investigate fully but I’ve done enough to find a workaround…

The project I’m working is for managing patient data and is nearing completion. It takes patient data from a number of source systems although there are two main ones.¬†Unfortunately, it’s just been discovered that the surnames of the patients in these two source systems don’t always match. We needed to understand why and the first step was to quantify the problem by comparing the surnames in the two systems to establish how many were different.

This was complicated by the fact that while one system used a MySQL database that could be queried directly the other system implemented a web services interface that only allowed a single patient to be queried at a time.

So, the solution required a SQL query to be executed against the database to retrieve all patients and then each row was processed by calling the web services method to get the corresponding patient on the other system. The surnames were compared and if they were different a message was written to a log file.

It was a simple enough bit of code, no more than about 50 lines of C# in total but it continually failed. Worse still, it didn’t throw an exception and exit, it just hung. It wasn’t possible to use the Visual Studio IDE to debug the code either because the program had to run in a production environment.

After the third attempt it became obvious that it was hanging at exactly the same point in the execution. After about 7 minutes and when it was processing patient surnames beginning with ‘CRAIG’. I added some trace output code and it appeared that the program was hanging in the loop where it read from the MySQLDataReader and then called the method that checked the surnames:

    while (reader.Read())
    {
        this.CompareLastName(reader.GetString("PatientID"), reader.GetString("LastName"));
    }

 
It was one of those moments when you begin to question your grip on reality – how could this code possibly hang ? I could understand that it might throw an exception but why would it just hang ?

This code was implemented using MySQL Connector/NET 6.0.3 which had been used extensively elsewhere in the project with only a couple of minor issues.

The more investigation I did the more it was clear that this was exactly where the problem was and it seemed to be time related – at least it always hung at the same stage of execution.

Finally, out of sheer desperation and a looming deadline, I changed the code to populate a Dictionary instead of calling the CompareLastName() method and then coded a foreach block to process the data in the Dictionary. That worked perfectly.

Once I had the results and was able to provide them to the teams responsible for maintaining the two source systems I was able to spend an hour or so trying to figure out why the original code didn’t work. The first thing I did was to pepper the code with trace output commands so I could see exactly where it was failing. It turned out that it was the MySQLDataReader.Read() method but, strangely, once the trace output methods were inserted, the method threw an exception rather than just hanging. The exception message was:

“Connection unexpectedly terminated.”

Not terribly helpful but better than nothing. I checked the default command timeout in the MySQL Connector/NET documentation – it’s 30 seconds. But the program was hanging after 7 minutes of execution. So, the problem wasn’t how long the connection had been open but how long it had been left idle. I’m guessing here but I think the problem was caused by one particular web services call that took long enough to return that the 30 seconds command timeout meant that the MySQL connection had been closed by the server.

I added a Default Command Timeout to the connection string that the program was using:

Database=vitality;Data Source=dbserver-43;User Id=vuser;Password=dh27ehdk;Default Command Timeout=300

 
So, the command timeout was now 5 minutes rather than 30 seconds.

I ran the program again… It worked perfectly.

I’m still not sure why it just hung and didn’t throw an exception. In future I’ll be more wary of code that potentially takes a long time to process a single row of data (e.g. has to make a web service call).

Written by Sea Monkey

June 9, 2010 at 2:00 pm

Posted in Debugging, Development

Tagged with ,