Skip to content

UCS/DATASTRUCT: Extract red-black tree core from interval tree - #11944

Merged
brminich merged 14 commits into
openucx:masterfrom
tomerg-nvidia:rb_tree
Sep 18, 2026
Merged

brminich merged 14 commits into
openucx:masterfrom
tomerg-nvidia:rb_tree

Conversation

@tomerg-nvidia

@tomerg-nvidia tomerg-nvidia commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

What?

Factor the balancing mechanics of ucs_interval_tree_t into a reusable intrusive red-black tree, ucs_rbtree_t, and port the interval tree onto it. The caller performs its own descent and hands the attachment point to ucs_rbtree_insert_at, so the ordering key stays in the embedding struct.

The only behavior that changes is node identity on removal: the successor is relinked rather than having its interval copied into the removed node, so the node object a caller names is the one removed. Interval tree nodes are pool allocated and never referenced from outside, so this is not observable here; it is what lets a later user hold node pointers across unrelated removals.

Why?

For later reuse of the rbtree in an interval_map structure

Factor the balancing mechanics of ucs_interval_tree_t into a reusable
intrusive red-black tree, ucs_rbtree_t, and port the interval tree onto
it. The caller performs its own descent and hands the attachment point to
ucs_rbtree_insert_at, so the ordering key stays in the embedding struct.
An optional augmentation callback recomputes data a node derives from its
subtrees, which the interval tree does not use and leaves NULL.

The only behavior that changes is node identity on removal: the successor
is relinked rather than having its interval copied into the removed node,
so the node object a caller names is the one removed. Interval tree nodes
are pool allocated and never referenced from outside, so this is not
observable here; it is what lets a later user hold node pointers across
unrelated removals.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.c Resolved
Comment thread test/gtest/Makefile.am Resolved
Comment thread src/ucs/datastruct/interval_tree.c Resolved
Comment thread src/ucs/Makefile.am Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gaps: the relink-successor removal path (ucs_rbtree_remove two-children case) is exercised by total_size_many_disjoint_then_merge / merge_multiple_overlaps but without any RB structural validation; only leftmost removals are validated (pop_drains_all). Also, ucs_rbtree_propagate() / augment is never executed by any test.

@svc-ucx

svc-ucx commented Sep 14, 2026

Copy link
Copy Markdown

🤖 CI Triage Agent — UCX PR (go gpu) · commit 440231f1

TL;DR: The Go binding test TestUcpWorkerEfd deadlocked and hit the 10-minute go test timeout because syscall.EpollWait returned EINTR (a Go runtime preemption signal, SIGURG), and the test's error path calls t.Fatalf from inside a goroutine — which kills that goroutine without ever sending on eventsChan, leaving the main test goroutine blocked forever. Fix: retry EpollWait on EINTR and never call t.Fatalf from a non-test goroutine.

Full analysis

Summary: Azure job "UCX PR (go gpu)" step "Run go tests" failed: panic: test timed out after 10m0s in github.com/openucx/ucx/bindings/go/tests, hung in TestUcpWorkerEfd.

Root cause: Log evidence:

  • 08:15:42.838 worker_test.go:61: Failed to call epoll_wait interrupted system call
  • then a 9m55s gap with zero output until 08:25:37 panic: test timed out after 10m0s
  • stack trace: goroutine 36 [chan receive, 9 minutes] ... TestUcpWorkerEfd ... worker_test.go:74

So this is a hang, not a slow test. syscall.EpollWait(epfdLocal, events[:], -1) at worker_test.go:59 returned EINTR — expected on Linux when the Go runtime delivers an async-preemption signal (SIGURG) to the thread blocked in the syscall; the test does not retry. The error branch then calls t.Fatalf at line 61, but t.Fatalf from a non-test goroutine only records the failure and calls runtime.Goexit() on that goroutine, so line 64 eventsChan <- nevents is never reached. The main test goroutine is parked forever on nevents := <-eventsChan (line 74) with no timeout guard (unlike the started channel, which has a 500 ms select/time.After). The result is a permanent deadlock that only the global 10-minute test alarm breaks, and the panic exit code 2 fails make -C build/bindings/go test.

