Skip to content

Commit 0cc28fe

Browse files
committed
Avoid deadlock with PERL_MEM_LOG
This fixes GH #18341 The Perl wrapper for getenv() was changed in 5.32 to allocate memory to squirrel safely away the result of the wrapped getenv() call. It does this while in a critical section so as to make sure another thread can't interrupt it and destroy it. Unfortunately, when Perl is compiled for debugging memory problems and has PERL_MEM_LOG enabled, that allocation causes a recursive call to getenv() for the purpose of checking an environment variable to see how to log that allocation. And hence it deadlocks trying to enter the critical section. There are various solutions. One is to use or emulate a general semaphore instead of a binary one. This is effectively what PL_lc_numeric_mutex_depth does for another mutex, and the code for that could be used as a template. But given that this is an extreme edge case which requires Perl to be specially compiled to enable this feature which is used only for debugging, a much simpler, if less safe if it were to ever be used in production, solution should suffice. Tony Cook suggested just avoiding the wrapper for this particular purpose.
1 parent 5640a37 commit 0cc28fe

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

util.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5008,7 +5008,11 @@ S_mem_log_common(enum mem_log_type mlt, const UV n,
50085008

50095009
PERL_ARGS_ASSERT_MEM_LOG_COMMON;
50105010

5011-
pmlenv = PerlEnv_getenv("PERL_MEM_LOG");
5011+
/* Use plain getenv() to avoid potential deadlock with PerlEnv_getenv().
5012+
* This means that 'pmlenv' is not protected from other threads overwriting
5013+
* it on platforms where getenv() returns an internal static pointer. See
5014+
* GH #18341 */
5015+
pmlenv = getenv("PERL_MEM_LOG");
50125016
if (!pmlenv)
50135017
return;
50145018
if (mlt < MLT_NEW_SV ? strchr(pmlenv,'m') : strchr(pmlenv,'s'))

0 commit comments

Comments
 (0)