Skip to content

Commit

Permalink
GH-1374: fix NPE on server.destroy() when queue mode is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jvermillard authored and sbernard31 committed Jan 4, 2023
1 parent 938e43d commit c1123be
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,9 @@ public void destroy() {
((Stoppable) requestSender).stop();
}

presenceService.destroy();
if (presenceService != null) {
presenceService.destroy();
}

LOG.info("LWM2M server destroyed.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,34 @@ public void testStartStopDestroy() throws InterruptedException {
assertEquals("All news created threads must be destroyed", numberOfThreadbefore, Thread.activeCount());
}

@Test
public void testStartStopDestroyQueueModeDisabled() throws InterruptedException {
// look at nb active thread before.
int numberOfThreadbefore = Thread.activeCount();

LeshanServer server = new LeshanServerBuilder().setLocalAddress(new InetSocketAddress(0))
.disableQueueModeSupport().build();
server.start();
Thread.sleep(100);
// HACK force creation thread creation.
forceThreadsCreation(server);
Thread.sleep(100);
server.stop();
Thread.sleep(100);
server.destroy();

// ensure all thread are destroyed
Thread.sleep(500);
assertEquals("All news created threads must be destroyed", numberOfThreadbefore, Thread.activeCount());
}

private void forceThreadsCreation(LeshanServer server) {
Registration reg = new Registration.Builder("id", "endpoint", Identity.unsecure(new InetSocketAddress(5555)))
.bindingMode(BindingMode.UQ).build();
// Force timer thread creation of preference service.
((PresenceServiceImpl) server.getPresenceService()).setAwake(reg);
if (server.getPresenceService() != null) {
((PresenceServiceImpl) server.getPresenceService()).setAwake(reg);
}
// Force time thread creation of CoapAsyncRequestObserver
server.send(reg, new ReadRequest(3), new ResponseCallback<ReadResponse>() {
@Override
Expand Down

0 comments on commit c1123be

Please sign in to comment.