Thread: Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Magnus Hagander wrote:

> Add putenv support for msvcrt from Visual Studio 2013
>
> This was missed when VS 2013 support was added.
>
> Michael Paquier
>
> Branch
> ------
> master
>
> Details
> -------
> http://git.postgresql.org/pg/commitdiff/9f633b404cb3be6139f8dfdea00538489ffef9ab

Just noticed something. This DLL detection by name has never worked in
debug builds where the DLL names end in "d". Is that important?

--
Christian



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Magnus Hagander
Date:


On Sun, Apr 24, 2016 at 9:56 PM, Christian Ullrich <chris@chrullrich.net> wrote:
* Magnus Hagander wrote:

Add putenv support for msvcrt from Visual Studio 2013

This was missed when VS 2013 support was added.

Michael Paquier

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/9f633b404cb3be6139f8dfdea00538489ffef9ab

Just noticed something. This DLL detection by name has never worked in debug builds where the DLL names end in "d". Is that important?

That's an interesting point.  I guess our release builds are never with debugging info - but could it make the buildfarm "wrong"?

Fixing it should probably be as easy as trying each dll with the specified name and also with a "d" as a suffix?

--

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Magnus Hagander wrote:

> On Sun, Apr 24, 2016 at 9:56 PM, Christian Ullrich <chris@chrullrich.net>
> wrote:
>
>> * Magnus Hagander wrote:
>>
>> Add putenv support for msvcrt from Visual Studio 2013
>> http://git.postgresql.org/pg/commitdiff/9f633b404cb3be6139f8dfdea00538489ffef9ab

>> Just noticed something. This DLL detection by name has never worked in
>> debug builds where the DLL names end in "d". Is that important?

> That's an interesting point.  I guess our release builds are never with
> debugging info - but could it make the buildfarm "wrong"?
>
> Fixing it should probably be as easy as trying each dll with the specified
> name and also with a "d" as a suffix?

I think so, yes.

Personally, I would have expected that at least the debug/release DLLs
of a single CRT version would somehow share their environment, but I
tried it and they don't.

--
Christian





Re: [COMMITTERS] pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Andrew Dunstan
Date:

On 04/25/2016 09:27 AM, Christian Ullrich wrote:
> * Magnus Hagander wrote:
>
>> On Sun, Apr 24, 2016 at 9:56 PM, Christian Ullrich
>> <chris@chrullrich.net>
>> wrote:
>>
>>> * Magnus Hagander wrote:
>>>
>>> Add putenv support for msvcrt from Visual Studio 2013
>>> http://git.postgresql.org/pg/commitdiff/9f633b404cb3be6139f8dfdea00538489ffef9ab
>>>
>
>>> Just noticed something. This DLL detection by name has never worked in
>>> debug builds where the DLL names end in "d". Is that important?
>
>> That's an interesting point.  I guess our release builds are never with
>> debugging info - but could it make the buildfarm "wrong"?
>>
>> Fixing it should probably be as easy as trying each dll with the
>> specified
>> name and also with a "d" as a suffix?
>
> I think so, yes.
>
> Personally, I would have expected that at least the debug/release DLLs
> of a single CRT version would somehow share their environment, but I
> tried it and they don't.
>

What if both are present? Is a release build prevented from loading a
debug dll and vice versa?

Alternatively, can we detect at compile time if we are a debug build and
if so add the suffix?

cheers

andrew



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Andrew Dunstan wrote:

> On 04/25/2016 09:27 AM, Christian Ullrich wrote:
>> * Magnus Hagander wrote:
>>
>>> On Sun, Apr 24, 2016 at 9:56 PM, Christian Ullrich
>>> <chris@chrullrich.net> wrote:

>>>> Just noticed something. This DLL detection by name has never worked in
>>>> debug builds where the DLL names end in "d". Is that important?
>>
>>> That's an interesting point.  I guess our release builds are never with
>>> debugging info - but could it make the buildfarm "wrong"?

> What if both are present? Is a release build prevented from loading a
> debug dll and vice versa?

Debug and release are simply two separate CRTs. If your process contains
a module that needs the one, and another that needs the other, you will
have both loaded at once.

I had hoped they might share state, but they don't, at least as far as
putenv()/getenv() are concerned.

> Alternatively, can we detect at compile time if we are a debug build and
> if so add the suffix?

No; same reason as above.

--
Christian



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Christian Ullrich wrote:

> * Andrew Dunstan wrote:

>> What if both are present? Is a release build prevented from loading a
>> debug dll and vice versa?
>
> Debug and release are simply two separate CRTs. If your process contains
> a module that needs the one, and another that needs the other, you will
> have both loaded at once.

