Buggy code:
public class OperatorMerge < T > implements Operator < T , Observable < ? extends T > > { public OperatorMerge ( ) { } public OperatorMerge ( boolean delayErrors ) { } private final boolean delayErrors ; @ Override public Subscriber < Observable < ? extends T > > call ( final Subscriber < ? super T > child ) { } private static final class MergeSubscriber < T > extends Subscriber < Observable < ? extends T > > { final NotificationLite < T > on = NotificationLite . instance ( ) ; final Subscriber < ? super T > actual ; private final OperatorMerge . MergeProducer < T > mergeProducer ; private int wip ; private boolean completed ; private final boolean delayErrors ; private ConcurrentLinkedQueue < Throwable > exceptions ; private volatile SubscriptionIndexedRingBuffer < OperatorMerge . InnerSubscriber < T > > childrenSubscribers ; private RxRingBuffer scalarValueQueue = null ; private int missedEmitting = 0 ; private boolean emitLock = false ; public MergeSubscriber ( Subscriber < ? super T > actual , boolean delayErrors ) { } @ Override public void onStart ( ) { } @ Override public void onNext ( Observable < ? extends T > t ) { } private void handleNewSource ( Observable < ? extends T > t ) { } private void handleScalarSynchronousObservable ( ScalarSynchronousObservable < ? extends T > t ) { } private void handleScalarSynchronousObservableWithoutRequestLimits ( ScalarSynchronousObservable < ? extends T > t ) { } private void handleScalarSynchronousObservableWithRequestLimits ( ScalarSynchronousObservable < ? extends T > t ) { } private void initScalarValueQueueIfNeeded ( ) { } private synchronized boolean releaseEmitLock ( ) { } private synchronized boolean getEmitLock ( ) { } private boolean drainQueuesIfNeeded ( ) { } int lastDrainedIndex = 0 ; private void drainChildrenQueues ( ) { } private int drainScalarValueQueue ( ) { } final Func1 < OperatorMerge . InnerSubscriber < T > , Boolean > DRAIN_ACTION ; @ Override public void onError ( Throwable e ) { } private void innerError ( Throwable e ) { } @ Override public void onCompleted ( ) { } void completeInner ( OperatorMerge . InnerSubscriber < T > s ) { } private void drainAndComplete ( ) { } } private static final class MergeProducer < T > implements Producer { private final OperatorMerge . MergeSubscriber < T > ms ; public MergeProducer ( OperatorMerge . MergeSubscriber < T > ms ) { } private volatile long requested = 0 ; @ SuppressWarnings ( "rawtypes" ) static final AtomicLongFieldUpdater < OperatorMerge . MergeProducer > REQUESTED = AtomicLongFieldUpdater . newUpdater ( OperatorMerge . MergeProducer . class , "requested" ) ; @ Override public void request ( long n ) { if ( ( requested ) == ( Long . MAX_VALUE ) ) { return ; } if ( n == ( Long . MAX_VALUE ) ) { requested = Long . MAX_VALUE ; } else { OperatorMerge . MergeProducer . REQUESTED . getAndAdd ( this , n ) ; if ( ms . drainQueuesIfNeeded ( ) ) { boolean sendComplete = false ; <START_BUG> synchronized ( this ) { <END_BUG> if ( ( ( ( ms . wip ) == 0 ) && ( ( ms . scalarValueQueue ) != null ) ) && ( ms . scalarValueQueue . isEmpty ( ) ) ) { sendComplete = true ; } } if ( sendComplete ) { ms . drainAndComplete ( ) ; } } } } } private static final class InnerSubscriber < T > extends Subscriber < T > { public int sindex ; final OperatorMerge . MergeSubscriber < T > parentSubscriber ; final OperatorMerge . MergeProducer < T > producer ; volatile int terminated ; @ SuppressWarnings ( "rawtypes" ) static final AtomicIntegerFieldUpdater < OperatorMerge . InnerSubscriber > ONCE_TERMINATED = AtomicIntegerFieldUpdater . newUpdater ( OperatorMerge . InnerSubscriber . class , "terminated" ) ; private final RxRingBuffer q = RxRingBuffer . getSpmcInstance ( ) ; int emitted = 0 ; final int THRESHOLD = ( ( int ) ( ( q . capacity ( ) ) * 0.7 ) ) ; public InnerSubscriber ( OperatorMerge . MergeSubscriber < T > parent , OperatorMerge . MergeProducer < T > producer ) { } @ Override public void onNext ( T t ) { } @ Override public void onError ( Throwable e ) { } @ Override public void onCompleted ( ) { } public void requestMore ( long n ) { } private void emit ( T t , boolean complete ) { } private void enqueue ( T t , boolean complete ) { } private int drainRequested ( ) { } private int drainAll ( ) { } private int drainQueue ( ) { } } No newline ; at end ; of file ; }

Fix:
synchronized ( ms ) {