Skip to content

Guard the consumer subscription read against ERR__STATE - #2804

Open
delthas wants to merge 1 commit into
development/9.5from
bugfix/BB-845/guard-subscription-state
Open

Guard the consumer subscription read against ERR__STATE#2804
delthas wants to merge 1 commit into
development/9.5from
bugfix/BB-845/guard-subscription-state

Conversation

@delthas

@delthas delthas commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

isPaused() read this._consumer.subscription() unguarded, and onEntryCommittable() evaluates isPaused() inside its if condition — outside the try/catch that protects the call it guards. node-rdkafka throws Local: Erroneous state from that read while the consumer is disconnecting, so the check meant to prevent ERR__STATE can raise it itself.

Both callers invoke onEntryCommittable() from a producer delivery callback with no try/catch, so the throw is uncaught and the process exits. It also skips finishProcessingTask(), leaking the task slot so _processingQueue.idle() never becomes true. resume(), close() and getServiceStatus() read the subscription the same way; in close() the throw skips next(), so close(cb) never fires and the pod is SIGKILLed after the grace period.

Shutdown path only, no data loss — the in-flight entry is re-delivered. The unguarded read dates to 2018; BB-758 added the isConnected() check and the try/catch but stopped one line short of it.

Fix: the four readers go through a _getSubscription() helper that reports no subscription when the read fails, which is the conservative answer for each caller. subscribe() is deliberately not guarded — swallowing there would leave a consumer that never subscribes while isReady() keeps passing, trading a loud crash for a silent stall.

This is not a CI-flakiness fix. That is caused by the librdkafka 2.3.0 pin and is handled separately under BB-849.

Tests: six unit tests, three of which fail with Local: Erroneous state against the unfixed code.

Issue: BB-845

@bert-e

bert-e commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Hello delthas,

My role is to assist you with the merge of this
pull request. Please type @bert-e help to get information
on this process, or consult the user documentation.

Available options
name description privileged authored
/after_pull_request Wait for the given pull request id to be merged before continuing with the current one.
/bypass_author_approval Bypass the pull request author's approval
/bypass_build_status Bypass the build and test status
/bypass_commit_size Bypass the check on the size of the changeset TBA
/bypass_incompatible_branch Bypass the check on the source branch prefix
/bypass_jira_check Bypass the Jira issue check
/bypass_peer_approval Bypass the pull request peers' approval
/bypass_leader_approval Bypass the pull request leaders' approval
/approve Instruct Bert-E that the author has approved the pull request. ✍️
/create_pull_requests Allow the creation of integration pull requests.
/create_integration_branches Allow the creation of integration branches.
/no_octopus Prevent Wall-E from doing any octopus merge and use multiple consecutive merge instead
/unanimity Change review acceptance criteria from one reviewer at least to all reviewers
/wait Instruct Bert-E not to run until further notice.
Available commands
name description privileged
/help Print Bert-E's manual in the pull request.
/status Print Bert-E's current status in the pull request.
/clear Remove all comments from Bert-E from the history TBA
/retry Re-start a fresh build TBA
/build Re-start a fresh build TBA
/force_reset Delete integration branches & pull requests, and restart merge process from the beginning.
/reset Try to remove integration branches unless there are commits on them which do not appear on the source branch.

Status report is not available.

@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.47%. Comparing base (52b9510) to head (a33d1f1).

Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
lib/BackbeatConsumer.js 93.50% <100.00%> (-1.40%) ⬇️

... and 2 files with indirect coverage changes

Components Coverage Δ
Bucket Notification 80.27% <ø> (ø)
Core Library 81.23% <100.00%> (-0.71%) ⬇️
Ingestion 70.09% <ø> (ø)
Lifecycle 80.46% <ø> (ø)
Oplog Populator 85.83% <ø> (ø)
Replication 62.01% <ø> (ø)
Bucket Scanner 85.76% <ø> (ø)
@@                 Coverage Diff                 @@
##           development/9.5    #2804      +/-   ##
===================================================
- Coverage            75.76%   75.47%   -0.29%     
===================================================
  Files                  200      200              
  Lines                13922    13926       +4     
===================================================
- Hits                 10548    10511      -37     
- Misses                3364     3405      +41     
  Partials                10       10              
Flag Coverage Δ
api:retry 9.10% <0.00%> (-0.01%) ⬇️
api:routes 8.87% <0.00%> (-0.01%) ⬇️
bucket-scanner 85.76% <ø> (ø)
ft_test:queuepopulator 9.16% <40.00%> (-1.83%) ⬇️
ingestion 12.26% <0.00%> (-0.01%) ⬇️
lifecycle 19.31% <60.00%> (+<0.01%) ⬆️
notification 1.02% <0.00%> (-0.01%) ⬇️
oplogPopulator 0.13% <0.00%> (-0.01%) ⬇️
replication 18.81% <80.00%> (-0.04%) ⬇️
unit 54.93% <80.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@scality scality deleted a comment from bert-e Aug 18, 2026
@bert-e

bert-e commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Waiting for approval

The following approvals are needed before I can proceed with the merge:

  • the author

  • 2 peers

