code 0 comments suggest edit

Ian Griffiths finds a niggle about my post on sockets.

This may surprise a few friends of mine who regard me as a “human dictionary”, but I had to look up the word “niggle”. Apparently only the “human” part of the appellation applies. I’ve apparently fooled them by reading a lot of sci-fi fantasy and choosing to learn and use “impressive” words such as Bacchanalian in everyday conversation (“I wrote this code in a drunken stupor from a bacchanalian display of excessive beer drinking.”). It’s really all smoke and mirrors. But I digress…

His comment is quite insightful and well worth repeating here in full.

One minor niggle with this code… \ \ Although the example is correct as it stands, it doesn’t mention an important issue: the Socket class is not thread-safe. This means that if you do use the async operations (and by the way, I’m completely with you here - I’m a big fan of the async operations) you need to take steps to synchronize access to the socket. \ \ As it stands there’s nothing wrong with this example as far as I can see. But what if you also have an asynchronous read operation outstanding? Can you guarantee that a read and a send won’t complete simultaneously, and that you’ll be trying to access the socket from both completion handlers simultaneously. \ \ So in practice, you tend to want to use some kind of locking to guarantee that your socket is only being used from one thread at a time, once you start using async socket IO. \ \ (Also, you left out one of the clever parts of IO completion ports - the scheduler tracks which threads are associated with work from an IO port, and tries to make sure that you have exactly as many running as you have CPUs. If one of the threads handling work from an IO completion port blocks, the OS will release another work item from the completion port. Conversely, if loads of IO operations complete simultaneously, it only lets them out of the completion port as fast as your system can handle them, and no faster - this avoids swamping the scheduler under high load.)

I have to say, Ian’s depth of knowledge on such topics (or nearly any geek topic) never ceases to impress me. Fortunately for my app, the client socket only receives data every three seconds and never sends data back to the remotely connected socket (how boring, I know). In any case, I will double check that I am synchronizing access to the socket just in case. Perhaps I’ll use the TimedLock to do that. ;)

While we’re in the business of finding niggles (Ian, you’ve hooked me on this word. For some strange reason, I can’t stop saying it) I should also point out that IO Completion ports awaken threads from the ThreadPool in order to perform an asynchronouse action. The entire asynchronous invocation model of .NET is built on the ThreadPool. Remember that the next time you call a method that starts with “Begin” such as “BeginInvoke”. Chances are, it’s using a thread from the ThreadPool (especially if its a framework method. I’ll make no guarantees for methods written by your coworkers.)”

By default, the max threadcount for the ThreadPool is 25 per processor. In my application, the remote socket sends short packets of data on a regular interval, so the threads that handle the received data are very short lived. Sounds like an ideal use of the ThreadPool doesn’t it? However, if I were expecting a huge number of simultaneous connections, I might look into changing the machine.config file to support more than 25 ThreadPool threads per processor. Before making any such change, measure measure measure.

If you have a situation where the operations on the data are long lived, you might consider spawning a full-fledged thread to handle the remote client communications and operations. Long running operations aren’t necessarily the best place to use a thread from the .NET built in ThreadPool.

0 comments suggest edit

