Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED] JetStream: natsSubscription_Fetch() not honoring timeout #634

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions src/js.c
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,7 @@ jsSub_scheduleFlowControlResponse(jsSub *jsi, const char *reply)
}

static natsStatus
_checkMsg(natsMsg *msg, bool checkSts, bool *usrMsg, natsMsg *mhMsg)
_checkMsg(natsMsg *msg, bool checkSts, bool *usrMsg, natsMsg *mhMsg, const char* reqSubj)
{
natsStatus s = NATS_OK;
const char *val = NULL;
Expand Down Expand Up @@ -1733,6 +1733,13 @@ _checkMsg(natsMsg *msg, bool checkSts, bool *usrMsg, natsMsg *mhMsg)
if (strncmp(val, CTRL_STATUS, HDR_STATUS_LEN) == 0)
return NATS_OK;

// Before checking for "errors", if the incoming status message is
// for a previous request (message's subject is not reqSubj), then
// simply return NATS_OK. The caller will destroy the message and
// proceed as if nothing was received.
if (strcmp(natsMsg_GetSubject(msg), reqSubj) != 0)
return NATS_OK;

// 404 indicating that there are no messages.
if (strncmp(val, NOT_FOUND_STATUS, HDR_STATUS_LEN) == 0)
return NATS_NOT_FOUND;
Expand Down Expand Up @@ -1798,6 +1805,7 @@ _fetch(natsMsgList *list, natsSubscription *sub, jsFetchRequest *req, bool simpl
bool sendReq = true;
jsSub *jsi = NULL;
natsMsg *mhMsg = NULL;
char *reqSubj = NULL;
bool noWait;

if (list == NULL)
Expand Down Expand Up @@ -1831,11 +1839,15 @@ _fetch(natsMsgList *list, natsSubscription *sub, jsFetchRequest *req, bool simpl
}
natsBuf_InitWithBackend(&buf, buffer, 0, sizeof(buffer));
nc = sub->conn;
rply = (const char*) sub->subject;
subj = jsi->nxtMsgSubj;
pmc = (sub->msgList.msgs > 0);
jsi->inFetch = true;
if (req->Heartbeat)
jsi->fetchID++;
if (nats_asprintf(&reqSubj, "%.*s%" PRIu64, (int) strlen(sub->subject)-1, sub->subject, jsi->fetchID) < 0)
s = nats_setDefaultError(NATS_NO_MEMORY);
else
rply = (const char*) reqSubj;
if ((s == NATS_OK) && req->Heartbeat)
{
int64_t hbi = req->Heartbeat / 1000000;
sub->refs++;
Expand Down Expand Up @@ -1882,8 +1894,9 @@ _fetch(natsMsgList *list, natsSubscription *sub, jsFetchRequest *req, bool simpl
}
if (s == NATS_OK)
{
// Here we care only about user messages.
s = _checkMsg(msg, false, &usrMsg, mhMsg);
// Here we care only about user messages. We don't need to pass
// the request subject since it is not even checked in this case.
s = _checkMsg(msg, false, &usrMsg, mhMsg, NULL);
if ((s == NATS_OK) && usrMsg)
{
msgs[count++] = msg;
Expand Down Expand Up @@ -1928,7 +1941,7 @@ _fetch(natsMsgList *list, natsSubscription *sub, jsFetchRequest *req, bool simpl
IFOK(s, natsSub_nextMsg(&msg, sub, timeout, true));
if (s == NATS_OK)
{
s = _checkMsg(msg, true, &usrMsg, mhMsg);
s = _checkMsg(msg, true, &usrMsg, mhMsg, rply);
if ((s == NATS_OK) && usrMsg)
{
msgs[count++] = msg;
Expand Down Expand Up @@ -1978,6 +1991,8 @@ _fetch(natsMsgList *list, natsSubscription *sub, jsFetchRequest *req, bool simpl
natsTimer_Stop(jsi->hbTimer);
natsSub_Unlock(sub);

NATS_FREE(reqSubj);

return NATS_UPDATE_ERR_STACK(s);
}

Expand Down Expand Up @@ -2558,10 +2573,19 @@ _subscribe(natsSubscription **new_sub, jsCtx *js, const char *subject, const cha
}
if (s == NATS_OK)
{
char *pullWCInbox = NULL;

if (isPullMode)
{
// Create a wildcard inbox.
s = natsConn_newInbox(nc, &inbox);
deliver = (const char*) inbox;
if (s == NATS_OK)
{
if (nats_asprintf(&pullWCInbox, "%s.*", (const char*) inbox) < 0)
s = nats_setDefaultError(NATS_NO_MEMORY);
}
if (s == NATS_OK)
deliver = (const char*) pullWCInbox;
}
// Create the NATS subscription on given deliver subject. Note that
// cb/cbClosure will be NULL for sync or pull subscriptions.
Expand All @@ -2576,6 +2600,7 @@ _subscribe(natsSubscription **new_sub, jsCtx *js, const char *subject, const cha
sub->refs--;
natsSub_Unlock(sub);
}
NATS_FREE(pullWCInbox);
}
if ((s == NATS_OK) && create)
{
Expand Down
1 change: 1 addition & 0 deletions src/natsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ typedef struct __jsSub
bool ordered;
bool dc; // delete JS consumer in Unsub()/Drain()
bool ackNone;
uint64_t fetchID;

// This is ConsumerInfo's Pending+Consumer.Delivered that we get from the
// add consumer response. Note that some versions of the server gather the
Expand Down
60 changes: 58 additions & 2 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -27637,15 +27637,28 @@ _sendToPullSub(void *closure)
{
struct threadArg *args = (struct threadArg*) closure;
natsMsg *msg = NULL;
char *subj = NULL;
natsSubscription *sub = NULL;
uint64_t id = 0;
natsStatus s;

nats_Sleep(250);
natsMutex_Lock(args->m);
s = natsMsg_create(&msg, args->sub->subject, (int) strlen(args->sub->subject),
NULL, 0, args->string, (int) strlen(args->string), (int) strlen(args->string));
sub = args->sub;
natsSub_Lock(sub);
id = sub->jsi->fetchID;
if (args->sum != 0)
id = (uint64_t) args->sum;
if (nats_asprintf(&subj, "%.*s%" PRIu64, (int) strlen(sub->subject)-1, sub->subject, id) < 0)
s = NATS_NO_MEMORY;
else
s = natsMsg_create(&msg, subj, (int) strlen(subj),
NULL, 0, args->string, (int) strlen(args->string), (int) strlen(args->string));
natsSub_Unlock(sub);
IFOK(s, natsConnection_PublishMsg(args->nc, msg));
natsMutex_Unlock(args->m);
natsMsg_Destroy(msg);
free(subj);
}

static void
Expand Down Expand Up @@ -27710,6 +27723,18 @@ _dropIdleHBs(natsConnection *nc, natsMsg **msg, void* closure)
*msg = NULL;
}

static void
_dropTimeoutProto(natsConnection *nc, natsMsg **msg, void* closure)
{
const char *val = NULL;

if (natsMsgHeader_Get(*msg, STATUS_HDR, &val) != NATS_OK)
return;

natsMsg_Destroy(*msg);
*msg = NULL;
}

static void
test_JetStreamSubscribePull(void)
{
Expand Down Expand Up @@ -28075,6 +28100,37 @@ test_JetStreamSubscribePull(void)
testCond((s == NATS_OK) && (list.Msgs != NULL) && (list.Count == 1) && (jerr == 0));

natsMsgList_Destroy(&list);

test("Fetch returns before 408: ");
natsConn_setFilter(nc, _dropTimeoutProto);
start = nats_Now();
s = natsSubscription_Fetch(&list, sub, 1, 1000, NULL);
dur = nats_Now() - start;
testCond((s == NATS_TIMEOUT) && (list.Count == 0) && (dur >= 600));
nats_clearLastError();

test("Next Fetch waits for proper timeout: ");
nats_Sleep(100);
natsConn_setFilter(nc, NULL);
natsMutex_Lock(args.m);
args.nc = nc;
args.sub = sub;
args.string = "NATS/1.0 408 Request Timeout\r\n\r\n";
// Will make the 408 sent to a subject ID with 1 while we are likely at 2 or above.
// So this will be considered a "late" 408 timeout and should be ignored.
args.sum = 1;
natsMutex_Unlock(args.m);
s = natsThread_Create(&t, _sendToPullSub, (void*) &args);
start = nats_Now();
IFOK(s, natsSubscription_Fetch(&list, sub, 1, 1000, NULL));
dur = nats_Now() - start;
testCond((s == NATS_TIMEOUT) && (list.Count == 0) && (dur >= 600));
nats_clearLastError();

natsThread_Join(t);
natsThread_Destroy(t);
t = NULL;

natsSubscription_Destroy(sub);
sub = NULL;

Expand Down