Re: BUG #16685: The ecpg thread/descriptor test fails sometimes on Windows

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: exclusion(at)gmail(dot)com
Cc: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: BUG #16685: The ecpg thread/descriptor test fails sometimes on Windows
Date: 2020-10-24 16:54:23
Message-ID: 380144.1603558463@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

I wrote:
> I'll bet that the correct fix is

> static void
> set_descriptors(struct descriptor *value)
> {
> + pthread_once(&descriptor_once, descriptor_key_init);
> pthread_setspecific(descriptor_key, value);
> }

No, looking closer, there are only two calls of set_descriptors()
and each of them is preceded by a call to get_descriptors().
So it should not be possible to get here without descriptor_key
having been initialized.

I'm forced to the conclusion that there's something wrong with
ECPG's emulation of pthread_once ...

... and now that I look at it, it seems just as obvious what
is wrong there:

void
win32_pthread_once(volatile pthread_once_t *once, void (*fn) (void))
{
if (!*once)
{
pthread_mutex_lock(&win32_pthread_once_lock);
if (!*once)
{
*once = true;
fn();
}
pthread_mutex_unlock(&win32_pthread_once_lock);
}
}

We should not set *once until AFTER we execute fn().
Otherwise, other threads passing through pthread_once()
will mistakenly fall through, expecting the initialization
to be done already.

(So in this view, adding a sleep just before fn() would
make the failure more reproducible.)

regards, tom lane

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2020-10-24 17:39:05 Re: BUG #16678: The ecpg connect/test5 test sometimes fails on Windows
Previous Message Andrey Borodin 2020-10-24 16:40:35 Re: BUG #16329: Valgrind detects an invalid read when building a gist index with buffering