Resharper After hearing so much about how IntelliJ IDEA is the ideal IDE and blows every other IDE to smithereens from various Java pundits (and even a friend who’s a C# and Java developer), I was excited to try out Resharper 1.0.2 from Jetbrains, the maker of IDEA.

Installation went smoothly and I really am impressed with all the productivity enhancements contained within this tool. Rather than wait for Whidbey, I can have code expansions and refactoring NOW! The code formatting feature will surely help your team avoid coming to blows over such important and divisive issues such as “curly brace on its own line or after a method declaration?”. Everyone can have the code formatted to his or her own tastes. There’s a whole slew of other noteworthy features I won’t go into.

The one problem I have with ReSharper, and this is a doozy, is that it replaces the VS.NET Intellisense with its own, and its implementation way too slow for real development. Note that I’m running a fairly decent machine: Pentium 4 1.7 GHZ 512 MB of RAM. One thing I appreciate about VS.NET’s Intellisense is that it is snappy and can keep up with me. So it’s a huge detriment when I’m typing along and hit “.” and have to wait a second for the auto-completion box to display. I hope there’s a way to disable the auto-completion, but maintain all the other great features. Otherwise I’m afraid I will have to uninstall this.

Anyone else have good or bad experiences with this product? Any recommendations?

UPDATE: I couldn’t take it any longer. I had to uninstall this. It’s a shame

personal 0 comments suggest edit

No, I’m not talking about Pizza from Chicago. I’m talking about this dynamic duo:

Deep Dish

As you can see at the bottom, I’m listening to their remix of Dido’s Thank You which won a Grammy. These guys are one of my favorite electronica bands out there. They’re much more in the deep funky progressive house area. Somewhat dark, without being too malicious. I highly recommend you check them out. Especially this Dido remix.

[Listening to: Dido . Thank you - Deep Dish - Global Underground 021 - Moscow CD1 (7:25)]

0 comments suggest edit

The path of least resistance when writing threading code as well as socket communications is to use techniques that cause indefinite blocking of some sort. Personally, I prefer never to block indefinitely. For example, it’s quite common to see code such as:

    //Do Something here...

Nothing wrong with this inherently, but this piece will try to acquire a lock on someObject indefinitely. Imagine if you mistakenly had code like (yes, it’s a bit contrived)

using System.Threading;

//... other stuff ...

object someObject = new Object();
object someOtherObject = new object();

public void LockItUp()
        Console.WriteLine("Lock 1 acquired.");

        ManualResetEvent mre = 
                new ManualResetEvent(false);
        WaitCallback callback = 
                new WaitCallback(ThrowAwayTheKey);        

        ThreadPool.QueueUserWorkItem(callback, mre);
        // wait till other thread has lock on 
        // someOtherObject

            Console.WriteLine("I never get called.");

void ThrowAwayTheKey(object resetEvent)
        Console.WriteLine("Lock 2 acquired.");
        ManualResetEvent mre = 
                resetEvent as ManualResetEvent;
        if(mre != null)
            mre.Set(); //original thread can continue.

            Console.WriteLine("Neither do I");

Calling the method LockItUp will cause a deadlock and the application will hang until you kill it. Although this example is a bit contrived, you’d be surprised how easy it is in a sufficiently large and complicated system with multiple developers for you to run into this situation in a more roundabout manner. I see this often enough because using the lock statement is the path of least resistance. Instead, try using the TimedLock struct.

Another situation this type of thing comes up is with socket programming. Often I see code like this:

using System.Net.Sockets;

//... other stuff ...

byte[] _buffer = new byte[4096];
public void Listen(Socket socket)
    int bytesRead 
        = socket.Receive(_buffer, 0, 4096
                , SocketFlags.None);

    //You're sitting here all day.

If the remote socket isn’t forthcoming with that data, you’re going to be sitting there all day holding that thread open. In order to stop that socket, you’ll need another thread to call Shutdown or close on the socket. Contrast that with this approach:

byte[] _buffer = new byte[4096];
public void BeginListen(Socket socket)
    socket.BeginReceive(_buffer, 0, 4096
            , SocketFlags.None
            , new AsyncCallback(OnDataReceived)
            , socket);
    //returns immediately.

void OnDataReceived(IAsyncResult ar)
    Socket socket = ar.AsyncState as Socket;
    int bytesRead = socket.EndReceive(ar);
    //go on with your bad self...

BeginListen returns immediately and OnDataReceived isn’t called until there’s actual data to receive. An added benefit is that you’re not taking up a thread from the ThreadPool, but rather you’re using IO completion ports. IO Completion ports is a method Windows uses for asynchronous IO operations. When an asynchronous IO is complete, Windows will awaken and notify your thread. The IO operations run on a pool of kernel threads whose only task in life is to process I/O requests.

Since BeginListen returns immediately, you’re free to close the socket if no data is received after a certain time or in response to some other event. This may be a matter of preference, but this is a more elegant and scalable approach to sockets.

For more on asynchronous sockets, take the time to read Using an Asynchronous Server Socket and related articles.

0 comments suggest edit

This seems to be my favorite geek subject, but I have to tell you a success story using Ian Griffith’s Timed Lock struct with my enhancement.

To recap, when you fail to acquire a lock on an object because another thread already has one, my enhancement allows you to see the stack trace of the blocking thread. Well the other day, I was running a suite of unit tests against a socket server I was building when one of the tests failed with a ThreadTimeoutException. Looking at the stack trace, I found the line of code where another thread was unnecessarily holding a lock on the object. I used to spend a lot of time poring through logs trying to decipher threading issues such as this.

0 comments suggest edit

There are by some estimates more than a million weblogs. But most of them get no visibility in search engines. Only a few “A-List” blogs get into the top search engine results for a given topic, while the majority of blogs just don’t get noticed. The reason is that the smaller blogs don’t have enough links pointing to them. But this posting could solve that. Let’s help the smaller blogs get more visibility!

This posting is GoMeme 4.0. It is part of an experiment to see if we can create a blog posting that helps 1000’s of blogs get higher rankings in Google. So far we have tried 3 earlier variations. Our first test, GoMeme 1.0, spread to nearly 740 blogs in 2.5 days. This new version 4.0 is shorter, simpler, and fits more easily into your blog.

Why are we doing this? We want to help thousands of blogs get more visibility in Google and other search engines. How does it work? Just follow the instructions below to re-post this meme in your blog and add your URL to the end of the Path List below. As the meme spreads onwards from your blog, so will your URL. Later, when your blog is indexed by search engines, they will see the links pointing to your blog from all the downstream blogs that got this via you, which will cause them to rank your blog higher in search results. Everyone in the Path List below benefits in a similar way as this meme spreads. Try it!

Instructions: Just copy this entire post and paste it into your blog. Then add your URL to the end of the path list below, and pass it on! (Make sure you add your URLs as live links or HTML code to the Path List below.)

Path List\

  1. Minding the Planet\
  2. Luke Hutteman’s public virtual MemoryStream\
  3. JayBaz_MS blog\
  4. You’ve Been Haacked! and you like it.\
  5. (your URL goes here! But first, please copy this line and move it down to the next line for the next person).

\ (NOTE: Be sure you paste live links for the Path List or use HTML code.)\

[Via Luke Hutteman’s public virtual MemoryStream]

0 comments suggest edit

Dave Winer writes:

There’s something missing from Windows. An application that hooks into the outbound Internet message flow, and shows me where messages are going. This would allow me to figure out what spyware is running on my system even if the various utilities can’t get rid of them. Then the next step would be to allow me to block traffic to certain servers. That would disable the spyware. It seems that I should have control of my machine at that level.

And Jeff Sandquist responds that Windows XP SP2 can do this via the new firewall, but look again. The firewall built into XP blocks incoming traffic, not outgoing. My guess is that feature will come with SP3. For now, I recommend the free version of ZoneAlarm which is perennially considered by many to be the best desktop firewall software out there. It will notify you when an application tries to send an outgoing message to the internet and its quite easy to configure.

0 comments suggest edit

Having been on one of the most beautiful hikes during our trip to Seattle, it would be a grave disservice not to post a few pictures of the hike here. For all you Microsofties out there (and other geeks in Washington state), I highly recommend getting out of the office for this hike. Bring some water and sunscreen.

The four to five mile trail begins on the Pacific Crest Trail No. 2000 (the entire Pacific Crest Trail stretches from Canada to Mexico) from Chinook pass. At the parking area, you’ll cross Route 410 by walking on a wooden overpass seen above. The two in the picture below are the friends we visited, Dan and Grace Kalish.

Beginning of the hike \ Car careens off the road while hikers wave at camera.

After a bit of hiking, through lush green forest, we came upon the first of several lakelets. The hike has an elevation gain of just 700 feet, so it’s fairly easy, though we managed to make it a challenge by taking a wrong turn and visiting a full fledged lake. The detour set us back around another half hour.

A lake \ Dan wonders how those people in the lake got so small

Akumi and I had a plane to catch that evening and we were running out of time and getting a bit worried. It took us a bit longer than we expected to get out here, and the unexpected detour cost us even more time. At that point, we decided to play it safe and return back the way we came and not complete the loop. What a terrible decision that would have been. When we returned to the main loop, another hiker told us we were exactly halfway around the loop. That led to the obvious conclusion that we should continue. What a fortuitous decision, as this trail saves all the best views for those willing to stay for act 2.

Mt. Rainier \ You’ve seen one picture perfect lake surrounded by violet wildflowers in the woods with an enormous mountain backdrop, you’ve seen them all.

Phil has a snack \ Man loves nature. Man eats nature.

Not one to let a photographic opportunity go by, we made sure to pose for a group shot with our new friend, Mt. Rainier. As big as it looks in the photos, it looked absolutely ginormous in person. It positively encroaches on us as we take this shot.

Group Shot \ That cardboard backdrop is so damn convincing!

For most of the rest of the hike, we were treated to wonderful views of the mountain. It started to get rather blase. “Yet another view of the one of the most spectacular woodland scenes I’ve ever seen. Ho hum.”

Hiking along \ Tired of my incessant demands to stop and pose, my friends pose a mutiny.

Wildflowers \ Rainier tries to hide behind some trees.

At the end, the hike ended up being reasonably vigorous due to our detour, but quite manageable over all. The next time we hike here, we plan on making it a picnic next to the lake with the view of the mountain. I highly recommend this hike.

Directions From Seattle: via the Seattle Post Intelligencer\ Go south on Interstate 5 and take the state Route 18/Auburn exit (before Tacoma). Follow the signs to Auburn and state Route 164 (take the second Auburn exit off Highway 18, not the C Street exit). Follow 164 through Enumclaw to state Route 410 and turn left. Continue east to Chinook Pass, about 44 miles from Enumclaw. At Chinook Pass, continue a short distance past the wooden overpass to a parking area on the left side of Route 410 (the Mather Memorial Parkway). This is the parking area for the PCT with stock access and portable toilets, elevation 5,040 feet. If you are beginning from Tipsoo Lake, park at the Tipsoo Lake picnic area lot and look for the signed Naches Peak Loop trailhead opposite the lake.

0 comments suggest edit

Patrick Cauldwell points out that the challenge of coding a business app isn’t in writing the code, that’s easy. The challenge is in understanding business requirements, and not because developers and architects can’t understand plain English.

In my experience, I find that often, “business people” (for the lack of a better word) do not apply the same rigor to developing a business model for an application as developers do towards requirements gathering, specifications, and coding for the application.

I understand that agile methodologies are designed to help manage change, but even they can’t keep up when the business owner doesn’t even know what the requirements should be, but is spouting them out anyways. What do you do when the business owner has been involved in every step of the project, played with every prototype and interim release, but at the end says, well that’s not what I want.

For ~95% of business software, the actual code needed to implement them ends up being, if not trivial, then at least not technically challenging.  Business software projects go over budget/schedule etc. because the business rules aren’t known (to anyone) or because no one in the organization really knows what they want.  …snip…

So when people complain that projects are going way over budget/schedule/what have you, it’s almost always (in my experience) due to the fact that engineers spend 6 hours a day poring over vague and conflicting sets of documents, and 2 hours writing code.

[Via Patrick Cauldwell’s Blog]

As a developer friend of mine used to say,

This job would be great if we didn’t have clients.

Of course I prefer the paycheck.

0 comments suggest edit

I’m happily married, but if you’re not, and you live in L.A., I recommend you take your dog for a walk at the Barrington Dog Park, on the east side of Barrington just south of Sunset. We were playing a game of soccer, but we couldn’t help noticing that all the talent was in the dog park area adjacent to us. Looked like a bonafide LA pickup joint.

0 comments suggest edit

If you’re a lazy mofo like me, there’s one thing you’re not lazy about, getting out of having to do a lot of work. An important skill that should be on every lazy developer’s utility belt is the ability to understand requirements.

However, there’s a subtle technique to this. When I say, “understand requirements”, I don’t necessarily mean understanding what the client tells you the requirement is, but taking a moment to dig deeper and get to the real purpose of the requirement. Let me tell you a story based on a true story. has a two email systems for communicating to its clients. One system sends news letters and “Please come back” emails to clients who have opted in. The other system sends emails welcoming newly registered users as well as password reminders etc… The second system is designed to send only the most important emails so as not to get blacklisted.

Millard in Marketing decides that the second system should route emails through an email delivery service that guarantees delivery. He tells Damien the developer that only emails in a certain category should be sent through the new delivery service, and all others should be sent in the current manner.

Well Damien thinks about it and tells Millard it will take a day or so to add this routing within the second system. Currently the system only supports sending emails to one configured SMTP server. Now he has to add the ability to send some emails to another SMTP server based on certain criteria. He dives right into designing a general purpose email routing system. After a bit of time, he has the sudden thought,

“Now why the hell are they routing only a portion of emails to this new service anyways?”

So he asks Millard, who replies,

“To make it easier for you of course.”

After some discussion, Damien finds out that Millard would prefer to send all emails to the delivery service. So he changes one setting in his config file pointing to the service’s SMTP address, closes his office door, and takes a nap. Meanwhile, Millard is sending out emails complimenting Damien for going above and beyond the call of duty.

Be like Damien.

personal 0 comments suggest edit

UPDATE: I cannot help you move to Alaska. This post was written in 2004. If you want to move to Alaska, please don’t contact me. Try looking for information at the official Alaska homepage. Thanks!

Alaska’s Flag As a relocated Alaskan, I read Rory’s post about moving Canada to the right a bit so that Alaska could join the “lower 48” with amusement. I don’t think that idea would go over too well with Alaskans, as we…er…they take great pride in their independence, not only in spirit, but in location. Not only that, it could affect the fishing season, and you don’t mess with the fishing season in Alaska, the biggest state in the Union. That’s right Texas, the stars at night are even bigger and brighter in Alaska. Get over it, we rule you.

Alaska Map However, Alaska is not paradise on Earth (except in the summer when the sun stays out quite late drinking and carousing). Did you know they pay residents to live there? It’s a little thing called the “Permanent Fund Dividend”. The Permanent Fund is a huge investment of oil revenues in a variety of stocks and bonds. Around October of every year, the interest earned from the fund pays each resident anywhere between 1 to 2 grand a year, depending on how well the market does. Just in time for some good Christmas shopping.

Not only that, there’s no sales tax and no income tax in Alaska. Yeah. Starting to sound like a good place to live, no? Well, before you rush off to go panning for gold, keep in mind that winter lasts around 8 to 9 months of the year, so I hope you like winter sports.

0 comments suggest edit

There ought to be a law that requires employers to give their charges the day off due to great weather. Back in Alaska, we’d have the day off when it dropped to -60 F with snow up to your nostrils. They call it a “snow day”. Well there ought to be a “good weather day”.

Not that I needed it, I’m on vacation fool! No, I ask for this for all you suckers trudging to work while I was kayaking away on Lake Union in Seattle. We paddled our way through a channel to Lake Washington and had fun limboing underneath the freeway. Yes, underneath. There’s a portion of the freeway directly over the lake, and portions of it are only three feet above the water. My wife and I nearly scraped our noses trying to glide underneath laying on our backs against the kayak.

Earlier today we cashed in my REI dividend buying my wife some nice sandals. Seattle has the nicest REI store I’ve ever seen, complete with a huge climbing wall. It made me nostalgic for the wilderness of Alaska. I had the sudden urge to climb mountains, wrestle bears and sleep in caves. Then again, L.A. has its own version of wilderness that’s pretty wild too.

0 comments suggest edit

twilight samuraiWe saw a fantastic movie at the Landmark last night called Twilight Samurai. It stars Hiroyuki Sanada who played the bad ass in The Last Samurai who beats the crap out of Tom Cruise and was the Samurai leader’s second in command.

What sets this movie apart from most Japanese Samurai movies is how vulnerable the hero is. He’s trying to lead a simple life and struggles to make ends meet. He is sensitive to his daughters and senile mother (his wife died of consumption) encouraging the daughters to study the classics (quite atypical) along with their needlework. He isn’t a typical bushido super-hero with perfect valor, but a petty Samurai who wants nothing more than to watch his daughters blossom into young women. As you might expect, it isn’t always that easy to maintain a simple life for a Samurai with duties.

This isn’t the movie for you if you’re looking for one gratuitious battle after another. The fighting is scarce, but significant to the story, well done, and have a realistic look to them. There are no crouching tigers nor hidden dragons here.

0 comments suggest edit

My friend Julie showed me and Akumi around the campus today. She works for the Finance department in Building 4. When I say “campus”, Microsoft really has a collegiate feel to it. We even saw a group sitting in a circle in a plot of grass having what appeared to be a discussion group. We had lunch at the cafeteria there and the food reminded me of my college dining hall, not terrible, but nothing to write blog about.

Seems like everyone we know in Seattle is trying to pimp it to us. Grace, whom we’re staying with, is constantly pointing out how wonderful it is while Julie is selling both Seattle and Microsoft. She certainly knows how to sell Microsoft to a person like me (not that I need any selling). After lunch, she immediately took us by the soccer field where two lunchtime games were going on a perfect turf field. Need I see more?

After walking around a bit, we headed over to the Microsoft Store and Museum. The store didn’t impress me too much, but the museum was neat, and not just because of the X-Box consoles. The old pictures of Bill and crew inspire a laugh and it was interesting to see the box for “Microsoft Adventure”, one of their first games.

After our impromptu tour was over, we sat in the lobby of building 4 when I noticed a group of people on the other side of the glass walk by. I noticed a guy in an orange XML Web Services shirt and told my wife that the huge book in my backpack that adds all the bulk to my luggage was written by him. I would’ve liked to have introduced myself to Chris, but we were already on the outside of the glass.

0 comments suggest edit

I’m here in Seattle hooking an unsuspecting Dan Kalish on the drug I call RSS Bandit. He just finished the Washington Bar (after having passed the Connecticut bar only a couple of years ago. Sucks to be a lawyer and move.) and is enjoying a quiet repose. This is my first attempt to a non geek on RSS.

0 comments suggest edit

We fly off tomorrow to Seattle and don’t let me catch you even thinking about trying to snake my stuff. I live in a very bad neighborhood, you wouldn’t want to be there after dark or in the nude. We have vicious blind dogs in the yard that have been raised on human meat and will bite anything that breathes or walks funny. And robots in the house with lasers and duct tape and … and Barry Manilow CDs!

And…um.. we have cameras all over the place, and a direct upload to America’s Funniest Home Videos so everyone can laugh at you when you’re attacked by blind dogs, including the girl in HR who never looks in your direction you pathetic slob.

Besides, my stereo is out-dated and sounds crappy. But I hear the neighbors just bought a huge flat panel HDTV with a Bang & Olufsen stereo. You should steal their shit instead.

0 comments suggest edit

I have a bad habit of writing specs in the future tense. Since the system I’m spec’ing doesn’t yet exist, it’s so easy for me to fall in the habit of saying things like:

  • This page will look like this:
  • At this point the system will do this.
  • The system will display that control here.

instead of:

  • This page looks like this:
  • At this point the system does this.
  • The system displays that control here.

So what’s the big deal? Well there’s two big deals. Number one is a question of written aesthetics and the other is a more practical consideration. Ok, so they’re not all that BIG a deal. But let me continue.

Aesthetically speaking, using the present tense sounds more active and interesting. Remember all those lessons about active voice and passive voice in high school English? I think it’s made me paranoid.

Secondly, and more importantly, is that after you build the system and someone comes along and refers to you spec as a piece of documentation, it sounds kinda funny to say the system “will look like this” when it already “does look like this”. You can argue that the system doesn’t respond till the user interacts with it so that saying “When the user clicks here, the system will navigate there” isn’t so off the mark. See my first point in response to that point. I thank you for your time.