Expert Texture Home Contact me About Subscribe Digipede Connect on LinkedIn rwandering on Twitter rwandering on FriendFeed

The blogged wandering of Robert W. Anderson

Dan’s Worst .NET Bug

Last Thurday, Dan posted Worst .NET Bug I’ve Ever Seen.  This post was the result of a Digipede customer support incident resulting from this .NET behavior.  We had some trouble tracking it down.  The unexpected exceptions were bad enough — and the deceptive exception message text made it worse. 

He posted code to reproduce the problem on a single thread (a tight loop of open, write, close).  The actual code in question included no looping, but multiple threads following a fairly common pattern (i.e., open temp file, write, close, delete, rename).  The code was properly synchronized, but it still threw exceptions.  To keep this simple, I’ll stick with Dan’s simple version:

while (true) { 
   using (Stream sw = File.Open(strFileName, FileMode.Create)) { 
         using (BinaryWriter bw = new BinaryWriter(sw)) { 
            BinaryFormatter bf = new BinaryFormatter(); 
            bf.Serialize(bw.BaseStream, this); 

Loop this code on one or more threads and you may eventually get a System.IO.IOException.  I say may because I can’t reproduce it, but Dan can and so can one of our customers. 

Given many caveats (e.g., the paths are valid, the proper permissions exist, the file is not opened elsewhere, etc.), this code should never throw an exception.  The unmanaged resources should be released when the Stream.Dispose method is called.  The file should be closed.  The types of exceptions that our customer (and Dan) got all follow from the fact that sometimes the file isn’t closed.  Add a Thread.Sleep(1000) after the using block and the problem goes away.

So, is this a .NET bug or a Windows bug, or just expected behavior?

The first thing I did was take Dan’s simple loop and write it using the Windows Platform SDK (i.e., using the Windows API, not .NET).  I left out the delete/rename part of the pattern to make it like Dan’s tight loop.  The code looks like this:

while (true) { 
    HANDLE hFile = CreateFile(fileName, GENERIC_READ | GENERIC_WRITE, 

    if (hFile == INVALID_HANDLE_VALUE) { 
        printf("Could not open file (error %d)\n", GetLastError()); 

    byte buffer[1024]; 
    DWORD writtenBytes; 
    if (!WriteFile(hFile, buffer, sizeof(buffer), &writtenBytes, NULL)) { 
        printf("Could not write file (error %d)\n", GetLastError()); 

    if (!CloseHandle(hFile)) { 
        printf("Could not close file (error %d)\n", GetLastError()); 

This code works fine on Dan’s machine (i.e., never exits).  

Next steps

So, yes, this appears to be a problem in .NET.  My next steps are to . . .

  1. Check the .NET versions / are they somehow different between our machines?
  2. See if this is a known bug., here I come.
  3. Question my assumptions.  

To this last point, does .NET (or Windows) not guarantee that a file is closed when the last handle is released?  If this is somehow true . . . how much code have you written that assumes that it does?  Me, a lot.  OK, not a huge amount, but enough that I’m really surprised this hasn’t come up before.

I’ll follow up on this post when I get to the bottom of it.  I also have some complaints about the content of some of the exceptions.  In the meantime, any reader want to try to reproduce this?

Tags: , , , , , , ,

Automatic Properties Follow-up

Back in March, I posted Automatic Properties in C# 3.0 which included a couple of questions.  At the time I hadn’t installed any Orcas beta.  My post was based on other posts (including one from Bart DeSmet’s excellent blog: C# 3.0 Automatic Properties Explained).  My questions leapt out at me (maybe I’ll post about why that is . . .). Since that post, I have played a bit with the VS 2008 betas as well as built and tested the Digipede SDK samples.

A recent comment reminded me that I should revisit the questions.

Question 1: Is there an attribute to set the automatic field name?

Reason?  To maintain binary-serialization compatibility when moving from automatic properties to “normal” properties. 

Answer:  I don’t know if there is explicitly.  The underlying field name seems to have been changed since Bart’s first post and VS 2008 beta 2 (they have improved it so the field name can be derived from the property name).  This is good, because it might mean that the automatic property could be converted to the “normal” kind of property while maintaining default binary serialization; however, the backing field name looks like:


. . . and angle brackets are not valid for member names — the compiler can generate them, but aren’t valid C# syntax.  So, this is not quite what I want.

Question 2: Is there an attribute you can put on the automatic property to hide the underlying field or is it automatic?

Reason?  By default, properties with backing fields result in (usually) duplicated information in the debugging views.  If the backing field is generated by the compiler, it would make sense to just hide it (with the DebuggerBrowsableAttribute or some other way).

Answer:  This is automatic.  Good.  Probably a result of the CompilerGeneratedAttribute that is added to the generated backing field.


Based on what I know now, I wouldn’t use automatic properties if default binary serialization is likely to be used.  Regardless of this possibly arcane issue, automatic properties are cool.  I like them.

Tags: , , , ,

Microsoft postpones PDC and more

Robert Scoble had a great post on the PDC and what is going on at Microsoft.   I liked it so much I included most of it here with my comments.

The PDC stands for “Professional Developer’s Conference.” It happens only when Microsoft knows it’ll have a major new platform to announce. Usually a new version of Windows or a new Internet strategy.

So, this means a couple of things: no new Windows and no major new Internet strategy this year.

I agree there is no new strategy this year and that is disappointing; however, Silverlight is huge and this year, and if not an Internet strategy it is an Internet developer strategy. 

Cleary Mix07 was the place to be — I would have made sure I went if I had known that PDC was going to be cancelled.

Some other things I’m hearing about the next version of Windows? There still is a ban on .NET code in core parts of Windows. They aren’t getting enough performance yet from .NET to include code written in it inside major parts of Windows. This is a bummer, because .NET is a lot easier to write than C++ and letting Microsoft’s developers write .NET code for Windows would unleash a bunch of innovation.

I fully agree with you here — a definite bummer.  Yet I don’t agree about the performance of .NET.  Certainly there are parts of Windows that need to be unmanaged code; but Digipede has a slew of customers using .NET for computation and getting terrific performance from it.  Face it, this “not performant yet” argument is used by people at Microsoft from kernel / device authors (OK) to the Office team (what?).  It is hard to separate the good arguments here from just plain bias and inertia. 

The person who told me this (who works at Microsoft) told me .NET still takes too long to startup and load into memory and because Windows is now being compared to OSX they can’t afford to ship components that would slow down Windows.

What?  If this were baked into the OS, couldn’t they do a better job of sharing this startup cost (i.e., doing it once with reuse)? 

This gets right back to my posts about how the Windows .NET API is actually dead (see these: WinFx).

It also means that Ray Ozzie’s team probably doesn’t have anything dramatic to announce yet and they aren’t willing to have live within the bounds of a forcing function like the PDC (PDC forces teams to get their acts together and finish off stuff enough to at least get some good demos together).

This is the “no Internet strategy this year” part.  Yup.  Definite bummer.

Some other things I’m hearing from the Windows team? That they are still planning out the next version of Windows. So, I don’t expect to see a beta until 2008 (probably second half of the year, if we see one at all) and I don’t expect to see a major new version of Windows to ship until 2009.

Microsoft says it won’t be as long between releases of the OS now.  I think, though, we won’t see a major new version released until Windows till 2010.

Anyway, this is sad cause I was hoping to see Microsoft make an all out push for developers this year.

Well, I think they have.  Their developer story is getting better and better every quarter.  I think they should have had the PDC anyway and continued to flog the .NET 3.0 and new .NET 3.5 stuff particularly Silverlight. 

What do you think it all means? Am I reading too much in between the lines?

Maybe you are.  I think the timing for the PDC was definitely wrong for Microsoft.  The Microsoft Internet strategy we are really waiting for has to do with Office / other applications and Internet services.  When this is unveiled, I think it will have less to do with developers than warranted at a PDC.  Ironically that should have been announced at Mix, but will have to wait for the next one.

Will Microsoft unveil a new Internet strategy at Mix08?  I bet.

Tags: , , , , , , , , ,