Thursday, December 21, 2006

QA - COM Initialization and IIS

Question:

Sometimes people (my customers) have a problem with COM Initialization in an ISAPI application. It seems sometimes they do not have to call CoInitializeEx and sometimes they do. Since CoInitializeEx "must" be called prior to using COM and sometimes the isapi works without calling this function, it seems IIS or something else has already called this function.

Can someone tell me, what is IIS's behavior in regard to COM initialization?

Answer:

IIS's behavior in regards to COM initialization is clean - it simply does nothing.

Thus, the behavior you describe sounds like a common bug with some ISAPI DLL running on IIS - calling CoInitializeEx() on threads not owned by the ISAPI. Since IIS reuses threads from its thread pool to call into ISAPI, the following sequence can occur and cause what you observe:

  1. IIS does not call CoInitializeEx() on any of the threads in its thread pool.
  2. IIS uses thread 1 from its thread pool to call the ISAPI entrypoint.
  3. In the entrypoint, ISAPI calls CoInitializeEx() on thread 1 but does not call CoUninitialize() prior to exiting the entrypoint and returning control to IIS.

    thread 1 has now called CoInitializeEx() once.

  4. IIS later uses thread 2 from its thread pool to call the ISAPI entrypoint. Since this thread has never called CoInitializeEx(), thread 2 has called CoInitializeEx() once.
  5. IIS later re-uses thread 1 from its thread pool to call the ISAPI entrypoint.

    thread 1 has now called CoInitializeEx() twice.

At this point, this ISAPI has seriously confused things. There is no way for it to call the correct number of CoUninitialize() on all the IIS threads (suppose the ISAPI entrypoint is never invoked again by IIS - no chance for it to call CoUninitialize() correct number of times, and no way for new user of the same thread to know how many times to call CoUninitialize()). It has tainted the thread pool shared by all other ISAPIs. All sorts of badness can commence at this point.

The simple rule of thumb: do not call CoInitialize()/CoUninitialize() on a thread which you do not own.

//David

5 comments:

David Wang said...

On the newsgroup thread where this originated, Wade disclosed that IIS6 *does* call CoInitialize() on the thread while it did not prior to IIS6 (Wade's preached that to me many times before). I trust that he knows this detail because he wrote it himself.

But the statement remains true - do not call CoInitialize() on threads that you do not own - because you don't know if someone else has called CoInitialize() already, and you need to pair CoUninitialize with each CoInitialize.

Just create your own thread pool, control execution, and instantiate as you want...

//David

gurgaonindustry said...

Schools In Gurgaon | Hotels In Gurgaon | Hospitals in Gurgaon | Jewelers In Gurgaon


i like your blog

Anonymous said...

Cartier enjoys a reputation for selling only the high quality watches and accessories.
http://www.watchestoo.com
replica watches
Men’s watches
Fake watches
watches
IWC
IWC Portofino Automatic IW356307
IWC Portofino Automatic IW356308
IWC Portofino Automatic IW356404
IWC Portofino CHRONOGRAPH IW378302
IWC Portofino CHRONOGRAPH IW378303
IWC Portofino Portfino IW353301
IWC Portofino PORTFINO IW3533-12
IWC Portofino PORTFINO IW353306
IWC Portofino PORTFINO IW353313
IWC Portofino PORTFINO IW356303
IWC Portofino PORTFINO IW356305
IWC Portofino Vintage IW544801
IWC Portofino

Unknown said...

Nowadays louis vuitton handbags are so popular in the world and the name of Louis vuitton bags is well known to the world, especially louis vuitton . Every woman dream of owning a lv , even some of them are crazy.

Anonymous said...

A few months ago, I bought my first two pairs of proper leather Louboutin Shoes online, one is high heels, the other one is mini-heels. They really cost me much money, and I love Christian Louboutin Boots very much. They are nice and comfortable. Since then, I have been struggling to find reading material about how to care for leather shoes. I read much before I wear Christian Louboutin Pumps shoes. I think I am a lucky gay. Many women are clueless about taking care of their Christian Louboutin Sandals .


People started liking Ed Hardy clothing because of the art work that the brand products had on them. People felt that they could express themselves very well by wearing ED Hardy Shoes . They felt that they were able to standout from the crowd if they wore ED Hardy Shoes collection of dresses. hardy shirts did work and very soon it became very famous among the youngsters of USA.


People that wear scarpe Hogan walking shoes on a regular basis are usually partial to one brand or another. And unfortunately, it is difficult to determine just by trying on a pair of shoes how hogan donna are going to feel after a 3 mile walk. I have had shoes that felt fine through the first mile or 2 and by the time I got through the third mile, I was wishing I had worn a different pair of shoes, then I choose Hogan scarpe uomo shoes. Hogan uomo
is particularly comfortable.