@@ -68,6 +68,24 @@ public final Disposable connect() {
68
68
return cc .disposable ;
69
69
}
70
70
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
+
71
89
/**
72
90
* Returns a {@code Flowable} that stays connected to this {@code ConnectableFlowable} as long as there
73
91
* is at least one subscription to this {@code ConnectableFlowable}.
@@ -89,7 +107,7 @@ public final Disposable connect() {
89
107
@ SchedulerSupport (SchedulerSupport .NONE )
90
108
@ BackpressureSupport (BackpressureKind .PASS_THROUGH )
91
109
public Flowable <T > refCount () {
92
- return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(this ));
110
+ return RxJavaPlugins .onAssembly (new FlowableRefCount <T >(onRefCount () ));
93
111
}
94
112
95
113
/**
@@ -216,7 +234,7 @@ public final Flowable<T> refCount(int subscriberCount, long timeout, TimeUnit un
216
234
ObjectHelper .verifyPositive (subscriberCount , "subscriberCount" );
217
235
ObjectHelper .requireNonNull (unit , "unit is null" );
218
236
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 ));
220
238
}
221
239
222
240
/**
0 commit comments