Skip to content

Fix memory leak in scan cleanup when scan context is unavailable#302

Open
hobostay wants to merge 1 commit intotimescale:mainfrom
hobostay:fix/memory-leak-in-scan-cleanup
Open

Fix memory leak in scan cleanup when scan context is unavailable#302
hobostay wants to merge 1 commit intotimescale:mainfrom
hobostay:fix/memory-leak-in-scan-cleanup

Conversation

@hobostay
Copy link
Copy Markdown

@hobostay hobostay commented Apr 1, 2026

Summary

Fixes a memory leak in tp_rescan_cleanup_results() that occurs when scan_context is unavailable.

Problem

In src/am/scan.c, the tp_rescan_cleanup_results() function has a code path that handles cleanup when scan_context is not available. The original code logged a warning about a memory leak but then only set the pointer to NULL without actually freeing the memory:

else
{
    elog(WARNING,
         "No scan context available for cleanup - memory leak!");
    so->result_ctids = NULL;  // Memory leaked!
}

This affected both result_ctids and result_scores allocations.

Solution

Add pfree() calls to properly release the allocated memory:

else
{
    elog(WARNING,
         "No scan context available for cleanup - freeing directly");
    pfree(so->result_ctids);
    so->result_ctids = NULL;
}

Impact

This prevents memory leaks in edge cases where scan context cleanup occurs in unusual scenarios, such as:

  • During error handling paths
  • Certain rescan patterns
  • Edge cases in index scan lifecycle management

The fix is minimal and safe - it only adds the missing pfree() calls that should have been there.

Test plan

  • Code compiles without warnings
  • No functional changes to existing behavior
  • Only adds missing cleanup in error paths

Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

When tp_rescan_cleanup_results() is called without a scan context,
the function was logging a warning about memory leak but then only
setting the pointer to NULL without actually freeing the memory.

This fix ensures that pfree() is called to properly release the
allocated memory even when scan_context is not available.

- Add pfree() call for result_ctids before setting to NULL
- Add pfree() call for result_scores before setting to NULL
- Update warning message to reflect the actual behavior

This prevents memory leaks in edge cases where scan context cleanup
occurs in unusual scenarios (e.g., during error handling or certain
rescan patterns).
Copy link
Copy Markdown

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

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.

2 participants