Skip to content

Commit d6dc64c

Browse files
committed
Fix NPE in ChannelPool metrics methods when pool is uninitialized
When the initial TCP connection to a node fails, ChannelPool.connectFuture completes (so the pool is added to PoolManager.pools) while `channels` is still null — initialize() is only called on the success path. The four methods size(), getAvailableIds(), getInFlight(), and getOrphanedIds() called Arrays.stream(channels) without guarding against this, throwing NullPointerException whenever a Dropwizard Metrics reporter (JMX, Graphite, etc.) scraped the gauge values during the reconnection window. Fix: mirror the pattern already used by next(), which checks singleThreaded.initialized before touching channels. Using the volatile initialized flag is correct by the JMM: the volatile write at line 365 (initialized = true) happens-after all channels writes, so any thread that reads initialized == true is guaranteed to see channels as fully populated. Fixes: CUSTOMER-413
1 parent 1d65bce commit d6dc64c

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

core/src/main/java/com/datastax/oss/driver/internal/core/pool/ChannelPool.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,17 @@ public DriverChannel next(@Nullable Token routingKey, @Nullable Integer shardSug
210210

211211
/** @return the number of active channels in the pool. */
212212
public int size() {
213+
if (!singleThreaded.initialized) {
214+
return 0;
215+
}
213216
return Arrays.stream(channels).mapToInt(ChannelSet::size).sum();
214217
}
215218

216219
/** @return the number of available stream ids on all channels in the pool. */
217220
public int getAvailableIds() {
221+
if (!singleThreaded.initialized) {
222+
return 0;
223+
}
218224
return Arrays.stream(channels).mapToInt(ChannelSet::getAvailableIds).sum();
219225
}
220226

@@ -223,6 +229,9 @@ public int getAvailableIds() {
223229
* {@link #getOrphanedIds() orphaned ids}).
224230
*/
225231
public int getInFlight() {
232+
if (!singleThreaded.initialized) {
233+
return 0;
234+
}
226235
return Arrays.stream(channels).mapToInt(ChannelSet::getInFlight).sum();
227236
}
228237

@@ -232,6 +241,9 @@ public int getInFlight() {
232241
* might still come from the server.
233242
*/
234243
public int getOrphanedIds() {
244+
if (!singleThreaded.initialized) {
245+
return 0;
246+
}
235247
return Arrays.stream(channels).mapToInt(ChannelSet::getOrphanedIds).sum();
236248
}
237249

0 commit comments

Comments
 (0)