-
Notifications
You must be signed in to change notification settings - Fork 134
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] Possible crash if error handler if subscription was destroyed #659
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14373,6 +14373,130 @@ test_AsyncErrHandler(void) | |
_stopServer(serverPid); | ||
} | ||
|
||
static void | ||
_asyncErrBlockingCb(natsConnection *nc, natsSubscription *sub, natsStatus err, void* closure) | ||
{ | ||
struct threadArg *arg = (struct threadArg*) closure; | ||
|
||
natsMutex_Lock(arg->m); | ||
|
||
arg->sum++; | ||
|
||
while ((arg->sum == 1) && !arg->closed) | ||
natsCondition_Wait(arg->c, arg->m); | ||
|
||
if (sub != arg->sub) | ||
arg->status = NATS_ERR; | ||
|
||
if ((arg->status == NATS_OK) && (err != NATS_SLOW_CONSUMER)) | ||
arg->status = NATS_ERR; | ||
|
||
if (arg->status == NATS_OK) | ||
{ | ||
// Call some subscription API to make sure that the pointer has not been freed. | ||
arg->current = natsSubscription_IsValid(sub); | ||
} | ||
|
||
arg->done = true; | ||
natsCondition_Signal(arg->c); | ||
|
||
natsMutex_Unlock(arg->m); | ||
} | ||
|
||
static void | ||
test_AsyncErrHandlerSubDestroyed(void) | ||
{ | ||
natsStatus s; | ||
natsConnection *nc = NULL; | ||
natsOptions *opts = NULL; | ||
natsSubscription *sub = NULL; | ||
natsPid serverPid = NATS_INVALID_PID; | ||
natsMsg *msg = NULL; | ||
struct threadArg arg; | ||
|
||
s = _createDefaultThreadArgsForCbTests(&arg); | ||
if (s != NATS_OK) | ||
FAIL("Unable to setup test!"); | ||
|
||
s = natsOptions_Create(&opts); | ||
IFOK(s, natsOptions_SetURL(opts, NATS_DEFAULT_URL)); | ||
IFOK(s, natsOptions_SetMaxPendingMsgs(opts, 1)); | ||
IFOK(s, natsOptions_SetErrorHandler(opts, _asyncErrBlockingCb, (void*) &arg)); | ||
|
||
if (s != NATS_OK) | ||
FAIL("Unable to create options for test AsyncErrHandler"); | ||
|
||
serverPid = _startServer("nats://127.0.0.1:4222", NULL, true); | ||
CHECK_SERVER_STARTED(serverPid); | ||
|
||
test("Connect: "); | ||
s = natsConnection_Connect(&nc, opts); | ||
testCond(s == NATS_OK); | ||
|
||
test("Create sync sub: "); | ||
s = natsConnection_SubscribeSync(&sub, nc, "foo"); | ||
testCond(s == NATS_OK); | ||
|
||
natsMutex_Lock(arg.m); | ||
arg.sub = sub; | ||
arg.current = true; | ||
natsMutex_Unlock(arg.m); | ||
|
||
test("Cause error: "); | ||
s = natsConnection_PublishString(nc, "foo", "msg1"); | ||
IFOK(s, natsConnection_PublishString(nc, "foo", "msg2")); | ||
testCond(s == NATS_OK); | ||
|
||
// Wait a bit to make sure that we have a slow consumer. | ||
nats_Sleep(250); | ||
|
||
test("Next should be error: "); | ||
s = natsSubscription_NextMsg(&msg, sub, 1000); | ||
testCond((s == NATS_SLOW_CONSUMER) && (msg == NULL)); | ||
nats_clearLastError(); | ||
s = NATS_OK; | ||
|
||
test("Consume 1: "); | ||
s = natsSubscription_NextMsg(&msg, sub, 1000); | ||
testCond(s == NATS_OK); | ||
natsMsg_Destroy(msg); | ||
msg = NULL; | ||
|
||
test("Cause error again: "); | ||
s = natsConnection_PublishString(nc, "foo", "msg3"); | ||
IFOK(s, natsConnection_PublishString(nc, "foo", "msg4")); | ||
testCond(s == NATS_OK); | ||
|
||
test("Wait a bit for async error to be posted: "); | ||
nats_Sleep(200); | ||
testCond(true); | ||
|
||
test("Destroy subscription: "); | ||
natsSubscription_Destroy(sub); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
test("Wait for async error callback to return: "); | ||
natsMutex_Lock(arg.m); | ||
// First unblock the first instance | ||
arg.closed = true; | ||
natsCondition_Signal(arg.c); | ||
// Now wait for the callback to have processed both and be done. | ||
while ((s != NATS_TIMEOUT) && !arg.done) | ||
s = natsCondition_TimedWait(arg.c, arg.m, 2000); | ||
// If ok, arg.current should be false since the subscription should | ||
// not be valid (closed) at the second callback iteration. | ||
if ((s == NATS_OK) && arg.current) | ||
s = NATS_ERR; | ||
natsMutex_Unlock(arg.m); | ||
testCond(s == NATS_OK); | ||
|
||
natsOptions_Destroy(opts); | ||
natsConnection_Destroy(nc); | ||
|
||
_destroyDefaultThreadArgs(&arg); | ||
|
||
_stopServer(serverPid); | ||
} | ||
|
||
static void | ||
_responseCb(natsConnection *nc, natsSubscription *sub, natsMsg *msg, void *closure) | ||
{ | ||
|
@@ -34440,6 +34564,7 @@ static testInfo allTests[] = | |
{"SyncSubscriptionPending", test_SyncSubscriptionPending}, | ||
{"SyncSubscriptionPendingDrain", test_SyncSubscriptionPendingDrain}, | ||
{"AsyncErrHandler", test_AsyncErrHandler}, | ||
{"AsyncErrHandlerSubDestroyed", test_AsyncErrHandlerSubDestroyed}, | ||
{"AsyncSubscriberStarvation", test_AsyncSubscriberStarvation}, | ||
{"AsyncSubscriberOnClose", test_AsyncSubscriberOnClose}, | ||
{"NextMsgCallOnAsyncSub", test_NextMsgCallOnAsyncSub}, | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