Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bef2d62

Browse files
committed
Fix potential memory leakage from HandleParallelMessages().
HandleParallelMessages leaked memory into the caller's context. Since it's called from ProcessInterrupts, there is basically zero certainty as to what CurrentMemoryContext is, which means we could be leaking into long-lived contexts. Over the processing of many worker messages that would grow to be a problem. Things could be even worse than just a leak, if we happened to service the interrupt while ErrorContext is current: elog.c thinks it can reset that on its own whim, possibly yanking storage out from under HandleParallelMessages. Give HandleParallelMessages its own dedicated context instead, which we can reset during each call to ensure there's no accumulation of wasted memory. Discussion: <16610.1472222135@sss.pgh.pa.us>
1 parent 42ebdc8 commit bef2d62

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/backend/access/transam/parallel.c

+25
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,9 @@ void
702702
HandleParallelMessages(void)
703703
{
704704
dlist_iter iter;
705+
MemoryContext oldcontext;
706+
707+
static MemoryContext hpm_context = NULL;
705708

706709
/*
707710
* This is invoked from ProcessInterrupts(), and since some of the
@@ -712,6 +715,23 @@ HandleParallelMessages(void)
712715
*/
713716
HOLD_INTERRUPTS();
714717

718+
/*
719+
* Moreover, CurrentMemoryContext might be pointing almost anywhere. We
720+
* don't want to risk leaking data into long-lived contexts, so let's do
721+
* our work here in a private context that we can reset on each use.
722+
*/
723+
if (hpm_context == NULL) /* first time through? */
724+
hpm_context = AllocSetContextCreate(TopMemoryContext,
725+
"HandleParallelMessages context",
726+
ALLOCSET_DEFAULT_MINSIZE,
727+
ALLOCSET_DEFAULT_INITSIZE,
728+
ALLOCSET_DEFAULT_MAXSIZE);
729+
else
730+
MemoryContextReset(hpm_context);
731+
732+
oldcontext = MemoryContextSwitchTo(hpm_context);
733+
734+
/* OK to process messages. Reset the flag saying there are more to do. */
715735
ParallelMessagePending = false;
716736

717737
dlist_foreach(iter, &pcxt_list)
@@ -758,6 +778,11 @@ HandleParallelMessages(void)
758778
}
759779
}
760780

781+
MemoryContextSwitchTo(oldcontext);
782+
783+
/* Might as well clear the context on our way out */
784+
MemoryContextReset(hpm_context);
785+
761786
RESUME_INTERRUPTS();
762787
}
763788

0 commit comments

Comments
 (0)