OK, Apple…. this should be amusing

Today Apple is having some sort of special press conference, which I’m sure is about all the iPhone 4 and iOS 4 problems… be it the silly stuff about the antenna or maybe how iOS 4 is less than stellar.

Can’t wait to see how Steve’s patented Reality Distortion Field comes into play.

I mean… look at this shit:

(h/t Gawker)

This is why my iPhone is still using the iPhone OS version 3.x.x. I’m a software developer (and a Mac developer at that… Apple has somehow buttered my bread for probably 30 years of my life)… if there’s anything I’ve learned in my career it’s to NEVER go with the x.0.0 release of something… wait for the x.0.1. 🙂  Of course, if a few months go by and there is no x.0.1 then likely the x.0.0 release is fine. My point is, I’ve given up on being an early adopter. Again, being a developer myself — one that writes his fair share of bugs — I know how fragile and buggy and troublesome software development (or any sort of product development) can be. I have enough of my own bugs to worry about, I don’t have the time to waste dealing with someone else’s bugs.

Let’s see what Steve does today. Should be amusing.

Named pipes and unwanted hangs

The past few days I’ve been dealing with a wonderful little problem.

They’re called “named pipes“.

The main product I work on in my day job was doing some filesystem scans and would hang for some unknown reason. After much investigation we noticed the file it was hanging on was a named pipe file way down in the bowels of the system (a temporary file created by the com.apple.notify process). What was happening was well… it’s a pipe. Something in my app was opening that pipe and thus the app “hung” because it was now wanting to read from the pipe. Thing is, my app doesn’t care about pipes at all, it’s just working with files. As well, we weren’t explicitly opening the file; we would call some other OS routine and somehow somewhere in that OS function’s implementation it called open() and thus we hung.

And so, what a bear this is.

In the end we decided to check for and avoid pipe and socket files at all costs. Any means by which a file can “get into” the app, we put up a wall at the perimeter so no such files can even get in. We figure we keep them out at the wall, then we don’t have to spend lots of CPU cycles internally to constantly deal with them (tho critical code should still perform checks). Plus, since one big part of the problem is we can’t control what others do and if they might open() the file, we have to nip it in the bud as soon we become aware of the file and minimize how much “work” is done on the file in case some other function might open() the file and we risk hanging.

