Re: libpq WSACleanup is not needed

From: Andrew Chernow <ac(at)esilo(dot)com>
To: Magnus Hagander <magnus(at)hagander(dot)net>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: libpq WSACleanup is not needed
Date: 2009-01-16 13:58:10
Message-ID: 49709272.8070906@esilo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Magnus Hagander wrote:
> Andrew Chernow wrote:
>> WSACleanup is not really needed during a PQfinish. Its horribly slow if
>> the library ref count is 0 and it actually unloads the winsock library,
>> adds 225ms to PQfinish.
>>
>> Solution:
>> A) Call WSAStartup once and never clean it up. When the app dies, so do
>> the ref counts and winsock is automatically unloaded.
>>
>> B) Have a way of specifying the behavior, the way it is now or tell
>> libpq to not initialize wsa at all (kinda like ssl init callbacks).
>> Leave it up to the application.
>>
>> I think the WSA startup/cleanup stuff is silly. If I dynamically link
>> with a DLL, I want it automatically loaded and cleaned up.
>>
>> Worst case, your app makes lots of connections to different backends.
>> So, it is constantly doing PQconnectdb and PQfinish; only has a single
>> conn open at a time. This means its constantly loading and unloading
>> winsock.
>
> Option A will make us leak the reference to it though, won't it? And we
> are supposed to clean up after ourselves...
>

Personally, I don't think its the job of libpq to call wsa startup or shutdown.
Pulling it out now will surely break existing apps and piss people off, so I
don't think this is an option. If anything, it should not be a per conn thing.
Its really a library wide thing. Think about it, there is no gain in doing
this per conn. Not to mention, I just found a major issue with it.

> Now, if we actually had libpq_init()/uninit() or something like it, it
> would make sense to move it there. But I'm not sure we want to just leak
> the reference. But I'm not entirely convinced either way :-)
>

There is probably someone out there that wants wsa to completely unload when
there done using libpq (maybe its a long-lived app like an NT service). The
only thing a leaked ref would do is never unload wsa until the app exited. So,
this is probably bad behavior.

libpq_init() is where an app should elect what libpq should load. If init is
never called, everything should work as it does now. Something like that.
openssl init stuff should be controlled by the same function, maybe some other
components. Auto-init'n is a nice feature most of the time, but it suffers from
ASSuming how and when an app wants to perfom the init.

> If you want to override this behavior today, you can just call
> WSAStartup() in your application, and it should never happen. Right?

Exactely what we did.

--
Andrew Chernow
eSilo, LLC
every bit counts
http://www.esilo.com/

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Alvaro Herrera 2009-01-16 14:17:10 Re: libpq WSACleanup is not needed
Previous Message Magnus Hagander 2009-01-16 13:40:32 Re: libpq WSACleanup is not needed