@@ -68,6 +68,24 @@ public final Disposable connect() {
6868 return cc .disposable ;
6969 }
7070
71+ /**
72+ * Apply a workaround for a race condition with the regular publish().refCount()
73+ * so that racing subscribers and refCount won't hang.
74+ *
75+ * @return the ConnectableFlowable to work with
76+ * @since 2.2.10
77+ */
78+ private ConnectableFlowable <T > onRefCount () {
79+ if (this instanceof FlowablePublishClassic ) {
80+ @ SuppressWarnings ("unchecked" )
81+ FlowablePublishClassic <T > fp = (FlowablePublishClassic <T >) this ;
82+ return RxJavaPlugins .onAssembly (
83+ new FlowablePublishAlt <T >(fp .publishSource (), fp .publishBufferSize ())
84+ );
85+ }
86+ return this ;
87+ }
88+
7189 /**
7290 * Returns a {@code Flowable} that stays connected to this {@code ConnectableFlowable} as long as there
7391 * is at least one subscription to this {@code ConnectableFlowable}.
@@ -89,7 +107,7 @@ public final Disposable connect() {
89107 @ SchedulerSupport (SchedulerSupport .NONE )
90108 @ BackpressureSupport (BackpressureKind .PASS_THROUGH )
91109 public Flowable <T > refCount () {
92- return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(this ));
110+ return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(onRefCount () ));
93111 }
94112
95113 /**
@@ -216,7 +234,7 @@ public final Flowable<T> refCount(int subscriberCount, long timeout, TimeUnit un
216234 ObjectHelper .verifyPositive (subscriberCount , "subscriberCount" );
217235 ObjectHelper .requireNonNull (unit , "unit is null" );
218236 ObjectHelper .requireNonNull (scheduler , "scheduler is null" );
219- return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(this , subscriberCount , timeout , unit , scheduler ));
237+ return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(onRefCount () , subscriberCount , timeout , unit , scheduler ));
220238 }
221239
222240 /**
0 commit comments