To check is pretty simple. Code something like this (NB: I tried using the “code” tags and less-than/greater-than signs for the #include, but WordPress’ editor seems to get confused… so I just used the “pre” tag, which isn’t quite giving me the results I want either… oh well.).

#include "sys/stat.h"

bool
FileUtilities::IsUnsupportedFilesystemItem(
const char*    inPath)
{
    bool is = true; // assume failure, because if we fail on such 
                    // a simple operation something really has to be wrong
    if (inPath != NULL)
    {
        struct stat statInfo;
        int retval = lstat(inPath, &statInfo); // use lstat() instead of stat() because 
                                                   // we want to minimize resolutions or other "work"
        if (retval == 0)
        {
            if (!S_ISFIFO(statInfo.st_mode) && !S_ISSOCK(stat.st_mode))
            {
                is = false;
            }
        }
    }
    return is;
}

How you actually cope with it or if you consider them problems or not is up to you and your code and your situation. The key of the above is to get the file’s mode then check the FIFO and SOCK flags. If set, reject the file.

For most people, this isn’t going to be an issue or a problem. I mean, we went around for quite some time and never dealt with the issue. And in daily use, most people aren’t going to see pipe or socket files.

But it’s worth noting and thinking about. Nothing wrong with being a little defensive.

Self-document code is anything but

If you know me, or maybe you can just tell from my blog, I can be verbose.

It’s how I am. I tend to prefer more information to less. I think you get further in life by knowing more, not less.

But I also know this wears on a lot of people. They just want the bottom line and don’t care why or how you got there. IMHO that’s a pity because then they never truly understand and can never arrive at educated conclusions on their own. OK fine, a baby is made, that’s the bottom line. If you want to stop there and never know more, that’s your business. But in my book, knowing how to make that baby is interesting, and then going through the motions of making that baby even more interesting. 🙂  Like I said, it’s good to have knowledge and information.

I write software for a living. There’s great debate about documenting code, be it formalized documentation apart from the code or writing comments in the code itself. I’ve never jived with folks that say code should be self-documenting and that’s all the documentation you need. Sure, you should write readable and maintainable code. Naming your variable “numberOfObjects” is far better than just naming the variable “i”. But you must have comments. Why? Precisely.

Self-documenting code can tell you what and how, but it cannot tell you why. For that, you must use external documentation.

You must go through the effort of writing comments to explain bits of code. Depending on the code, you may also want to write larger external documents (e.g. in a word-processor) that explain the greater architecture and how all the parts of the code fit together and how to use it all. This is something that cannot be conveyed by reading the code itself, and I just don’t understand those that think this sort of documentation is a waste of time and somehow if you do it makes you “not a real programmer”.

Well buddy, real programmers know the moment after the code is written it must start being maintained. If you can’t remember what you had for breakfast a week ago, can you expect to remember why you wrote this code when you come back to it in 6 months?

Case in point. Just yesterday I was working on a bug in our software where the application would hang. All signs and symptoms were odd but somehow made sense to each other. When our QA guy told me one key point (“looking at the permissions flags, there’s a ‘p’… what’s that?”) it all came back to me. The file was a named pipe and I dealt with this very problem in the past. I went looking in code for where I previously dealt with it. I found it. The comment was dated over 5 years ago.

5 years ago.

When I fixed the problem — 5 years ago — I added copious comments to the source code to explain the problem in great detail; 50 lines of comments. I know many would say that was ridiculous! That it’s just his (annoying) verbosity! Well, thank goodness for it because without it there would have been no way I would have remembered that 5-year-old problem in such great detail and know exactly how to fix it (again) today.

Here’s an article by Jef Raskin discussing the same thing. Jef Raskin would know.

So yeah… people tell me I’m verbose. I really don’t care. I am who I am and I know when to be curt and I know when to ramble on. There are times when comments aren’t needed (don’t tell me what or how), but you do need to explain why and not be afraid to go into detail because the code you may have to maintain may be your own. Do yourself a favor and explain yourself. And if someday that code gets to be maintained by someone else, do them a favor and explain yourself.

I’ve never known anyone to say there’s such a thing has having too little ammo. I feel the same way about code comments and information. 🙂

xml standalone

Apple changed the NSXML/CFXML implementation in Snow Leopard to use libxml. Generally a good move, but so far it appears to have caused some problems:

  • The notion of -[NSXMLDocument isStandalone] changed, at least in the sample code I’m using as a reproducible case for the bugs. In 10.5 the value is NO, in 10.6 it’s YES.
  • Having the XML declaration contain standalone="yes", at least in the sample code, eventually causes the libxml parser to crash. Maybe not always, but if you send the sample code an AppleScript of
    tell application "SimpleScriptingPlugin"
        beep
    end tell

    the sample app will crash. Force [myNSXMLDocument setStandalone:NO] and no more crash. Or run it under Leopard regardless of the standalone setting and no crash.

It’s taken many months of investigation and going back and forth with Apple to figure out this OS bug, mostly because due to how the problem comes up we were chasing down other avenues (was this an AppleScripting problem? Still could be, at least in part.). But for now, it seems we’ve a few issues with how NSXMLDocument generates XML, how NSXMLDocument regards being standalone (and across the OS versions), and then how libxml ungracefully handles that specification. Hopefully with my further information, Apple will be able to properly remedy all of this. Yes, it’s all properly filed with Apple, so they’re aware.

I tell you. This crash problem has been plaguing me and our users for quite a while. We’ve been able to provide the users with ways to work around things, but it’s not optimal. My hope is now with this information we can provide a better solution in our code and hope that Apple will soon fix this in the OS. Finally hitting upon this last night was quite the euphoric moment. 🙂  Granted, there may still be other things to deal with for this, and I could be premature in celebrating… but certainly it’s more info for Apple towards fixing the problem, and it’s still a hopeful step in the right direction for me and the users of my product.

Google is watching you… quickly

Wow. That didn’t take long.

I publish the previous post. I thought… hrm. “Officer Potus”. I wonder if Pastis is meaning anything by that, so I punch “Officer Potus” into Google.

I get the first page of results.

What was the last link on that first page of results? My previous post… with the time stamp of “46 seconds ago”.

Man… Google is fast. Makes you wonder what Google has to do to get things indexed so quickly. What must it be monitoring, and how… and the sort of infrastructure necessary for that. Fascinating. A little scary too.

Safari 5

Apple released Safari 5 yesterday.

Being a software developer as long as I’ve been, I know better than to get the 5.0 release — wait for the inevitable 5.0.1. 🙂

But I’ve been having so many problems with Safari lately (I think more due to the websites visited than Safari itself, e.g. Facebook) I figured what the hey. I needed to reboot my machine anyways, the Safari 5 install would require a reboot, so might as well.

So far I’m pleased with the update. Rendering of pages is a lot faster than with Safari 4. The new “reader” feature is pretty spiffy, like Quick Look for a multi-page web story. It makes the text bigger, more readable, better layout, no ads. That it’s more readable is certainly a big win. I’m sure I’ll be using this feature more and more as I get used to it being there.

There are finally “extensions” for Safari. I’ll be curious to see what people come up with.

iPhone 4

Being a Mac developer, it used to be an annual ritual to attend Apple’s World Wide Developer Conference (WWDC).

I was supposed to attend this year, but opted out of it because I knew it’d be nothing but an iPhone love fest. I’ve nothing against that, just that it’s nothing directly relevant to my present-day money-making, so the cost of sending me out there isn’t worth it. Nevertheless, I still like trying to catch the keynote because important things happen. Funny thing is, I remember when WWDC specifically was NOT a horse-and-pony show. That the keynote was purely a developer thing, there weren’t product announcements and other such things. Ah, to be an old-fart remembering the good old days. 😉

The iPhone 4 looks to be rather impressive, both from a consumer perspective and an internals geek perspective. iOS 4 as well looks to be quite a nice advancement as well. Apple’s really working to knock things out of the park and continue to set the bar that the rest of the industry will chase. Will I buy an iPhone 4? No, only because I don’t really need what it offers. But without question I like what’s being offered. As soon as I heard about the “front facing phone” I knew it would be about video chatting, and lo, FaceTime.

I am curious about the details of the WiFi stuff messing up the demos. Exactly why did that cause such a problem?

I did notice, no mention of 4G. I wonder why not.

Of course, we’re still stuck with AT&T. *sigh*

But as you could see from the keynote, it’s all about the iPhone. Don’t get me wrong, for Apple that’s where it should be. But it reinforces why I’m happy I stayed home. WWDC just isn’t what it used to be. And even while the topics have changed well… let’s just say the old WWDC’s were a lot more fun. You old timers know what I’m talking about. 🙂