pgwin32_putenv() is the gift that keeps giving.


According to its comment, it is not required that all modules exchanging
calls with postgres.exe have to be built with the same CRT version (100,
110, 120, etc.). Is it?

If not, the logic that remembers negative results from GetModuleHandle()
(i.e. gives up forever on each possible CRT once it does not find it) is
wrong even without considering the debug/release split. If we load a
compiled extension built with a CRT we have not seen yet, _after_ the
first call to pgwin32_putenv(), that module's CRT's view of its
environment will be frozen because we will never attempt to update it.

If that code is in there because it has some noticeable performance
advantage, the negative results could probably be reset in SQL LOAD,
rather than just not remembering them anymore.


This comment is also incomplete then:

    /*
     * Module loaded, but we did not find the function last time.
     * We're not going to find it this time either...
     */

This else branch is also taken if the module handle is set to
INVALID_HANDLE_VALUE because the module was not found in a previous call.


If it can happen that a CRT DLL is unloaded before the process exits,
and we cached the module handle while it was loaded, and later
pgwin32_putenv() is called, that won't end well for the process. This
might be a bit far-fetched; I have to see if I can actually make it happen.

One situation I can think of where this could occur is if an extension
loaded with LOAD creates a COM in-proc server from a DLL built with yet
another CRT, and when that object is released, either FreeLibrary()
(transitively) or CoFreeUnusedLibraries() (directly) boots that CRT (if
they do; it's possible that a CRT, once loaded, stays loaded.)


Finally: A nonzero handle returned from GetModuleHandle() is not
something that needs to be CloseHandle()d. It is not actually a handle,
but a pointer to the base (load) address of the module, although the
documentation for GetModuleHandle() is careful not to admit that.

The value it is compared against to see whether we have seen the module
before should be NULL, not 0.


It's getting a bit late for me today, but I will do the necessary
experimentation and try to come up with a POC patch to fix whatever of
the above I can actually prove to be real. Should anyone know for sure
that I'm completely off track on something, better yet, everything,
please let me know.

I should finish thinking before posting, then I would not have to reply
to myself so often.

--
Christian



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Christian Ullrich wrote:

> wrong even without considering the debug/release split. If we load a
> compiled extension built with a CRT we have not seen yet, _after_ the
> first call to pgwin32_putenv(), that module's CRT's view of its
> environment will be frozen because we will never attempt to update it.

Four patches attached:

master --- 0001 --- 0002 --- 0003
                          \
                           `- 0004

0001 is just some various fixes to set the stage.

0002 fixes this "load race" by not remembering a negative result
anymore. However, ...

> If it can happen that a CRT DLL is unloaded before the process exits,
> and we cached the module handle while it was loaded, and later
> pgwin32_putenv() is called, that won't end well for the process. This
> might be a bit far-fetched; I have to see if I can actually make it happen.

... this *can* and does happen, so fixing the load race alone is not
enough. 0004 fixes the unload race as well, by dropping the entire DLL
handle/_putenv pointer cache from the function and going through the
list of DLLs each time.

I tested this with a project
(<https://bitbucket.org/chrullrich/pgputenv-demo>) that contains two DLLs:

- The first one is built with VS 2013 (debug), as is the server. It
   does not matter what it is built with, except it must not be the same
   as the second DLL. It exports a single function callable from SQL.

- The second one is built with VS 2015 (debug), and again, the exact
   CRT is not important as long as it is not the same as the server
   or the first DLL.

The function does this:

1. It loads the second DLL. This brings in ucrtbased.dll as well.
2. It calls putenv().
3. It unloads the second DLL. This also causes ucrtbased.dll to be
    unloaded because it is not needed anymore.
4. It calls putenv() again.

    - With current master, this works, because pgwin32_putenv(),
      after the first call somewhere early during backend startup,
      never looks for ucrtbased again (including in step 2).

    - With patch 0002 applied, it crashes because pgwin32_putenv(),
      having detected ucrtbased.dll and cached its HMODULE during
      the call in step 2 above, reuses these values after the DLL
      is long gone.

    - With patch 0004 applied as well, it works again because no
      caching is done anymore.

Even with patch 0004, there is still a race condition between the main
thread and a theoretical additional thread created by some other
component that unloads some CRT between GetProcAddress() and the
_putenv() call, but that is hardly fixable.

The fact that master looks like it does means that there have not been
many (or any) complaints about missing cross-module environment
variables. If nobody ever needs to see a variable set elsewhere, we have
a very simple solution: Why don't we simply throw out the whole #ifdef
_MSC_VER block?

There is another possible fix, ugly as sin, if we really need to keep
the whole environment update machinery *and* cannot do the full loop
each time. Patch 0003 pins each CRT when we see it for the first time.
GET_MODULE_HANDLE_EX_FLAG_PIN is documented as "The module stays loaded
until the process is terminated, no matter how many times FreeLibrary is
called", so the unload race cannot occur anymore.

--
Christian


Attachment

Re: [COMMITTERS] pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Wed, Apr 27, 2016 at 2:39 AM, Christian Ullrich <chris@chrullrich.net> wrote:
> * Christian Ullrich wrote:
>
>> wrong even without considering the debug/release split. If we load a
>> compiled extension built with a CRT we have not seen yet, _after_ the
>> first call to pgwin32_putenv(), that module's CRT's view of its
>> environment will be frozen because we will never attempt to update it.
>
>
> Four patches attached:
>
> master --- 0001 --- 0002 --- 0003
>                          \
>                           `- 0004
>
> 0001 is just some various fixes to set the stage.
>
> 0002 fixes this "load race" by not remembering a negative result anymore.
> However, ...

From 0001, which does not apply anymore on HEAD because of the
integration with MS2015:
                    if (rtmodules[i].putenvFunc == NULL)
                    {
-                       CloseHandle(rtmodules[i].hmodule);
                        rtmodules[i].hmodule = INVALID_HANDLE_VALUE;
                        continue;
                    }
Nice catch. This portion is a bug and should be backpatched. As far as
I can read from MS docs, GetModuleHandle() retrieves an existing
handle so there is no need to free it. Or that would fail.

And actually, by looking at those patches, isn't it a dangerous
practice to be able to load multiple versions of the same DLL routines
in the same workspace? I have personally very bad souvenirs with that,
and particularly loading multiple versions of msvcr into the same
workspace can cause unwanted crashes and corruptions of processes. In
short I mean this thing: https://en.wikipedia.org/wiki/DLL_Hell.

So, shouldn't we first make the DLL list a little bit more severe
depending on the value of _MSC_VER? I would mean something like that:
#ifdef _MSC_VER >= 1900
{"ucrtbase",    NULL,   NULL},
#elif _MSC_VER >= 1800
{"msvcr120",    NULL,   NULL},
#elif etc, etc.
[...]
#endif

This would require modules to be built using the same msvc version as
the core server, but that's better than just plain crash if loaded
DLLs corrupt the stack. Am I missing something?
--
Michael


Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Michael Paquier wrote:

 > On Wed, Apr 27, 2016 at 2:39 AM, Christian Ullrich
<chris@chrullrich.net> wrote:

 >> * Christian Ullrich wrote:

 > And actually, by looking at those patches, isn't it a dangerous
 > practice to be able to load multiple versions of the same DLL routines
 > in the same workspace? I have personally very bad souvenirs with that,

No, it is exactly what the version-specific CRTs are meant to allow.
Each module uses the CRT version it needs, and they don't share any
state, so absent bugs, they cannot conflict.

Of the processes currently running on my system, 25 have more than one
CRT loaded (one has three, the others two).

 > and particularly loading multiple versions of msvcr into the same
 > workspace can cause unwanted crashes and corruptions of processes. In
 > short I mean this thing: https://en.wikipedia.org/wiki/DLL_Hell.

That was about DLLs existing in different versions with the same file
name, and installers replacing them with their own, leading to problems
with other applications that expected to load their preferred version.
This does not apply to the multiple-CRT situation, because all minor
versions of a given CRT are supposed to be ABI compatible.

 > So, shouldn't we first make the DLL list a little bit more severe
 > depending on the value of _MSC_VER? I would mean something like that:
 > #ifdef _MSC_VER >= 1900
 > {"ucrtbase",    NULL,   NULL},
 > #elif _MSC_VER >= 1800
 > {"msvcr120",    NULL,   NULL},
 > #elif etc, etc.
 > [...]
 > #endif
 >
 > This would require modules to be built using the same msvc version as
 > the core server, but that's better than just plain crash if loaded
 > DLLs corrupt the stack. Am I missing something?

Yes: This turns (this part of) pgwin32_putenv() into a great big NOP. We
call putenv() anyway on the very last line of the function, so if we
require common-CRT builds, that call alone (together with the
SetEnvironmentVariable() just above) is sufficient.

That said, introducing this requirement would be a very significant
change. I'm not sure how many independently maintained compiled
extensions there are, but this would mean that their developers would
have to have the matching VS versions for every PG distribution they
want to support. Even if that's just EDB, it still is a lot of effort.

My conclusion from April stands:

 > The fact that master looks like it does means that there have not been
 > many (or any) complaints about missing cross-module environment
 > variables. If nobody ever needs to see a variable set elsewhere, we
 > have a very simple solution: Why don't we simply throw out the whole
 > #ifdef _MSC_VER block?

--
Christian



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Thu, Sep 1, 2016 at 4:03 PM, Christian Ullrich <chris@chrullrich.net> wrote:
> * Michael Paquier wrote:
>
>> On Wed, Apr 27, 2016 at 2:39 AM, Christian Ullrich <chris@chrullrich.net>
>> wrote:
>
>>> * Christian Ullrich wrote:
>
>> And actually, by looking at those patches, isn't it a dangerous
>> practice to be able to load multiple versions of the same DLL routines
>> in the same workspace? I have personally very bad souvenirs with that,
>
> No, it is exactly what the version-specific CRTs are meant to allow. Each
> module uses the CRT version it needs, and they don't share any state, so
> absent bugs, they cannot conflict.

Hm. OK.

> That said, introducing this requirement would be a very significant change.
> I'm not sure how many independently maintained compiled extensions there
> are, but this would mean that their developers would have to have the
> matching VS versions for every PG distribution they want to support. Even if
> that's just EDB, it still is a lot of effort.

Yes. FWIW in my stuff everything gets compiled based on the same VS
version and bundled in the same msi, including a set of extensions
compiled from source, but perhaps my sight is too narrow in this
area... Well let's forget about that.

> My conclusion from April stands:
>
>> The fact that master looks like it does means that there have not been
>> many (or any) complaints about missing cross-module environment
>> variables. If nobody ever needs to see a variable set elsewhere, we
>> have a very simple solution: Why don't we simply throw out the whole
>> #ifdef _MSC_VER block?

Looking at the commit logs and 741e4ad7 that does not sound like a good idea.

+                   if (!rtmodules[i].pinned)
+                   {
+                       HMODULE tmp;
+                       BOOL res = GetModuleHandleEx(
+                               GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
+                                   | GET_MODULE_HANDLE_EX_FLAG_PIN,
+                               (LPCTSTR)rtmodules[i].hmodule,
+                               &tmp);
+                       rtmodules[i].pinned = !!res;
+                   }
It is better to avoid !!. See for example 1a7a436 that avoided
problems with VS2015 as far as I recall.

In order to avoid any problems with the load and unload windows, my
bet goes for 0001, 0002 and 0003, with the last two patches merged
together, 0001 being only a set of independent fixes. That's ugly, but
that looks the safest course of actions. I have rebased/rewritten the
patches as attached. Thoughts?
--
Michael

Attachment

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Michael Paquier wrote:

> On Thu, Sep 1, 2016 at 4:03 PM, Christian Ullrich <chris@chrullrich.net> wrote:

>> My conclusion from April stands:
>>
>>> The fact that master looks like it does means that there have not been
>>> many (or any) complaints about missing cross-module environment
>>> variables. If nobody ever needs to see a variable set elsewhere, we
>>> have a very simple solution: Why don't we simply throw out the whole
>>> #ifdef _MSC_VER block?
>
> Looking at the commit logs and 741e4ad7 that does not sound like a good idea.

Well, I still maintain that if it doesn't work and has never worked,
getting rid of it is better than making it work six years after the
fact. OTOH, there may have been cases where it did actually work,
perhaps those gnuwin32 libraries that were mentioned in the comment
before it was changed in that commit above, if they were loaded before
the first call to the function.

OTTH, wouldn't it be funny if fixing it actually broke something that
worked accidentally because it *didn't* get the updated environment? I
think that is at least as likely as suddenly getting excited reports
that something now works that hasn't before.

> It is better to avoid !!. See for example 1a7a436 that avoided
> problems with VS2015 as far as I recall.

Agreed, thanks for noticing. This change creates a warning, however,
because GetModuleHandleEx() returns BOOL, not HMODULE. Updated 0003
attached, simplified over my original one.

> In order to avoid any problems with the load and unload windows, my
> bet goes for 0001, 0002 and 0003, with the last two patches merged
> together, 0001 being only a set of independent fixes. That's ugly, but
> that looks the safest course of actions. I have rebased/rewritten the
> patches as attached. Thoughts?

In lieu of convincing you to drop the entire thing, yes, that looks
about right, except for the BOOL thing.

--
Christian


Attachment

Re: [COMMITTERS] pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Craig Ringer
Date:

On 6 Sep. 2016 15:12, "Michael Paquier" <michael.paquier@gmail.com> wrote:
>
> On Thu, Sep 1, 2016 at 4:03 PM, Christian Ullrich <chris@chrullrich.net> wrote:
>
> > That said, introducing this requirement would be a very significant change.
> > I'm not sure how many independently maintained compiled extensions there
> > are, but this would mean that their developers would have to have the
> > matching VS versions for every PG distribution they want to support. Even if
> > that's just EDB, it still is a lot of effort.
>
> Yes. FWIW in my stuff everything gets compiled based on the same VS
> version and bundled in the same msi, including a set of extensions
> compiled from source, but perhaps my sight is too narrow in this
> area... Well let's forget about that.

3rd party extensions may not and may not be able to. Most obvious example is people building things with mingw.

This is just expected to work on win32. Breaking this assumption will cause pain. Requiring a single unified C runtime across the process isn't viable. It isn't like Unix. You might have a legacy DLL compiled with Borland C that you're wrapping up to expose as an extension using mingw to link into a Pg compiled with MSVC.

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Tue, Sep 6, 2016 at 5:36 PM, Christian Ullrich <chris@chrullrich.net> wrote:
> * Michael Paquier wrote:
>> In order to avoid any problems with the load and unload windows, my
>> bet goes for 0001, 0002 and 0003, with the last two patches merged
>> together, 0001 being only a set of independent fixes. That's ugly, but
>> that looks the safest course of actions. I have rebased/rewritten the
>> patches as attached. Thoughts?
>
>
> In lieu of convincing you to drop the entire thing, yes, that looks about
> right, except for the BOOL thing.

Yes, right. Thanks. Patch 0001 is definitely something that should be
applied and backpatched, the CloseHandle() call is buggy. Now 0002 and
0003 are improving things, but there have been no reports regarding
problems in this area, so this could just be applied to master I
guess. Christian, does that sound right?

By the way, how is it decided that a DLL gets unloaded in a process if
it is not pinned? Is that something the OS decides?
--
Michael


Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Michael Paquier wrote:

> On Tue, Sep 6, 2016 at 5:36 PM, Christian Ullrich <chris@chrullrich.net> wrote:
>> * Michael Paquier wrote:

>>> In order to avoid any problems with the load and unload windows, my
>>> bet goes for 0001, 0002 and 0003, with the last two patches merged
>>> together, 0001 being only a set of independent fixes. That's ugly, but
>>> that looks the safest course of actions. I have rebased/rewritten the
>>> patches as attached. Thoughts?
>>
>> In lieu of convincing you to drop the entire thing, yes, that looks about
>> right, except for the BOOL thing.
>
> Yes, right. Thanks. Patch 0001 is definitely something that should be
> applied and backpatched, the CloseHandle() call is buggy. Now 0002 and
> 0003 are improving things, but there have been no reports regarding
> problems in this area, so this could just be applied to master I
> guess. Christian, does that sound right?

Yes.

> By the way, how is it decided that a DLL gets unloaded in a process if
> it is not pinned? Is that something the OS decides?

Reference counting in LoadLibrary() and FreeLibrary(), among other
places. For example, GetModuleHandleEx() (but not the non-Ex) will by
default increment the counter.

--
Christian


Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
(Just remembered to remove pgsql-committers here).

On Tue, Sep 6, 2016 at 9:21 PM, Christian Ullrich <chris@chrullrich.net> wrote:
> * Michael Paquier wrote:
>
>> On Tue, Sep 6, 2016 at 5:36 PM, Christian Ullrich <chris@chrullrich.net>
>> wrote:
>>>
>>> * Michael Paquier wrote:
>
>>>> In order to avoid any problems with the load and unload windows, my
>>>> bet goes for 0001, 0002 and 0003, with the last two patches merged
>>>> together, 0001 being only a set of independent fixes. That's ugly, but
>>>> that looks the safest course of actions. I have rebased/rewritten the
>>>> patches as attached. Thoughts?
>>>
>>>
>>> In lieu of convincing you to drop the entire thing, yes, that looks about
>>> right, except for the BOOL thing.
>>
>>
>> Yes, right. Thanks. Patch 0001 is definitely something that should be
>> applied and backpatched, the CloseHandle() call is buggy. Now 0002 and
>> 0003 are improving things, but there have been no reports regarding
>> problems in this area, so this could just be applied to master I
>> guess. Christian, does that sound right?
>
> Yes.

OK, let's get to the next step of the game and get a committer to look
at this patch.
-- 
Michael



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Tue, Sep 6, 2016 at 9:36 PM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> OK, let's get to the next step of the game and get a committer to look
> at this patch.

Moved to next CF. It would be good to get a committer on this one. We
have come on a conclusion on what to do. Actually, 0001 can be just
HEAD-only per the lack of complaints.
-- 
Michael



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Noah Misch
Date:
On Tue, Apr 26, 2016 at 07:39:29PM +0200, Christian Ullrich wrote:
> * Christian Ullrich wrote:
> 
> >wrong even without considering the debug/release split. If we load a
> >compiled extension built with a CRT we have not seen yet, _after_ the
> >first call to pgwin32_putenv(), that module's CRT's view of its
> >environment will be frozen because we will never attempt to update it.
> 
> Four patches attached:
> 
> master --- 0001 --- 0002 --- 0003
>                          \
>                           `- 0004
> 
> 0001 is just some various fixes to set the stage.

Patch 1 looks good, except that it should be three patches:

- cosmetic parts: change whitespace and s/0/NULL/
- remove CloseHandle() call
- probe for debug CRT modules, not just release CRT modules

Please split along those boundaries.  I plan to back-patch all of that.  I've
seen some gettext builds mysteriously ignore "SET lc_messages = ..."; ignoring
debug CRTs could have been the cause.

> I tested this with a project
> (<https://bitbucket.org/chrullrich/pgputenv-demo>) that contains two DLLs:

That's a pithy test; thanks for assembling it.

> Even with patch 0004, there is still a race condition between the main
> thread and a theoretical additional thread created by some other component
> that unloads some CRT between GetProcAddress() and the _putenv() call, but
> that is hardly fixable.

I think you can fix it by abandoning GetModuleHandle() in favor of
GetModuleHandleEx() + GetProcessAddress() + FreeLibrary().  I recommend also
moving the SetEnvironmentVariable() call before the putenv calls.  That way,
if a CRT loads while pgwin32_putenv() is executing, the newly-loaded CRT will
always have the latest value.  (I'm assuming CRTs populate their environment
from GetEnvironmentStrings(), because I can't think of an alternative.)

As a separate patch, I am inclined to remove the "#ifdef _MSC_VER" directive,
activating its enclosed code under all compilers.  A MinGW-built postgres.exe
has the same need to update all CRTs.

> The fact that master looks like it does means that there have not been many
> (or any) complaints about missing cross-module environment variables. If
> nobody ever needs to see a variable set elsewhere, we have a very simple
> solution: Why don't we simply throw out the whole #ifdef _MSC_VER block?

pgwin32_putenv() originated, in commit 0154345, to make "SET lc_messages =
..." effective when gettext uses a different CRT from postgres.exe.  I suspect
it also makes krb_server_keyfile effective when GSS uses a different CRT.
Those are achievements worth keeping.  I'm not surprised about the lack of
complaints, because environment variables don't often change after backend
startup.  Here are some ways one could notice the difference between master
and patches 2+3 or 2+4:

- Use shared_preload_libraries to load a module that reads LC_CTYPE or LC_COLLATE.  CheckMyDatabase() sets those
variablessubsequent to process_shared_preload_libraries().
 

- Load, at any time, a module that reads LC_MESSAGES.  There's little reason to read that variable outside of gettext.
Amodule could use a gettext DLL other than the postgres.exe gettext DLL, but such a module would need to work around
pg_bindtextdomain()always using the postgres.exe gettext.
 

- Load, at any time, a module that itself changes environment variables, other than LC_MESSAGES, after backend startup.
I suspect PL/Python suffices.
 

Those are plausible scenarios, but they're sufficiently specialized that
problems could lie unnoticed or undiagnosed for years.  I lean against
back-patching anything from patches 2, 3 or 4.

> There is another possible fix, ugly as sin, if we really need to keep the
> whole environment update machinery *and* cannot do the full loop each time.
> Patch 0003 pins each CRT when we see it for the first time.
> GET_MODULE_HANDLE_EX_FLAG_PIN is documented as "The module stays loaded
> until the process is terminated, no matter how many times FreeLibrary is
> called", so the unload race cannot occur anymore.

I prefer the simplicity of abandoning the cache (patch 4), if it performs
decently.  Would you compare the performance of patch 1, patches 1+2+3, and
patches 1+2+4?  This should measure the right thing (after substituting
@libdir@ for your environment):

CREATE FUNCTION putenv(text)  RETURNS void  AS '@libdir@/regress.dll', 'regress_putenv'  LANGUAGE C STRICT;
\timing on
SELECT count(putenv('foo=' || n)) FROM generate_series(1,1000000) t(n);

(I'm interested for the sake of backend startup time.  I recall nine putenv()
in every backend startup, seven in main() and two in CheckMyDatabase().)

Thanks,
nm



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Noah Misch wrote:

> On Tue, Apr 26, 2016 at 07:39:29PM +0200, Christian Ullrich
> wrote:

> > * Christian Ullrich wrote:

> Patch 1 looks good, except that it should be three patches:
> 
> - cosmetic parts: change whitespace and s/0/NULL/
> - remove CloseHandle() call
> - probe for debug CRT modules, not just release CRT modules

Attached as 0001, 0002, 0003, in that order.

0004 is what used to be 0002, it disables caching of "DLL not 
loaded" results.

> I recommend also moving the SetEnvironmentVariable() call before
> the putenv calls.  That way, if a CRT loads while pgwin32_putenv()
> is executing, the newly-loaded CRT will always have the latest
> value.

Agreed, attached as 0005.

0006 was previously known as 0004, removing all caching.

> > Even with patch 0004, there is still a race condition between
> > the main thread and a theoretical additional thread created by
> > some other component that unloads some CRT between
> > GetProcAddress() and the _putenv() call, but that is hardly
> > fixable.
> 
> I think you can fix it by abandoning GetModuleHandle() in favor
> of GetModuleHandleEx() + GetProcessAddress() + FreeLibrary(). 

Attached as 0007.

> > There is another possible fix, ugly as sin, if we really need
> > to keep the whole environment update machinery *and* cannot do 
> > the full loop each time. Patch 0003 pins each CRT when we see 
> > it for the first time.

This is now 0008.

Patch topology: master --- 1 .. 5 --- 6 --- 7
                                  \
                                   `- 8

> I prefer the simplicity of abandoning the cache (patch 4), if it
> performs decently.  Would you compare the performance of patch 1,
> patches 1+2+3, and patches 1+2+4?  This should measure the right 
> thing (after substituting @libdir@ for your environment):

Tested with release builds; Core i7-6700K (quad/HT; 4000 MHz).
I did three runs each, and they were always within 0.5 % of each
other's run time.

- patch 1 (now 1-3):         24 μs/iteration (24 s for 1e6)
- patch 1+2+3 (now 1-5 + 8): 29 μs/iteration
- patch 1+2+4 (now 1-7):     30 μs/iteration

I also did a debug build with 1+2+4 that came in at 84 μs/iteration.

--
Christian

... now how do I get all the dangling debris out of the repo ...

Attachment

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Wed, Nov 16, 2016 at 12:45 PM, Christian Ullrich
<chris@chrullrich.net> wrote:
> I also did a debug build with 1+2+4 that came in at 84 μs/iteration.

Moved to next CF. Christian, perhaps this patch should have an extra
round of reviews?
--
Michael



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* Michael Paquier wrote:
> On Wed, Nov 16, 2016 at 12:45 PM, Christian Ullrich> <chris@chrullrich.net> wrote:>> I also did a debug build with
1+2+4that came in at 84 μs/iteration.>> Moved to next CF. Christian, perhaps this patch should have an extra> round of
reviews?

It is significantly different from the last version, so yes, of course.

-- 
Christian




Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Tue, Nov 29, 2016 at 08:45:13PM +0100, Christian Ullrich wrote:
> * Michael Paquier wrote:
>
> > On Wed, Nov 16, 2016 at 12:45 PM, Christian Ullrich
> > <chris@chrullrich.net> wrote:
> >> I also did a debug build with 1+2+4 that came in at 84 μs/iteration.
> >
> > Moved to next CF. Christian, perhaps this patch should have an extra
> > round of reviews?
>
> It is significantly different from the last version, so yes, of course.

Patches 0001 (Cosmetic fixes), 0002 (Remove unnecessary CloseHandle)
and 0003 (support for debug CRTs) look in good shape to me. 0004 (Fix
load race) is 0002 from the previous set, and this change makes sense
in itself.

With 0005 I am seeing a compilation failure: you need to have the
declarations in the _MSC_VER block at the beginning of the routine. It
would be nice to mention in a code comment that this what Noah has
mentioned upthread: if a CRT loads while pgwin32_putenv() is
executing, the newly-loaded CRT will always have the latest value.

Based on that 0006 will need a rebase, nothing huge though.

Removing the caching per the measurements upthread is causing a 1us
regression compared to the full set. Let's do things simple then! This
smells like noise.
--
Michael



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Christian Ullrich
Date:
* From: Michael Paquier

> With 0005 I am seeing a compilation failure: you need to have the
> declarations in the _MSC_VER block at the beginning of the routine. It

Sorry, too used to C++.

> would be nice to mention in a code comment that this what Noah has
> mentioned upthread: if a CRT loads while pgwin32_putenv() is
> executing, the newly-loaded CRT will always have the latest value.

I fixed the existing comment by removing the last sentence that is in the wrong place now, but I don't see the point in
suddenlystarting to explain why it is done this way and not the other.
 

> Based on that 0006 will need a rebase, nothing huge though.

Done, new revisions attached.

-- 
Christian


Attachment

Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Thu, Dec 1, 2016 at 1:24 AM, Christian Ullrich <chris@chrullrich.net> wrote:
> * From: Michael Paquier
>
>> With 0005 I am seeing a compilation failure: you need to have the
>> declarations in the _MSC_VER block at the beginning of the routine. It
>
> Sorry, too used to C++.
>
>> would be nice to mention in a code comment that this what Noah has
>> mentioned upthread: if a CRT loads while pgwin32_putenv() is
>> executing, the newly-loaded CRT will always have the latest value.
>
> I fixed the existing comment by removing the last sentence that is in the wrong place now, but I don't see the point
insuddenly starting to explain why it is done this way and not the other.
 
>
>> Based on that 0006 will need a rebase, nothing huge though.
>
> Done, new revisions attached.

Okay, switched as ready for committer.
-- 
Michael



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Robert Haas
Date:
On Wed, Nov 30, 2016 at 2:56 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> On Tue, Nov 29, 2016 at 08:45:13PM +0100, Christian Ullrich wrote:
>> * Michael Paquier wrote:
>>
>> > On Wed, Nov 16, 2016 at 12:45 PM, Christian Ullrich
>> > <chris@chrullrich.net> wrote:
>> >> I also did a debug build with 1+2+4 that came in at 84 μs/iteration.
>> >
>> > Moved to next CF. Christian, perhaps this patch should have an extra
>> > round of reviews?
>>
>> It is significantly different from the last version, so yes, of course.
>
> Patches 0001 (Cosmetic fixes), 0002 (Remove unnecessary CloseHandle)
> and 0003 (support for debug CRTs) look in good shape to me. 0004 (Fix
> load race) is 0002 from the previous set, and this change makes sense
> in itself.

0001 looks fine insofar as it makes things consistent regarding 0 vs.
NULL, but the whitespace changes will be reverted by pgindent.  (I
just tested.)

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Noah Misch
Date:
On Wed, Nov 16, 2016 at 08:45:20PM +0000, Christian Ullrich wrote:
> * Noah Misch wrote:
> > I prefer the simplicity of abandoning the cache (patch 4), if it
> > performs decently.  Would you compare the performance of patch 1,
> > patches 1+2+3, and patches 1+2+4?  This should measure the right 
> > thing (after substituting @libdir@ for your environment):
> 
> Tested with release builds; Core i7-6700K (quad/HT; 4000 MHz).
> I did three runs each, and they were always within 0.5 % of each
> other's run time.
> 
> - patch 1 (now 1-3):         24 μs/iteration (24 s for 1e6)
> - patch 1+2+3 (now 1-5 + 8): 29 μs/iteration
> - patch 1+2+4 (now 1-7):     30 μs/iteration

Thanks for measuring; 6μs*9=54μs is a negligible addition to Windows backend
startup time.

On Wed, Nov 30, 2016 at 04:24:34PM +0000, Christian Ullrich wrote:
> * From: Michael Paquier
> > would be nice to mention in a code comment that this what Noah has
> > mentioned upthread: if a CRT loads while pgwin32_putenv() is
> > executing, the newly-loaded CRT will always have the latest value.
> 
> I fixed the existing comment by removing the last sentence that is in the wrong place now, but I don't see the point
insuddenly starting to explain why it is done this way and not the other.
 
> 
> > Based on that 0006 will need a rebase, nothing huge though.
> 
> Done, new revisions attached.

I committed patches 1-7 with some comment changes, a pgindent, and other
cosmetic trivia.  (The file was pgindent-clean before this work.  Patch 6
still achieved the more-compact formatting you sought.)



Re: pgsql: Add putenv support for msvcrt from Visual Studio 2013

From
Michael Paquier
Date:
On Sun, Dec 4, 2016 at 5:58 AM, Noah Misch <noah@leadboat.com> wrote:
> On Wed, Nov 30, 2016 at 04:24:34PM +0000, Christian Ullrich wrote:
>> * From: Michael Paquier
>> > would be nice to mention in a code comment that this what Noah has
>> > mentioned upthread: if a CRT loads while pgwin32_putenv() is
>> > executing, the newly-loaded CRT will always have the latest value.
>>
>> I fixed the existing comment by removing the last sentence that is in the wrong place now, but I don't see the point
insuddenly starting to explain why it is done this way and not the other.
 
>>
>> > Based on that 0006 will need a rebase, nothing huge though.
>>
>> Done, new revisions attached.
>
> I committed patches 1-7 with some comment changes, a pgindent, and other
> cosmetic trivia.  (The file was pgindent-clean before this work.  Patch 6
> still achieved the more-compact formatting you sought.)

Thanks all for helping in closing this item. We have a fine result here.
-- 
Michael