@delthas
delthas force-pushed the bugfix/BB-845/guard-subscription-state branch from 3950fba to f6fee9c Compare August 18, 2026 13:08
Comment thread lib/BackbeatConsumer.js
@delthas
delthas force-pushed the bugfix/BB-845/guard-subscription-state branch 2 times, most recently from 3d0b402 to 4413584 Compare August 18, 2026 14:15
@delthas
delthas marked this pull request as draft August 18, 2026 14:17
Comment thread lib/BackbeatConsumer.js
node-rdkafka's subscription() throws `Local: Erroneous state` when the native
layer reports itself not connected, which it does as soon as `m_is_closing` is
set -- while the JS isConnected() flag stays true until the `disconnected`
event lands. So a caller cannot rely on isConnected() alone.

isPaused() read the subscription unguarded, and onEntryCommittable() evaluates
isPaused() inside its `if` condition, outside the try/catch that was added to
keep offsetsStore() from crashing the process. The condition meant to prevent
ERR__STATE could therefore raise it. The throw escaped through
_onEntryProcessingDone, which runs from the processing-queue completion
callback, so finishProcessingTask() was skipped: the task slot leaked and
_processingQueue.idle() could never become true. With no uncaughtException
handler anywhere in the service, the exception then exited the process without
leaving the consumer group, delaying the rebalance for surviving members.

Read the subscription through a _getSubscription() helper that reports "no
subscription" when the call fails, and route all four readers through it.
isPaused() treats the consumer as paused and getServiceStatus() reports
inactive, so callers skip storing offsets and the entry is re-delivered to
whichever consumer holds the partition next. close() relied on a `|| []`
fallback that could never run, and a throw there would have skipped the
waterfall callback and hung shutdown; it now proceeds straight to disconnect.
resume() subscribes when the subscription is unreadable rather than throwing,
since leaving the consumer paused with nothing to retry it is worse.

Reporting "not subscribed" is the conservative answer for every one of these
readers. subscribe() is deliberately left unguarded: swallowing a failure there
would leave a consumer that never subscribes, which isReady() does not detect
(it checks only connectivity), turning a loud crash into a silent stall.

Same log-don't-crash contract as BB-758 and BB-823.

Issue: BB-845
@delthas
delthas force-pushed the bugfix/BB-845/guard-subscription-state branch from 4413584 to a33d1f1 Compare August 18, 2026 14:22
@delthas
delthas marked this pull request as ready for review August 19, 2026 14:45
@delthas
delthas requested review from a team, SylvainSenechal and francoisferrand August 19, 2026 14:45

@francoisferrand francoisferrand 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.

  • not sure if we should handle in 9.5 or just 9.6
  • if we remove the librdkafka pin to 2.3 (and bump to 2.12), do we still need? It seems we may actually be able to to it after all (i.e. revert the revert)
  • nothing wrong with this PR, but I wonder if we are not missing something: we are already guarding lot of code with isConnected calls, so I wonder if the exception here is a real corner case or just a missing isConencted call (i.e. is it expected that the API throws, or are we just not respecting the contract)

@scality scality deleted a comment from bert-e Aug 20, 2026
@bert-e

bert-e commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Request integration branches

Waiting for integration branch creation to be requested by the user.

To request integration branches, please comment on this pull request with the following command:

/create_integration_branches

Alternatively, the /approve and /create_pull_requests commands will automatically
create the integration branches.

@delthas

delthas commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks — on the two technical points:

Is this a missing isConnected() call rather than a real corner case?

It isn't: onEntryCommittable already calls it (lib/BackbeatConsumer.js:949committableOffset !== null && this._consumer.isConnected() && !this.isPaused()), and it cannot close the window, because the two "connected" notions are different objects:

side definition when it flips
JS isConnected() !!(this._isConnected && this._client)lib/client.js:281 _isConnected is cleared only on the disconnected event (lib/client.js:112)
native IsConnected() !m_is_closing && m_client != NULLsrc/connection.cc:79 the instant m_is_closing is set

KafkaConsumer::Subscription() checks the native one and returns Baton(RdKafka::ERR__STATE, "Consumer is not connected") (src/kafka-consumer.cc:348-351), which subscription() converts into a throw via _errorWrap(..., true) (lib/kafka-consumer.js:321). disconnect() sets m_is_closing immediately while the disconnected event arrives later, so there is a window where JS reports connected and the native call throws. No arrangement of isConnected() checks closes it — which is why this guards the call rather than adding another precondition.

So the API is expected to throw here; we were respecting the contract as documented by isConnected(), and that contract is just weaker than it looks.

Does bumping librdkafka to 2.12 remove the need?

No — the window is in node-rdkafka's JS wrapper state model, not in librdkafka, so it is version-independent.

It does get rarer in practice. From the CI A/B (25 runs per arm, full lib suite), the isPaused/ERR__STATE signature appeared in 4/25 runs on the 17 Jul tree and 7/25 at HEAD, both on librdkafka 2.3.0, and 0/25 on 2.12 for both trees. But 25 clean runs of a test suite is not evidence that the production close path is safe — the tests do not exercise a pod terminated mid-unassign, which is the realistic trigger.

@delthas

delthas commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Waiting untilethe 9.5 vs 9.6 discussion is settled.

@delthas

delthas commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

/wait

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