From 3c64f4a6c98847266e1f784ad5407d04d4a314b9 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Wed, 8 Mar 2023 17:28:28 -0700 Subject: [PATCH 1/3] [FIXED] natsConnection_RequestMsg() incorrectly returns NATS_NO_SERVER_SUPPORT When using `natsOptions_SetRetryOnFailedConnect` option to connect and the library is not yet connected, calling `natsConnection_RequestMsg` with a message with headers would incorrectly return `NATS_NO_SERVER_SUPPORT` instead of `NATS_TIMEOUT`. Resolves #644 Signed-off-by: Ivan Kozlovic --- src/pub.c | 2 +- test/test.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/pub.c b/src/pub.c index 5a563bfba..3a6e1e260 100644 --- a/src/pub.c +++ b/src/pub.c @@ -104,7 +104,7 @@ natsConn_publish(natsConnection *nc, natsMsg *msg, const char *reply, bool direc // accessing the headers. It should still be considered having headers. if ((msg->headers != NULL) || natsMsg_needsLift(msg)) { - if (!nc->info.headers) + if (!nc->initc && !nc->info.headers) { natsConn_Unlock(nc); diff --git a/test/test.c b/test/test.c index 27990d6e9..a692a9e39 100644 --- a/test/test.c +++ b/test/test.c @@ -9530,6 +9530,8 @@ test_RetryOnFailedConnect(void) int64_t end = 0; natsThread *t = NULL; natsSubscription *sub = NULL; + natsMsg *msg = NULL; + natsMsg *rmsg = NULL; struct threadArg arg; s = _createDefaultThreadArgsForCbTests(&arg); @@ -9596,6 +9598,16 @@ test_RetryOnFailedConnect(void) testCond((s == NATS_NOT_YET_CONNECTED) && (nc != NULL)); nats_clearLastError(); + // Request with a message with headers does timeout as opposed to returning + // the NATS_NO_SERVER_SUPPORT error. + test("Request with message with headers: "); + s = natsMsg_Create(&msg, "request.headers", NULL, "hello", 5); + IFOK(s, natsMsgHeader_Set(msg, "some", "header")); + IFOK(s, natsConnection_RequestMsg(&rmsg, nc, msg, 250)); + testCond(s == NATS_TIMEOUT); + nats_clearLastError(); + natsMsg_Destroy(msg); + test("Subscription ok: "); arg.control = 99; s = natsConnection_Subscribe(&sub, nc, "foo", _recvTestString, (void*)&arg); From 0fc77109bbcb9805430a512e9372faf726aae825 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Thu, 9 Mar 2023 15:17:28 -0700 Subject: [PATCH 2/3] Added a comment Signed-off-by: Ivan Kozlovic --- src/pub.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/pub.c b/src/pub.c index 3a6e1e260..c865fb2c5 100644 --- a/src/pub.c +++ b/src/pub.c @@ -104,6 +104,10 @@ natsConn_publish(natsConnection *nc, natsMsg *msg, const char *reply, bool direc // accessing the headers. It should still be considered having headers. if ((msg->headers != NULL) || natsMsg_needsLift(msg)) { + // Do the check for server's headers support only after we have completed + // the initial connect (we could be here with initc true - that is, initial + // connect in progress - when using natsOptions_SetRetryOnFailedConnect + // option). if (!nc->initc && !nc->info.headers) { natsConn_Unlock(nc); From 511103350010afaf76e714f3558839417c1eb02d Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Thu, 9 Mar 2023 15:45:16 -0700 Subject: [PATCH 3/3] Fix other possible STAN flappers Signed-off-by: Ivan Kozlovic --- test/test.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test/test.c b/test/test.c index a692a9e39..a7cf40ed7 100644 --- a/test/test.c +++ b/test/test.c @@ -32674,6 +32674,9 @@ test_StanBasicPublish(void) stanConnection_Destroy(sc); + if (valgrind) + nats_Sleep(900); + _stopServer(pid); } @@ -32731,6 +32734,9 @@ test_StanBasicPublishAsync(void) _destroyDefaultThreadArgs(&args); + if (valgrind) + nats_Sleep(900); + _stopServer(pid); }