You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/03/20 16:36:06 UTC

[GitHub] [pulsar] BewareMyPower opened a new issue #9993: Thread safe problem about HandlerState#changeToReadyState

BewareMyPower opened a new issue #9993:
URL: https://github.com/apache/pulsar/issues/9993


   I'm not sure if it's bug. It's more a question. As we can see, https://github.com/apache/pulsar/blob/608929227824fe4303f46aa432e42af77bcbf625/pulsar-client/src/main/java/org/apache/pulsar/client/impl/HandlerState.java#L53-L56
   
   `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` or `Connecting` was a closed state.
   
   I see references of `changeToReadyState` in `ProducerImpl` and `ConsumerImpl` were protected by the lock directly or indirectly, like https://github.com/apache/pulsar/blob/608929227824fe4303f46aa432e42af77bcbf625/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java#L738-L739
   
   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.
   
   https://github.com/apache/pulsar/blob/608929227824fe4303f46aa432e42af77bcbf625/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TransactionMetaStoreHandler.java#L115-L117
   
   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?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] codelipenghui commented on issue #9993: [QUESTION] Thread safe problem about HandlerState#changeToReadyState

Posted by GitBox <gi...@apache.org>.
codelipenghui commented on issue #9993:
URL: https://github.com/apache/pulsar/issues/9993#issuecomment-1058892151


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


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [pulsar] BewareMyPower commented on issue #9993: [QUESTION] Thread safe problem about HandlerState#changeToReadyState

Posted by GitBox <gi...@apache.org>.
BewareMyPower commented on issue #9993:
URL: https://github.com/apache/pulsar/issues/9993#issuecomment-803407153


   Is following implementation better?
   
   ```java
       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;
       }
   
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org