Skip to content

Commit

Permalink
Fix NPE when an error is returned after timeout
Browse files Browse the repository at this point in the history
Fix NPE happening when the server responds too late with an error, and
the request is already failed with timeout and request metadata is
removed
  • Loading branch information
akudiyar committed Aug 27, 2023
1 parent bfcebff commit 8e8b553
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ public Long getRequestId() {

@Override
public String toString() {
Optional<TarantoolRequestSignature> requestSignature = request.getSignature();
return !requestSignature.isPresent() ?
String.format("request id: %d", getRequestId()) :
String.format("request signature: %s", requestSignature.get());
return request.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.tarantool.driver.core.RequestFutureManager;
import io.tarantool.driver.core.TarantoolRequestMetadata;
import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.protocol.TarantoolRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Performs registration of requests and pushes them forward. Should stay first in the channel pipeline
*
* @author Alexey Kuzin
*/
public class TarantoolRequestHandler extends ChannelOutboundHandlerAdapter {
private final Logger log = LoggerFactory.getLogger(TarantoolRequestHandler.class);
private final RequestFutureManager futureManager;

public TarantoolRequestHandler(RequestFutureManager futureManager) {
Expand All @@ -25,8 +30,15 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
TarantoolRequest request = (TarantoolRequest) msg;
ctx.write(request).addListener((ChannelFutureListener) channelFuture -> {
if (!channelFuture.isSuccess()) {
futureManager.getRequest(request.getHeader().getSync()).getFuture()
.completeExceptionally(new TarantoolClientException(channelFuture.cause()));
TarantoolRequestMetadata requestMeta = futureManager.getRequest(request.getHeader().getSync());
// The request metadata may has been deleted already after timeout
if (requestMeta != null) {
requestMeta.getFuture()
.completeExceptionally(new TarantoolClientException(channelFuture.cause()));
} else {
log.info(
"Received an error for {} but it is already timed out: {}", request, channelFuture.cause());
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public void toMessagePack(MessagePacker packer, MessagePackObjectMapper mapper)
}
}

@Override
public String toString() {
return !signature.isPresent() ?
String.format("request id: %d", header.getSync()) :
String.format("request signature: %s", signature.get());
}

/**
* Base class for request builder implementations
*/
Expand Down

0 comments on commit 8e8b553

Please sign in to comment.