Skip to content

[QUESTION] Thread safe problem about HandlerState#changeToReadyState #9993

Closed

Description

@BewareMyPower

I'm not sure if it's bug. It's more a question. As we can see,

protected boolean changeToReadyState() {
return (STATE_UPDATER.compareAndSet(this, State.Uninitialized, State.Ready)
|| STATE_UPDATER.compareAndSet(this, State.Connecting, State.Ready)
|| STATE_UPDATER.compareAndSet(this, State.RegisteringSchema, State.Ready));

HandlerState#changeToReadyState is not an atomic operation. I'm not sure there's a race case like following timeline

Time Event State Before State Now
1 STATE_UPDATER.compareAndSet(this, State.Uninitialized, State.Ready) State.Connecting State.Connecting
2 setState(State.Uninitialized) State.Connecting State.Uninitialized
3 STATE_UPDATER.compareAndSet(this, State.Connecting, State.Ready) State.Uninitialized State.Uninitialized
4 STATE_UPDATER.compareAndSet(this, State.RegisteringSchema, State.Ready) State.Uninitialized State.Uninitialized

As we can see, there's a time point that the state was changed back to Uninitialized from Connecting. However, we should expect the state to be Ready because neither Uninitialized nor Connecting was a closed state.

I see references of changeToReadyState in ProducerImpl and ConsumerImpl were protected by the lock directly or indirectly, like

synchronized (ConsumerImpl.this) {
if (changeToReadyState()) {

I'm not sure if the lock works because it requires some setState invocations are protected by the lock and I didn't check it in detail.

And in TransactionMetaStoreHandler#connectionOpened, there's no lock.

if (!changeToReadyState()) {
cnx.channel().close();
}

I'm not sure if the thread safety could be guaranteed. IMO, if there's no possibility that the state was changed back to Connecting or Uninitialized during changeToReadyState, it will be thread safe. Or this race condition is acceptable?

Activity

  1. changed the title [-]Thread safe problem about HandlerState#changeToReadyState[/-] [+][QUESTION] Thread safe problem about HandlerState#changeToReadyState[/+] on Mar 20, 2021
  2. BewareMyPower commented on Mar 20, 2021

    @BewareMyPower
    ContributorAuthor

    Is following implementation better?

        private static boolean notClosed(State state) {
            return state == State.Uninitialized || state == State.Connecting || state == State.RegisteringSchema;
        }
    
        // moves the state to ready if it wasn't closed
        protected boolean changeToReadyState() {
            return STATE_UPDATER.getAndUpdate(this, state -> (notClosed(state) ? State.Ready : state)) == State.Ready;
        }
  3. codelipenghui commented on Mar 4, 2022

    @codelipenghui
    Contributor

    The issue had no activity for 30 days, mark with Stale label.

  4. locked and limited conversation to collaborators on Nov 10, 2022
  5. converted this issue into a discussion #18401 on Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions