Skip to content

Commit fa9f51d

Browse files
committed
ZOOKEEPER-5051: Avoid NPE closing 4lw connection during startup
1 parent 34a9492 commit fa9f51d

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

zookeeper-server/src/main/java/org/apache/zookeeper/server/ZooKeeperServer.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ protected enum State {
330330
private final AuthenticationHelper authHelper = new AuthenticationHelper();
331331

332332
void removeCnxn(ServerCnxn cnxn) {
333-
zkDb.removeCnxn(cnxn);
333+
ZKDatabase db = zkDb;
334+
if (zkDb != null) {
335+
zkDb.removeCnxn(cnxn);
336+
}
334337
}
335338

336339
/**

zookeeper-server/src/test/java/org/apache/zookeeper/server/ZooKeeperServerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,14 @@ public void testUpdateQuotaExceededMetrics() {
225225
public void testUpdateQuotaExceededMetrics_nullNamespace() {
226226
assertDoesNotThrow(() -> ZooKeeperServer.updateQuotaExceededMetrics(null));
227227
}
228+
229+
@Test
230+
public void testRemoveCnxnBeforeStartData() {
231+
// Simulate a connection cleanup triggered before the server has fully started.
232+
// During this phase zkDb is still null (before startdata()).
233+
// 4-letter commands can hit this path.
234+
ZooKeeperServer zks = new ZooKeeperServer();
235+
ServerCnxn cnxn = mock(ServerCnxn.class);
236+
assertDoesNotThrow(() -> zks.removeCnxn(cnxn));
237+
}
228238
}

0 commit comments

Comments
 (0)