Skip to content

Avoid concurrent calls to bindtextdomain(). - #2201

Open
tuhaihe wants to merge 1 commit into
apache:mainfrom
tuhaihe:fix-libpq-binddomain
Open

tuhaihe wants to merge 1 commit into
apache:mainfrom
tuhaihe:fix-libpq-binddomain

Conversation

@tuhaihe

@tuhaihe tuhaihe commented Oct 27, 2026

Copy link
Copy Markdown
Member

We previously supposed that it was okay for different threads to call bindtextdomain() concurrently (cf. commit 1f655fd). It now emerges that there's at least one gettext implementation in which that triggers an abort() crash, so let's stop doing that. Add mutexes guarding libpq's and ecpglib's calls, which are the only ones that need worry about multithreaded callers.

Note: in libpq, we could perhaps have piggybacked on default_threadlock() to avoid defining a new mutex variable. I judge that not terribly safe though, since libpq_gettext could be called from code that is holding the default mutex. If that were the first such call in the process, it'd fail. An extra mutex is cheap insurance against unforeseen interactions.

Per bug #18312 from Christian Maurer. Back-patch to all supported versions.

Discussion: https://postgr.es/m/18312-bbbabc8113592b78@postgresql.org
Discussion: https://postgr.es/m/264860.1707163416@sss.pgh.pa.us

Cloudberry: the merge of REL_16_9 (0f4cf8d) brought in only this commit's removal of ldir's declaration from the outer block of libpq_binddomain(), not the mutex or the declaration's new place. The function was left without the lock and using ldir undeclared, which breaks any build with ENABLE_NLS. The ecpglib half came through intact, so only fe-misc.c changes here.

Assisted-by: Claude Code
(cherry picked from commit 52afe56)

Fixes #ISSUE_Number

What does this PR do?

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Breaking Changes

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:

User-facing changes:

Dependencies:

Checklist

Additional Context

CI Skip Instructions


We previously supposed that it was okay for different threads to
call bindtextdomain() concurrently (cf. commit 1f655fd).
It now emerges that there's at least one gettext implementation
in which that triggers an abort() crash, so let's stop doing that.
Add mutexes guarding libpq's and ecpglib's calls, which are the
only ones that need worry about multithreaded callers.

Note: in libpq, we could perhaps have piggybacked on
default_threadlock() to avoid defining a new mutex variable.
I judge that not terribly safe though, since libpq_gettext could
be called from code that is holding the default mutex.  If that
were the first such call in the process, it'd fail.  An extra
mutex is cheap insurance against unforeseen interactions.

Per bug #18312 from Christian Maurer.  Back-patch to all
supported versions.

Discussion: https://postgr.es/m/18312-bbbabc8113592b78@postgresql.org
Discussion: https://postgr.es/m/264860.1707163416@sss.pgh.pa.us

Cloudberry: the merge of REL_16_9 (0f4cf8d) brought in only this
commit's removal of ldir's declaration from the outer block of
libpq_binddomain(), not the mutex or the declaration's new place. The
function was left without the lock and using ldir undeclared, which
breaks any build with ENABLE_NLS. The ecpglib half came through intact,
so only fe-misc.c changes here.

Assisted-by: Claude Code
(cherry picked from commit 52afe56)
@tuhaihe tuhaihe added this to the 3.0 milestone Oct 27, 2026
@tuhaihe

tuhaihe commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

We accidentally found this issue at #1940.


Some context for reviewers

this changes nothing in today's builds. libpq_binddomain() is compiled only with ENABLE_NLS, and Cloudberry's configure has no --enable-nls, so the function is never built and nothing at compile time or runtime is affected.

The point is to bring the function back in line with upstream. The REL_16_9 merge (0f4cf8d) kept only part of 52afe56, leaving the function without its mutex and using ldir undeclared. Matching upstream again means future PostgreSQL minor-version merges will not conflict here, and if NLS is ever turned back on, the code builds and carries upstream's fix for bug #18312.

@tuhaihe tuhaihe modified the milestones: 3.0, 3.x Meson Oct 27, 2026

@leborchuk leborchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, bugfix from upstream

* might as well do it the same way everywhere.
*/
static volatile bool already_bound = false;
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I provided suggested changes to guard the mutex declaration and both pthread_mutex_lock() / pthread_mutex_unlock() calls with #ifdef ENABLE_THREAD_SAFETY. libpq-int.h includes the pthread headers only when thread safety is enabled, so this change fails to compile with --enable-nls --disable-thread-safety.

* might as well do it the same way everywhere.
*/
static volatile bool already_bound = false;
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
+#ifdef ENABLE_THREAD_SAFETY
static pthread_mutex_t binddomain_mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif

ldir = LOCALEDIR;
bindtextdomain(PG_TEXTDOMAIN("libpq"), ldir);
already_bound = true;
(void) pthread_mutex_lock(&binddomain_mutex);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(void) pthread_mutex_lock(&binddomain_mutex);
+#ifdef ENABLE_THREAD_SAFETY
(void) pthread_mutex_lock(&binddomain_mutex);
+#endif

already_bound = true;
}

(void) pthread_mutex_unlock(&binddomain_mutex);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(void) pthread_mutex_unlock(&binddomain_mutex);
+#ifdef ENABLE_THREAD_SAFETY
(void) pthread_mutex_unlock(&binddomain_mutex);
+#endif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants