Skip to content

[BugFix] Fix bRPC stub cache clean timer leak#75973

Merged
satanson merged 7 commits into
StarRocks:mainfrom
kaijianding:fix_brpc_timer_leak
Jul 14, 2026
Merged

[BugFix] Fix bRPC stub cache clean timer leak#75973
satanson merged 7 commits into
StarRocks:mainfrom
kaijianding:fix_brpc_timer_leak

Conversation

@kaijianding

@kaijianding kaijianding commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Why I'm doing:

there is mem leak caused by bRPC stub cache clean timer leak due to timer is unscheduled but not removed from mem.

std::shared_ptr<PInternalService_RecoverableStub> BrpcStubCache::get_stub(const butil::EndPoint& endpoint) {
    ...
    if (_timer->unschedule((*stub_pool)->_cleanup_task.get()) != TIMER_TASK_RUNNING) {
        timespec tm = butil::seconds_from_now(config::brpc_stub_expire_s);
        auto status = _timer->schedule((*stub_pool)->_cleanup_task.get(), tm);
        if (!status.ok()) {
            LOG(WARNING) << "Failed to schedule brpc cleanup task: " << endpoint;
        }
    }
}

every get_stub() will do _timer->unschedule() and _timer->schedule().
unlike what we expect, _timer->unschedule() will not remove internal objects, thus leak happens

What I'm doing:

extend timer deadline if the stub is still in use instead of unschedule()

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
    • This pr needs auto generate documentation
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 4.1
    • 4.0
    • 3.5

@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 218a34af6a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread be/src/common/brpc/brpc_stub_cache.h Outdated
@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch 3 times, most recently from 3d9d169 to f88cedd Compare July 8, 2026 01:07
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f88ceddd5a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread be/src/common/brpc/brpc_stub_cache.h Outdated
@stdpain

stdpain commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

I don't understand the cause of the memory leak. Could you explain it in more detail? This might not be a memory leak.

@kaijianding

Copy link
Copy Markdown
Contributor Author

@stdpain the root cause of this leak is that unscheduled timer internal objects is not removed from brpc itself when timer.unschedule().

std::shared_ptr<PInternalService_RecoverableStub> BrpcStubCache::get_stub(const butil::EndPoint& endpoint) {
    ...
    if (_timer->unschedule((*stub_pool)->_cleanup_task.get()) != TIMER_TASK_RUNNING) {
        timespec tm = butil::seconds_from_now(config::brpc_stub_expire_s);
        auto status = _timer->schedule((*stub_pool)->_cleanup_task.get(), tm);
        if (!status.ok()) {
            LOG(WARNING) << "Failed to schedule brpc cleanup task: " << endpoint;
        }
    }
}

every get_stub() will do _timer->unschedule() and _timer->schedule().
unlike what we expect, _timer->unschedule() will not remove internal objects, thus leak happens

@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch 2 times, most recently from 7648625 to 62d84ff Compare July 8, 2026 05:13
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 62d84ff4a5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread be/src/common/brpc/brpc_stub_cache.cpp
satanson
satanson previously approved these changes Jul 8, 2026
@stdpain

stdpain commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

@stdpain the root cause of this leak is that unscheduled timer internal objects is not removed from brpc itself when timer.unschedule().

std::shared_ptr<PInternalService_RecoverableStub> BrpcStubCache::get_stub(const butil::EndPoint& endpoint) {
    ...
    if (_timer->unschedule((*stub_pool)->_cleanup_task.get()) != TIMER_TASK_RUNNING) {
        timespec tm = butil::seconds_from_now(config::brpc_stub_expire_s);
        auto status = _timer->schedule((*stub_pool)->_cleanup_task.get(), tm);
        if (!status.ok()) {
            LOG(WARNING) << "Failed to schedule brpc cleanup task: " << endpoint;
        }
    }
}

every get_stub() will do _timer->unschedule() and _timer->schedule(). unlike what we expect, _timer->unschedule() will not remove internal objects, thus leak happens

This doesn't actually cause a memory leak. Why would you think there's a leak here? Is it because you observed high memory usage from the schedule in the profile? brpc's rpc timeout is used in this way.

@stdpain stdpain left a comment

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.

I suspect the issue is caused by an unexpected blockage in the timer thread. This isn't the root cause; otherwise, regular RPC calls would also have the same problem.

@kaijianding

Copy link
Copy Markdown
Contributor Author

This doesn't actually cause a memory leak. Why would you think there's a leak here? Is it because you observed high memory usage from the schedule in the profile? brpc's rpc timeout is used in this way.

staff from Mirrorship analyzed the heap dump for me. The heap dump indicates the timer objects leaks.

@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 25d0bc3dbd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread be/src/common/brpc/brpc_stub_cache.cpp Outdated
Signed-off-by: kaijian.ding <kaijian.ding@gmail.com>
@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch from 25d0bc3 to df737f5 Compare July 8, 2026 15:24
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch from d01f668 to 4b89d77 Compare July 13, 2026 14:43
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: d01f6687b4

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch 2 times, most recently from 46726f6 to f7a37db Compare July 13, 2026 16:38
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: f7a37db171

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Replace white-box _timer assertions with behavioral assertions via
public API in the two singleton-reinitialize tests.

Signed-off-by: kaijian.ding <kaijian.ding@gmail.com>
@kaijianding
kaijianding force-pushed the fix_brpc_timer_leak branch from f7a37db to 9f3cd25 Compare July 14, 2026 03:00
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9f3cd25612

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread be/src/common/brpc/brpc_stub_cache.h Outdated
Comment thread be/src/common/brpc/brpc_stub_cache.cpp
@CelerData-Reviewer

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 4b8ba5c04a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

Copy link
Copy Markdown
Contributor

[Java-Extensions Incremental Coverage Report]

pass : 0 / 0 (0%)

@github-actions

Copy link
Copy Markdown
Contributor

[FE Incremental Coverage Report]

pass : 0 / 0 (0%)

@github-actions

Copy link
Copy Markdown
Contributor

[BE Incremental Coverage Report]

pass : 101 / 113 (89.38%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 be/src/common/brpc/brpc_stub_cache.cpp 70 79 88.61% [62, 137, 138, 277, 278, 279, 368, 369, 370]
🔵 be/src/common/brpc/brpc_stub_cache.h 31 34 91.18% [91, 96, 98]

@satanson
satanson merged commit 2bbca67 into StarRocks:main Jul 14, 2026
65 of 66 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

@Mergifyio backport branch-4.1

@github-actions

Copy link
Copy Markdown
Contributor

@Mergifyio backport branch-4.0

@mergify

mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

backport branch-4.1

✅ Backports have been created

Details

Cherry-pick of 2bbca67 has failed:

On branch mergify/bp/branch-4.1/pr-75973
Your branch is up to date with 'origin/branch-4.1'.

You are currently cherry-picking commit 2bbca67281.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   be/src/util/brpc_stub_cache.cpp
	both modified:   be/src/util/brpc_stub_cache.h
	both modified:   be/test/util/brpc_stub_cache_test.cpp

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

@mergify

mergify Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

backport branch-4.0

✅ Backports have been created

Details

Cherry-pick of 2bbca67 has failed:

On branch mergify/bp/branch-4.0/pr-75973
Your branch is up to date with 'origin/branch-4.0'.

You are currently cherry-picking commit 2bbca67281.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   be/src/util/brpc_stub_cache.cpp
	both modified:   be/src/util/brpc_stub_cache.h
	both modified:   be/test/util/brpc_stub_cache_test.cpp

no changes added to commit (use "git add" and/or "git commit -a")

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

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.

5 participants