Re: libpq WSACleanup is not needed - Mailing list pgsql-hackers

From Andrew Chernow
Subject Re: libpq WSACleanup is not needed
Date
Msg-id 49709272.8070906@esilo.com
Whole thread Raw
In response to Re: libpq WSACleanup is not needed  (Magnus Hagander <magnus@hagander.net>)
Responses Re: libpq WSACleanup is not needed  (Magnus Hagander <magnus@hagander.net>)
List 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
existingapps 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.
Thinkabout 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
notsure 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/


pgsql-hackers by date:

Previous
From: Magnus Hagander
Date:
Subject: Re: libpq WSACleanup is not needed
Next
From: Alvaro Herrera
Date:
Subject: Re: libpq WSACleanup is not needed