Skip to content

Commit c4881b4

Browse files
committed
NPE in PgDecoder if a notice is raised while no query is being executed (#1460)
See #1442 This may happen if the connection is used by a PgSubscriber. Signed-off-by: Thomas Segismont <[email protected]>
1 parent 8bde735 commit c4881b4

File tree

3 files changed

+40
-10
lines changed

3 files changed

+40
-10
lines changed

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/PgCommandCodec.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
*/
1717
package io.vertx.pgclient.impl.codec;
1818

19+
import io.netty.buffer.ByteBuf;
1920
import io.vertx.core.impl.logging.Logger;
2021
import io.vertx.core.impl.logging.LoggerFactory;
2122
import io.vertx.pgclient.PgException;
22-
import io.vertx.sqlclient.impl.command.CommandResponse;
2323
import io.vertx.sqlclient.impl.command.CommandBase;
24-
import io.netty.buffer.ByteBuf;
24+
import io.vertx.sqlclient.impl.command.CommandResponse;
2525

2626
import java.util.Arrays;
2727

@@ -68,10 +68,6 @@ void handleNoData() {
6868
logger.warn(getClass().getSimpleName() + " should handle message NoData");
6969
}
7070

71-
void handleNoticeResponse(NoticeResponse noticeResponse) {
72-
decoder.fireNoticeResponse(noticeResponse);
73-
}
74-
7571
void handleErrorResponse(ErrorResponse errorResponse) {
7672
logger.warn(getClass().getSimpleName() + " should handle message " + errorResponse);
7773
}

vertx-pg-client/src/main/java/io/vertx/pgclient/impl/codec/PgDecoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717

1818
package io.vertx.pgclient.impl.codec;
1919

20+
import io.netty.buffer.ByteBuf;
2021
import io.netty.buffer.ByteBufAllocator;
2122
import io.netty.buffer.CompositeByteBuf;
2223
import io.netty.channel.ChannelHandlerContext;
2324
import io.netty.channel.ChannelInboundHandlerAdapter;
24-
import io.vertx.sqlclient.impl.Notification;
25-
import io.vertx.pgclient.impl.util.Util;
26-
import io.netty.buffer.ByteBuf;
2725
import io.netty.util.ByteProcessor;
26+
import io.vertx.pgclient.impl.util.Util;
27+
import io.vertx.sqlclient.impl.Notification;
2828
import io.vertx.sqlclient.impl.command.CommandBase;
2929
import io.vertx.sqlclient.impl.command.CommandResponse;
3030

@@ -273,7 +273,7 @@ private void decodeError(ChannelHandlerContext ctx, ByteBuf in) {
273273
private void decodeNotice(ByteBuf in) {
274274
NoticeResponse response = new NoticeResponse();
275275
decodeErrorOrNotice(response, in);
276-
codec.peek().handleNoticeResponse(response);
276+
fireNoticeResponse(response);
277277
}
278278

279279
private void decodeErrorOrNotice(Response response, ByteBuf in) {

vertx-pg-client/src/test/java/io/vertx/pgclient/PubSubTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.vertx.pgclient;
1818

1919
import io.vertx.core.Vertx;
20+
import io.vertx.core.buffer.Buffer;
2021
import io.vertx.ext.unit.Async;
2122
import io.vertx.ext.unit.TestContext;
2223
import io.vertx.pgclient.impl.pubsub.PgSubscriberImpl;
@@ -28,6 +29,7 @@
2829
import org.junit.Test;
2930

3031
import java.util.Arrays;
32+
import java.util.concurrent.CompletableFuture;
3133
import java.util.concurrent.atomic.AtomicInteger;
3234
import java.util.concurrent.atomic.AtomicReference;
3335

@@ -307,4 +309,36 @@ public void testClose(TestContext ctx, String channelName) {
307309
endLatch.awaitSuccess(10000);
308310
closeLatch.awaitSuccess(10000);
309311
}
312+
313+
@Test
314+
public void testNoticedRaised(TestContext ctx) {
315+
Async async = ctx.async();
316+
ProxyServer proxy = ProxyServer.create(vertx, options.getPort(), options.getHost());
317+
CompletableFuture<Void> connected = new CompletableFuture<>();
318+
proxy.proxyHandler(conn -> {
319+
connected.thenAccept(v -> {
320+
Buffer noticeMsg = Buffer.buffer();
321+
noticeMsg.appendByte((byte) 'N'); // Notice
322+
noticeMsg.appendInt(0);
323+
noticeMsg.appendByte((byte) 0);
324+
noticeMsg.setInt(1, noticeMsg.length() - 1);
325+
conn.clientSocket().write(noticeMsg);
326+
});
327+
conn.connect();
328+
});
329+
proxy.listen(8080, "localhost", ctx.asyncAssertSuccess(v1 -> {
330+
PgConnectOptions connectOptions = new PgConnectOptions(options).setPort(8080).setHost("localhost");
331+
PgConnection.connect(vertx, connectOptions).onComplete(ctx.asyncAssertSuccess(conn -> {
332+
conn
333+
.noticeHandler(notice -> {
334+
async.complete();
335+
})
336+
.query("LISTEN \"toto\"")
337+
.execute()
338+
.onComplete(ctx.asyncAssertSuccess(result1 -> {
339+
connected.complete(null);
340+
}));
341+
}));
342+
}));
343+
}
310344
}

0 commit comments

Comments
 (0)