This is a pre-existing flaky test, not caused by this PR: bindings/go/tests/worker_test.go was last modified in 2024 (bd1b1143, Artemy Kovalyov), and the PR under test (#11944, branch rb_tree, commit 440231f) touches the UCS red-black tree, which is unrelated to the Go wakeup/efd path. All preceding Go tests (TestUcpContext, TestUcpEpTag, TestUcpEpAm, TestUcpListener, TestUcpMmap) passed.

Implicated commit: unknown for the failure trigger (flaky pre-existing test); the test code originates from 2bbf7cfe (Peter Rudenko, "BINDINGS/GO: Ucp endpoint functionality") and was last touched by bd1b1143 (Artemy Kovalyov). PR commit 440231f (rb_tree) is not implicated.

File: bindings/go/tests/worker_test.go:59-64 (EINTR not retried; t.Fatalf in goroutine) and bindings/go/tests/worker_test.go:74 (unbounded channel receive)

Suggested fix:

  1. Retry the syscall on EINTR instead of failing:
var nevents int
for {
    nevents, err = syscall.EpollWait(epfdLocal, events[:], -1)
    if err != syscall.EINTR {
        break
    }
}
if err != nil {
    t.Errorf("Failed to call epoll_wait %v", err)
    close(eventsChan)   // never leave the parent blocked
    return
}
eventsChan <- nevents
  1. Replace all t.Fatalf calls inside the goroutine (lines 39, 44, 51, 61) with t.Errorf + return, ensuring eventsChan is always closed/written on every exit path — t.Fatalf is documented as unsafe outside the test goroutine.
  2. Guard the receive at line 74 with a select + time.After(...) (same pattern as the started channel) so a future regression reports a real test failure in seconds instead of burning the 10-minute suite timeout and giving a useless "test timed out" panic.
  3. Consider re-running the PR job; since the failure is unrelated to rb_tree, it should not block UCS/DATASTRUCT: Extract red-black tree core from interval tree #11944.

Related: PR #11944 (build under test); no existing issue found tracking this TestUcpWorkerEfd EINTR flake — worth opening one.

Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.c Resolved
Comment thread src/ucs/datastruct/rbtree.c Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

src/ucs/datastruct/interval_tree.c

ucs_interval_tree_cleanup() frees every node but never clears rb.root, so afterwards num_nodes == 0 (tree reports empty) while rb.root points at released mpool elements. A later ucs_interval_tree_insert() takes the slow path → remove_overlapping() → find_overlap(tree->rb.root) and walks freed memory. Not reachable today (all callers free/re-init right after), but now it is a one-liner.

pls also reset the tree root here; otherwise rb.root keeps pointing to freed nodes while num_nodes is 0, so a later insert into the same tree walks released memory.

    ucs_interval_tree_cleanup_recursive(tree, tree->rb.root);
    ucs_rbtree_init(&tree->rb);

The comment explaining why the augmentation walk starts at 'child_parent'
was left behind when the hook was removed; there is no derived data to
refresh here, and 'child_parent' is only the fixup start point.
Both changes alter behaviour only on a malformed tree, where the extracted
code carried over defensive tests from ucs_interval_tree_delete_fixup():

- The removal fixup asserts that a double-black node has a sibling rather
  than testing for it. Removing a black node always leaves the opposite
  subtree non-empty, and a red sibling has two non-NIL black children, so
  the post-rotation sibling exists too. The tests were unreachable, and a
  tree that did violate them silently skipped the rebalance.

- Cleanup resets the tree root, which previously kept pointing at nodes it
  had just freed while the node count read zero. Every caller either frees
  the containing object or re-initializes, so this was latent.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.c Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread test/gtest/ucs/test_interval_tree.cc Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gap: ucs_rbtree_remove() is only exercised through ucs_interval_tree_pop_any() (leftmost) and remove_overlapping() in the interval-tree suite; the deep-successor relink path is covered only by the new test_rbtree cases, not by any interval-tree scenario that also validates total_size/num_nodes afterwards.

Residual coverage gap: no test inserts equal keys through ucs_rbtree_insert_at() beyond the EXPECT_GE in check_ordering, so the "duplicates go right" tie-break is not directly asserted.

Comment thread test/gtest/ucs/test_rbtree.cc Resolved
Comment thread test/gtest/ucs/test_rbtree.cc Resolved
Comment thread test/gtest/ucs/test_rbtree.cc Resolved
Comment thread test/gtest/ucs/rbtree_check.h Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/interval_tree.c Resolved
Comment thread test/gtest/ucs/test_rbtree.cc Resolved
Deduplicate the ucs_derived_of cast: ucs_interval_tree_root() now returns
ucs_interval_tree_node(tree->rb.root) instead of repeating it.
It is called once per tree lifetime, so there is nothing to gain from
inlining it, and the other tree operations already live in rbtree.c.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.c Resolved
Comment thread src/ucs/datastruct/interval_tree.h Resolved
Comment thread test/gtest/ucs/test_interval_tree.cc Resolved
The remove fixup reaches those branches only when the sibling's other child
is red, so the NULL checks were unreachable. Also document that
ucs_interval_tree_root() returns NULL for an empty tree.
It also verifies the disjoint-with-a-gap property that coalescing provides,
which is not a red-black invariant.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.c Resolved
Comment thread src/ucs/datastruct/rbtree.c Resolved
The caller descends to the insertion point itself and passes (parent, link)
as a pair. An inconsistent pair links the node under one node while setting
its parent to another, which the existing *link == NULL check cannot catch.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread test/gtest/ucs/test_interval_tree.cc Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread test/gtest/ucs/test_interval_tree.cc Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gaps (already reported in earlier rounds): the deep-successor relink path in ucs_rbtree_remove() is covered only by test_rbtree, not by any interval-tree scenario that also validates total_size/num_nodes afterwards. New note: rbtree_check::validate() intentionally checks only core structure, so the interval tree's ordering/coalescing is validated exclusively through test_interval_tree::check_invariants(), which is called in 4 tests.

@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

Comment thread src/ucs/datastruct/rbtree.h Resolved
Comment thread test/gtest/ucs/test_rbtree.cc Resolved
@svc-nvidia-pr-review

Copy link
Copy Markdown

Residual coverage gap (already noted in earlier rounds, not re-posting): the deep-successor relink path of ucs_rbtree_remove() is covered only by test_rbtree, not by an interval-tree scenario that also re-checks total_size/num_nodes afterwards.

The file-level description sat above the node typedef, so doxygen attached
it to ucs_rbtree_node_t; move it to ucs_rbtree_t and describe the node.
Stop random_stress at the first failure instead of repeating it for every
remaining iteration.
@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Starting review — findings will be posted here when done.

@svc-nvidia-pr-review

Copy link
Copy Markdown

🤖 Review complete — no issues found.

@svc-ucx

svc-ucx commented Sep 17, 2026

Copy link
Copy Markdown

🤖 CI Triage Agent — UCX PR (AddressSanitizer BlueField on worker 2) · commit 99af2f43

[incomplete: stop_reason=max_tokens]

@brminich
brminich merged commit 0385f63 into openucx:master Sep 18, 2026
162 checks passed
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.

6 participants