Skip to content

Commit

Permalink
Fix NPE cased by inflating message (#1045)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattisonchao authored and Technoboy- committed Jul 27, 2023
1 parent 9aed8bf commit a1ebce4
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ public void sendConnAck() {
builder.responseInformation(resInformation.value());
}
MqttAck connAck = builder.build();
sendAck(connAck).thenAccept(__ -> assignState(CONNECT_ACK, ESTABLISHED));
assignState(CONNECT_ACK, ESTABLISHED);
sendAck(connAck).exceptionally(ex -> {
assignState(ESTABLISHED, DISCONNECTED);
return null;
});
if (log.isDebugEnabled()) {
log.debug("The CONNECT message has been processed. CId={}", clientId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void channelRead(ChannelHandlerContext ctx, Object message) {
checkArgument(message instanceof MqttAdapterMessage);
MqttAdapterMessage adapterMsg = (MqttAdapterMessage) message;
MqttMessage mqttMessage = adapterMsg.getMqttMessage();
ProtocolMethodProcessor processor = processors.computeIfAbsent(adapterMsg.getClientId(), key -> {
final ProtocolMethodProcessor processor = processors.computeIfAbsent(adapterMsg.getClientId(), key -> {
MQTTBrokerProtocolMethodProcessor p = new MQTTBrokerProtocolMethodProcessor(mqttService, ctx);
CompletableFuture<Void> inactiveFuture = p.getInactiveFuture();
inactiveFuture.whenComplete((id, ex) -> {
Expand All @@ -65,6 +65,21 @@ public void channelRead(ChannelHandlerContext ctx, Object message) {
log.debug("Inbound handler read message : type={}, clientId : {} adapter encodeType : {}", messageType,
adapterMsg.getClientId(), adapterMsg.getEncodeType());
}
if (messageType != MqttMessageType.CONNECT && !processor.connectionEstablished()) {
if (adapterMsg.fromProxy()) {
if (log.isDebugEnabled()) {
log.debug("[{}] Ignore inflating message from proxy. message:{}",
ctx.channel().remoteAddress(), mqttMessage);
}
ReferenceCountUtil.safeRelease(mqttMessage); // release publish packet
return;
} else {
log.warn("[{}] Receive message before connect. message:{}",
ctx.channel().remoteAddress(), mqttMessage);
ReferenceCountUtil.safeRelease(mqttMessage); // release publish packet
ctx.channel().close();
}
}
switch (messageType) {
case CONNECT:
processor.processConnect(adapterMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,6 @@ public interface ProtocolMethodProcessor {
void processPingReq(MqttAdapterMessage msg);

void processAuthReq(MqttAdapterMessage msg);

boolean connectionEstablished();
}
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,11 @@ public void processPingReq(final MqttAdapterMessage msg) {
});
}

@Override
public boolean connectionEstablished() {
return connection != null && connection.getState() == Connection.ConnectionState.ESTABLISHED;
}

@Override
public void processDisconnect(final MqttAdapterMessage msg) {
if (isDisconnected.compareAndSet(false, true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ public void processPingReq(MqttAdapterMessage adapter) {
connection.send(pingResp());
}

@Override
public boolean connectionEstablished() {
return connection != null && connection.getState() == Connection.ConnectionState.ESTABLISHED;
}

@Override
public void processSubscribe(MqttAdapterMessage adapter) {
MqttSubscribeMessage msg = (MqttSubscribeMessage) adapter.getMqttMessage();
Expand Down

0 comments on commit a1ebce4

Please sign in to comment.