Skip to content

Use pre_len in package cleanup debug logging#1215

Draft
c2main wants to merge 1 commit intoIvorySQL:masterfrom
Data-Bene:upstream/readd-prelen-post-delete-assert
Draft

Use pre_len in package cleanup debug logging#1215
c2main wants to merge 1 commit intoIvorySQL:masterfrom
Data-Bene:upstream/readd-prelen-post-delete-assert

Conversation

@c2main
Copy link
Copy Markdown
Contributor

@c2main c2main commented Mar 10, 2026

Keep pre-removal list length available where it is actually useful by including it in the DEBUG1 cleanup message, while preserving the length invariant Assert() around list_delete_ptr().

Summary by CodeRabbit

  • Chores
    • Enhanced diagnostic logging for package resource management with improved error detection through validation checks.

Keep pre-removal list length available where it is actually useful by
including it in the DEBUG1 cleanup message, while preserving the length
invariant Assert() around list_delete_ptr().
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 10, 2026

📝 Walkthrough

Walkthrough

The change enhances diagnostic logging in the plisql_remove_function_references function by recording the pre-removal function list length and adding a verification assertion. The log message is updated to include the number of function references being removed, improving observability of package cleanup operations.

Changes

Cohort / File(s) Summary
Package cleanup diagnostics
src/pl/plisql/src/pl_package.c
Added pre-removal length tracking and assertion to verify function removal count. Enhanced log message to include function reference count for improved debugging visibility during package cleanup.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Suggested reviewers

  • jiaoshuntian

Poem

🐰 Hop hop, a logger's delight!
Pre-lengths tracked, assertions held tight,
Function counts now on display,
Debugging's clearer every day!
Better logs, the rabbit's way! 🌿

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Use pre_len in package cleanup debug logging' directly reflects the main change: incorporating pre-removal list length into the cleanup debug message.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/pl/plisql/src/pl_package.c`:
- Around line 2733-2739: The code currently assumes list_delete_ptr removed one
element and unconditionally decrements psource->source.use_count, which breaks
in non-Assert builds if func wasn't present; modify the logic around
psource->source.funclist and use_count so you compute post_len =
list_length(psource->source.funclist) after calling list_delete_ptr (or check
whether pre_len > post_len), only decrement psource->source.use_count when
pre_len == post_len + 1 (and keep the Assert for the debug case if desired), and
do not change use_count when no element was removed to avoid corrupting the
refcount and prematurely calling plisql_free_package_function().

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fb452cee-6464-4045-b8f3-6492fd41f7f0

📥 Commits

Reviewing files that changed from the base of the PR and between 0da228b and 0e4694d.

📒 Files selected for processing (1)
  • src/pl/plisql/src/pl_package.c

Comment on lines +2733 to 2739
int pre_len = list_length(psource->source.funclist);

psource->source.funclist = list_delete_ptr(psource->source.funclist, (void *) func);

Assert(pre_len == list_length(psource->source.funclist) + 1);

psource->source.use_count--;
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.

⚠️ Potential issue | 🟠 Major

Don’t rely on Assert() to protect the refcount.

On Line 2737 the invariant disappears in non-assert builds, but Line 2739 still decrements use_count. If func is not actually present in funclist, production builds will corrupt the refcount and can prematurely reach plisql_free_package_function().

Suggested hardening
 	int pre_len = list_length(psource->source.funclist);

 	psource->source.funclist = list_delete_ptr(psource->source.funclist, (void *) func);

-	Assert(pre_len == list_length(psource->source.funclist) + 1);
+	if (pre_len == list_length(psource->source.funclist))
+		elog(ERROR, "function reference for package %u was not registered", item->pkey);
+	Assert(pre_len == list_length(psource->source.funclist) + 1);

 	psource->source.use_count--;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
int pre_len = list_length(psource->source.funclist);
psource->source.funclist = list_delete_ptr(psource->source.funclist, (void *) func);
Assert(pre_len == list_length(psource->source.funclist) + 1);
psource->source.use_count--;
int pre_len = list_length(psource->source.funclist);
psource->source.funclist = list_delete_ptr(psource->source.funclist, (void *) func);
if (pre_len == list_length(psource->source.funclist))
elog(ERROR, "function reference for package %u was not registered", item->pkey);
Assert(pre_len == list_length(psource->source.funclist) + 1);
psource->source.use_count--;
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/pl/plisql/src/pl_package.c` around lines 2733 - 2739, The code currently
assumes list_delete_ptr removed one element and unconditionally decrements
psource->source.use_count, which breaks in non-Assert builds if func wasn't
present; modify the logic around psource->source.funclist and use_count so you
compute post_len = list_length(psource->source.funclist) after calling
list_delete_ptr (or check whether pre_len > post_len), only decrement
psource->source.use_count when pre_len == post_len + 1 (and keep the Assert for
the debug case if desired), and do not change use_count when no element was
removed to avoid corrupting the refcount and prematurely calling
plisql_free_package_function().

@c2main c2main marked this pull request as draft March 10, 2026 20:04
@OreoYang
Copy link
Copy Markdown
Collaborator

@NotHimmel follow up this PR

@bigplaice
Copy link
Copy Markdown
Collaborator

Hi, Cedric, the code change looks good to me. Are you going to change the state from Draft to Open? @c2main

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