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

13 comments:

Rick Strahl said...

Hmmm... that doesn't gel with what I see in my applications.

I use COM in my ISAPI applications and in IIS 6 there's no CoInitialize() call required even if the hit is the first on the server (ie nothing else could have initialized that ISAPI thread but IIS). It was definitely required prior to IIS 6 so it seems that IIS is definitely initializing the thread somehow.

Also, I'm not sure I understand what you are suggesting. You suggest don't call CoInitialize() even though you say IIS doesn't initialize the thread. So how are you supposed to call a COM object then?

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

Anonymous said...

Now do you worried about that in the game do not had enough 2moons dil to play the game, now you can not worried, my friend told me a website, in here you can buy a lot 2moons gold and only spend a little money, do not hesitate, it was really, in here we had much 2moon dil, we can sure that you will get the cheap 2moons gold, quick to come here to buy 2moons dil.

Now do you worried about that in the game do not had enough 9Dragons gold to play the game, now you can not worried, my friend told me a website, in here you can buy a lot 9 Dragons gold and only spend a little money, do not hesitate, it was really, in here we had much 9Dragons money, we can sure that you will get the cheap 9Dragons gold, quick to come here to buy 9 Dragons gold.

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa pages. Thank you So much.

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

longge 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.

wangzi 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.

Anonymous said...

http://123bags.blog.com/
http://123louisvuitton.blog.com/
http://123louisvuittonbags.blog.com/
http://louisbags.blog.com/
http://louisvu.blog.com/

longge said...

However, ugg boots which owns a array of acclaimed designers, not abandoned delights women by contemporary styles, but aswell by absolute abundance and practicality.Sheepskin ugg is absolutely crafted from wool. Many times, uggs are befuddled abroad because they get adulterated or channelled afterwards getting beat for one season, instead of acceptable out of date. There are lots of styles in the accumulating of affidavit ugg sundance , accoutrement tall, abbreviate and abate versions. Due to adaptable sheepskin, these ugg sundance boots do not get channelled or torn even admitting you bend and extend them times.

fgdg said...

I know someone in the world is waiting for me, although I've no idea of who he is. But I feel happy every day for this 4story Gold, If a woman is not sexy, she needs emotion; if she is not emotional, she needs reason 4story Luna, in this world, only those men who really feel happy can give women happiness 4story Gold.

fgdg said...

I know someone in the world is waiting for me, although I've no idea of who he is. But I feel happy every day for this 4story Gold, If a woman is not sexy, she needs emotion; if she is not emotional, she needs reason 4story Luna, in this world, only those men who really feel happy can give women happiness 4story Gold.

fgdg said...

Life is not measured by the number of breaths we take Runescape Gold, but by the moments that take our breath away rs gold. I have a simple philosophy: Fill what's empty Runescape Gold.