Skip to content

Commit fc50801

Browse files
authored
[MOD-8115] Free spec resources in the main thread (#5324)
* free spec resources in the main thread. * improve cpp gc tests suite: allow multiple gc runs in each test move args to test suite members use pthread_join instead of pthread_detach loop to enable multiple gc interval taken from #5049
1 parent 8c5e190 commit fc50801

3 files changed

Lines changed: 22 additions & 19 deletions

File tree

tests/cpptests/common.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class MyEnvironment : public ::testing::Environment {
2626
const char *arguments[] = {"NOGC"};
2727
// No arguments..
2828
RMCK_Bootstrap(my_OnLoad, arguments, 1);
29+
RSGlobalConfig.freeResourcesThread = false;
2930
}
3031

3132
virtual void TearDown() {

tests/cpptests/index_utils.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ RefManager *createSpec(RedisModuleCtx *ctx) {
7070
}
7171

7272
void freeSpec(RefManager *ism) {
73-
int free_resources_config = RSGlobalConfig.freeResourcesThread;
74-
RSGlobalConfig.freeResourcesThread = false;
7573
IndexSpec_RemoveFromGlobals({ism});
76-
RSGlobalConfig.freeResourcesThread = free_resources_config;
7774
}
7875

7976
NumericRangeTree *getNumericTree(IndexSpec *spec, const char *field) {

tests/cpptests/test_cpp_forkgc.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,26 @@ static timespec getTimespecCb(void *) {
4747
typedef struct {
4848
void *fgc;
4949
RefManager *ism;
50+
volatile bool runGc;
5051
} args_t;
5152

52-
static pthread_t thread;
53-
5453
void *cbWrapper(void *args) {
5554
args_t *fgcArgs = (args_t *)args;
56-
ForkGC *fgc = reinterpret_cast<ForkGC *>(get_spec(fgcArgs->ism)->gc->gcCtx);
55+
GCContext *gc = get_spec(fgcArgs->ism)->gc;
56+
ForkGC *fgc = reinterpret_cast<ForkGC *>(gc->gcCtx);
5757

58-
// sync thread
59-
while (fgc->pauseState != FGC_PAUSED_CHILD) {
60-
usleep(500);
61-
}
58+
while (true) {
59+
// sync thread
60+
while (fgcArgs->runGc && fgc->pauseState != FGC_PAUSED_CHILD) {
61+
usleep(500);
62+
}
63+
if (!fgcArgs->runGc) {
64+
break;
65+
}
6266

63-
// run ForkGC
64-
get_spec(fgcArgs->ism)->gc->callbacks.periodicCallback(fgc);
65-
rm_free(args);
67+
// run ForkGC
68+
gc->callbacks.periodicCallback(fgc);
69+
}
6670
return NULL;
6771
}
6872

@@ -71,6 +75,8 @@ class FGCTest : public ::testing::Test {
7175
RMCK::Context ctx;
7276
RefManager *ism;
7377
ForkGC *fgc;
78+
args_t args;
79+
pthread_t thread;
7480

7581
void SetUp() override {
7682
ism = createSpec(ctx);
@@ -82,17 +88,16 @@ class FGCTest : public ::testing::Test {
8288
void runGcThread() {
8389
fgc = reinterpret_cast<ForkGC *>(get_spec(ism)->gc->gcCtx);
8490
thread = {0};
85-
args_t *args = (args_t *)rm_calloc(1, sizeof(*args));
86-
*args = {.fgc = fgc, .ism = ism};
91+
args = {.fgc = fgc, .ism = ism, .runGc = true};
8792

88-
pthread_create(&thread, NULL, cbWrapper, args);
93+
pthread_create(&thread, NULL, cbWrapper, &args);
8994
}
9095

9196
void TearDown() override {
97+
args.runGc = false;
98+
// wait for the gc thread to finish current loop and exit the thread
99+
pthread_join(thread, NULL);
92100
freeSpec(ism);
93-
// Detach from the gc to make sure we are not stuck on waiting
94-
// for the pauseState to be changed.
95-
pthread_detach(thread);
96101
}
97102

98103
size_t addDocumentWrapper(const char *docid, const char *field, const char *value) {

0 commit comments

Comments
 (0)