Skip to content

Commit d0f3d72

Browse files
committed
Fix memory management in local context dict
1 parent 68f7aac commit d0f3d72

File tree

2 files changed

+3
-34
lines changed

2 files changed

+3
-34
lines changed

src/dag.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void *RedisAI_DagRunSession(RedisAI_RunInfo *rinfo) {
8484
const char *key_string = RedisModule_StringPtrLen(
8585
currentOp->outkeys[outputNumber], NULL);
8686
const char *dictKey = RedisModule_Strdup(key_string);
87-
AI_dictReplace(rinfo->dagTensorsContext, (void*)dictKey, tensor);
87+
AI_dictReplace(rinfo->dagTensorsContext, (void*)dictKey, RAI_TensorGetShallowCopy(tensor));
8888
} else {
8989
RAI_SetError(currentOp->err, RAI_EMODELRUN,
9090
"ERR output tensor on DAG's MODELRUN was null");
@@ -126,7 +126,7 @@ void *RedisAI_DagRunSession(RedisAI_RunInfo *rinfo) {
126126
const char *key_string = RedisModule_StringPtrLen(
127127
currentOp->outkeys[outputNumber], NULL);
128128
const char *dictKey = RedisModule_Strdup(key_string);
129-
AI_dictReplace(rinfo->dagTensorsContext, (void*)dictKey, tensor);
129+
AI_dictReplace(rinfo->dagTensorsContext, (void*)dictKey, RAI_TensorGetShallowCopy(tensor));
130130
} else {
131131
RAI_SetError(currentOp->err, RAI_EMODELRUN,
132132
"ERR output tensor on DAG's SCRIPTRUN was null");
@@ -329,7 +329,7 @@ int RAI_parseDAGLoadArgs(RedisModuleCtx *ctx, RedisModuleString **argv,
329329
}
330330
RedisModule_CloseKey(key);
331331
const char *dictKey = RedisModule_Strdup(arg_string);
332-
AI_dictAdd(*localContextDict, (void*)dictKey, t);
332+
AI_dictAdd(*localContextDict, (void*)dictKey, RAI_TensorGetShallowCopy(t));
333333
const char *keyspacePersistKey = RedisModule_Strdup(dictKey);
334334
AI_dictAdd(*loadedContextDict, (void*)keyspacePersistKey, (void *)1);
335335
number_loaded_keys++;

src/run_info.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -177,37 +177,6 @@ void RAI_FreeRunInfo(RedisModuleCtx *ctx, struct RedisAI_RunInfo *rinfo) {
177177
RAI_FreeError(rinfo->err);
178178

179179
if (rinfo->dagTensorsContext) {
180-
AI_dictIterator *iter = AI_dictGetSafeIterator(rinfo->dagTensorsContext);
181-
AI_dictEntry *entry = AI_dictNext(iter);
182-
RAI_Tensor *tensor = NULL;
183-
184-
while (entry) {
185-
tensor = AI_dictGetVal(entry);
186-
char *key = (char *)AI_dictGetKey(entry);
187-
188-
if (tensor && key != NULL) {
189-
// if the key is persistent then we should not delete it
190-
AI_dictEntry *persistent_entry =
191-
AI_dictFind(rinfo->dagTensorsPersistentContext, key);
192-
// if the key was loaded from the keyspace then we should not delete it
193-
AI_dictEntry *loaded_entry =
194-
AI_dictFind(rinfo->dagTensorsLoadedContext, key);
195-
196-
if (persistent_entry == NULL && loaded_entry == NULL) {
197-
AI_dictDelete(rinfo->dagTensorsContext, key);
198-
}
199-
200-
if (persistent_entry) {
201-
AI_dictDelete(rinfo->dagTensorsPersistentContext, key);
202-
}
203-
if (loaded_entry) {
204-
AI_dictDelete(rinfo->dagTensorsLoadedContext, key);
205-
}
206-
}
207-
entry = AI_dictNext(iter);
208-
}
209-
AI_dictReleaseIterator(iter);
210-
211180
AI_dictRelease(rinfo->dagTensorsContext);
212181
AI_dictRelease(rinfo->dagTensorsLoadedContext);
213182
AI_dictRelease(rinfo->dagTensorsPersistentContext);

0 commit comments

Comments
 (0)