Skip to content

Fix subscription consumer lifecycle state ordering#18151

Open
Caideyipi wants to merge 1 commit into
masterfrom
fix/subscription-consumer-lifecycle-race
Open

Fix subscription consumer lifecycle state ordering#18151
Caideyipi wants to merge 1 commit into
masterfrom
fix/subscription-consumer-lifecycle-race

Conversation

@Caideyipi

Copy link
Copy Markdown
Collaborator

Description

This PR fixes subscription consumer lifecycle state ordering for both push and pull consumers.

Concrete failure scenario

A push or pull subscription consumer calls open(). AbstractSubscriptionConsumer.open() opens providers and submits the heartbeat and endpoint-sync workers before the push/pull subclass flips its own isClosed flag to false. If either scheduled worker runs immediately, the worker dispatches to the subclass isClosed() override, observes the consumer as closed, cancels its own future, and leaves the already-open consumer without heartbeat or endpoint synchronization.

A symmetric close path can also leak providers: push/pull close() previously called the base close before marking the subclass closed. During that window, an endpoint-sync task can still treat the consumer as open and reopen subscription providers after close has begun.

Fix

  • Mark push/pull consumers open before invoking the base open path, and roll the subclass state back if open fails.
  • Mark push/pull consumers closed before closing providers in the base close path.
  • Re-check the consumer closed state after acquiring the provider write lock in heartbeat and endpoint sync.
  • Add focused lifecycle unit tests for push and pull consumers.

Tests

  • mvn spotless:apply -pl iotdb-client/subscription
  • mvn -pl iotdb-client/subscription -Dtest=SubscriptionConsumerLifecycleTest test
  • mvn -pl iotdb-client/subscription test
  • mvn -pl iotdb-client/subscription -P with-zh-locale -DskipTests test-compile

This PR has:

  • been self-reviewed.
    • concurrent read
    • concurrent write
    • concurrent read and write
  • added documentation for new or modified features or behaviors.
  • added Javadocs for most classes and all non-trivial methods.
  • added or updated version, license, or notice information
  • added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage.
  • added integration tests.
  • been tested in a test IoTDB cluster.

Key changed/added classes (or packages if there are too many classes) in this PR
  • org.apache.iotdb.session.subscription.consumer.base.AbstractSubscriptionPushConsumer
  • org.apache.iotdb.session.subscription.consumer.base.AbstractSubscriptionPullConsumer
  • org.apache.iotdb.session.subscription.consumer.base.AbstractSubscriptionProviders
  • org.apache.iotdb.session.subscription.consumer.base.SubscriptionConsumerLifecycleTest

@sonarqubecloud

sonarqubecloud Bot commented Jul 8, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
60.0% Duplication on New Code (required ≤ 5%)

See analysis details on SonarQube Cloud

@codecov

codecov Bot commented Jul 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 37.50000% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 41.85%. Comparing base (b353905) to head (1236dc8).

Files with missing lines Patch % Lines
...n/consumer/base/AbstractSubscriptionProviders.java 0.00% 4 Missing ⚠️
...onsumer/base/AbstractSubscriptionPullConsumer.java 50.00% 3 Missing ⚠️
...onsumer/base/AbstractSubscriptionPushConsumer.java 50.00% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master   #18151      +/-   ##
============================================
- Coverage     41.87%   41.85%   -0.03%     
  Complexity      318      318              
============================================
  Files          5297     5297              
  Lines        374168   374180      +12     
  Branches      48302    48304       +2     
============================================
- Hits         156693   156608      -85     
- Misses       217475   217572      +97     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants