Skip to content

Fix ART iterator re-seeding root after remove - #706

Open
hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/art-iterator-reseed
Open

hardikkaurani wants to merge 1 commit into
pgexporter:mainfrom
hardikkaurani:fix/art-iterator-reseed

Conversation

@hardikkaurani

@hardikkaurani hardikkaurani commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Cross-port of pgmoneta/pgmoneta#1705.

pgexporter_art_iterator_next() previously seeded the BFS queue from tree->root whenever iter->count == 0. Because pgexporter_art_iterator_remove() decrements iter->count, removing the first yielded key resets iter->count to 0 and causes pgexporter_art_iterator_next() to push tree->root onto iter->que again mid-traversal.

  • Track traversal initialization explicitly with bool started on struct art_iterator so tree->root is only queued once.
  • Add test_art_iterator_remove_collapse in test/testcases/test_art.c to cover removing entries during iteration when the root collapses.

Verification

  • Formatted all modified files with clang-format.
  • Full build and test suite run in GitHub Actions CI (format-check, contrib-check, build-macos, build-linux Release builds, and build-rpm pass; the two Linux Debug failures are pre-existing on main).

Copilot AI balanced review requested due to automatic review settings November 26, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Hi @jesperpedersen,

Cross-ported the art_iterator fix from pgmoneta/pgmoneta#1705:

  • Added bool started to struct art_iterator so pgexporter_art_iterator_next() only seeds tree->root once instead of re-queueing it when pgexporter_art_iterator_remove() drops iter->count back to 0.
  • Added the test_art_iterator_remove_collapse regression test in test/testcases/test_art.c.
  • Formatted all modified files with clang-format.

Please let me know if any adjustments are needed.

@Pranav-error Pranav-error left a comment

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.

The test passes with and without the art.c change, so it doesn't catch the bug: iterator_next() stops at count == size, and the re-pushed root is at the end of the queue, so it's never reached. Same as pgagroal/pgagroal#1117 and the test in pgmoneta #1705. The change is fine as hardening, but either add a test that reaches the duplicate visit / use-after-free, or drop that part of the description.

The two failing Debug jobs fail on main too.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Good catch, @Pranav-error — you're right that pgexporter_art_iterator_next() stops as soon as iter->count == iter->tree->size, so the re-pushed tree->root at the back of iter->que isn't reached before iteration ends.

I've updated the PR description to drop the duplicate-visit / use-after-free claim and describe bool started accurately as hardening against re-queueing tree->root when iter->count drops to 0. The branch is already rebased onto main as a single commit (9dcd42a).

@Pranav-error

Copy link
Copy Markdown
Collaborator

Adding the same check as pgagroal/pgagroal#1117 makes the test catch it here too: with MCTF_ASSERT(pgexporter_deque_empty(iter->que), ...) after the loop (and #include <deque.h>), it fails at test_art.c:335 without the art.c change and passes with it.

@hardikkaurani
hardikkaurani force-pushed the fix/art-iterator-reseed branch from 9dcd42a to 45ba588 Compare November 30, 2026 11:35
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Updated test/testcases/test_art.c in 45ba588 to include <deque.h> and assert MCTF_ASSERT(pgexporter_deque_empty(iter->que), cleanup, "iterator queue should be empty"); right after the traversal loop, so the test directly catches tree->root being re-queued without the art.c fix.

@Pranav-error

Copy link
Copy Markdown
Collaborator

Checked at 45ba588: the art module passes 5/5, and with art.c from main test_art_iterator_remove_collapse fails at test_art.c:333, so the new check catches it. Looks good to me.

@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Thanks for verifying!

@Pranav-error

Copy link
Copy Markdown
Collaborator

#692 is merged now. It fixes the Debug crash in test_connection_bind_unix_socket_permissions (#691), which is what fails both build-linux Debug jobs here. Please rebase on main.

@hardikkaurani
hardikkaurani force-pushed the fix/art-iterator-reseed branch from 45ba588 to 7de8bc0 Compare December 2, 2026 21:29
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

@Pranav-error Rebased onto main (7de8bc0) to pick up #692.

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.

3 participants