Skip to content

Commit 21696cd

Browse files
committed
xds: Detect negative ref count for xds client
If the refcount goes negative, then the next getObject() will return null. This was noticed during code inspection when investigating a NullPointerException in b/454396128, although it is unclear if this is actually happening.
1 parent 9cc31e5 commit 21696cd

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

xds/src/main/java/io/grpc/xds/SharedXdsClientPoolProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,10 @@ public XdsClient returnObject(Object object) {
202202
metricReporter = null;
203203
targetToXdsClientMap.remove(target);
204204
scheduler = SharedResourceHolder.release(GrpcUtil.TIMER_SERVICE, scheduler);
205+
} else if (refCount < 0) {
206+
assert false; // We want our tests to fail
207+
log.log(Level.SEVERE, "Negative reference count. File a bug", new Exception());
208+
refCount = 0;
205209
}
206210
return null;
207211
}

xds/src/test/java/io/grpc/xds/XdsClientFallbackTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ public void setUp() throws XdsInitializationException {
186186

187187
@After
188188
public void cleanUp() {
189-
if (xdsClientPool != null) {
190-
xdsClientPool.returnObject(xdsClient);
189+
if (xdsClient != null) {
190+
xdsClient = xdsClientPool.returnObject(xdsClient);
191191
}
192192
CommonBootstrapperTestUtils.setEnableXdsFallback(originalEnableXdsFallback);
193193
}

0 commit comments

Comments
 (0)