@@ -30,10 +30,8 @@ void _SetTensorsInDagLocalContext(RedisAI_RunInfo *rinfo) {
3030 demangle the keys in order to persist them.*/
3131int _MangleTensorsNames (RedisModuleCtx * ctx , RedisAI_RunInfo * rinfo ) {
3232
33+ int res = REDISMODULE_ERR ;
3334 AI_dict * mangled_tensors = AI_dictCreate (& AI_dictTypeHeapRStrings , NULL );
34- if (!mangled_tensors ) {
35- return REDISMODULE_ERR ;
36- }
3735
3836 {
3937 AI_dictIterator * iter = AI_dictGetSafeIterator (rinfo -> dagTensorsContext );
@@ -61,9 +59,9 @@ int _MangleTensorsNames(RedisModuleCtx *ctx, RedisAI_RunInfo *rinfo) {
6159 RedisModuleString * key = currentOp -> inkeys [j ];
6260 AI_dictEntry * entry = AI_dictFind (mangled_tensors , key );
6361 if (!entry ) {
64- AI_dictRelease ( mangled_tensors );
62+ array_free ( mangled_inkeys );
6563 RedisModule_ReplyWithError (ctx , "ERR INPUT key cannot be found in DAG" );
66- return REDISMODULE_ERR ;
64+ goto cleanup ;
6765 }
6866 int * instance = AI_dictGetVal (entry );
6967 char buf [16 ];
@@ -94,8 +92,19 @@ int _MangleTensorsNames(RedisModuleCtx *ctx, RedisAI_RunInfo *rinfo) {
9492 mangled_outkeys = array_append (mangled_outkeys , mangled_key );
9593 }
9694
97- array_free (currentOp -> inkeys );
98- array_free (currentOp -> outkeys );
95+ if (currentOp -> inkeys ) {
96+ for (size_t j = 0 ; j < array_len (currentOp -> inkeys ); j ++ ) {
97+ RedisModule_FreeString (NULL , currentOp -> inkeys [j ]);
98+ }
99+ array_free (currentOp -> inkeys );
100+ }
101+
102+ if (currentOp -> outkeys ) {
103+ for (size_t j = 0 ; j < array_len (currentOp -> outkeys ); j ++ ) {
104+ RedisModule_FreeString (NULL , currentOp -> outkeys [j ]);
105+ }
106+ array_free (currentOp -> outkeys );
107+ }
99108
100109 currentOp -> inkeys = mangled_inkeys ;
101110 currentOp -> outkeys = mangled_outkeys ;
@@ -109,11 +118,10 @@ int _MangleTensorsNames(RedisModuleCtx *ctx, RedisAI_RunInfo *rinfo) {
109118 RedisModuleString * key = (RedisModuleString * )AI_dictGetKey (entry );
110119 AI_dictEntry * mangled_entry = AI_dictFind (mangled_tensors , key );
111120 if (!mangled_entry ) {
112- AI_dictRelease (mangled_tensors );
113121 AI_dictRelease (mangled_persisted );
114122 AI_dictReleaseIterator (iter );
115123 RedisModule_ReplyWithError (ctx , "ERR PERSIST key cannot be found in DAG" );
116- return REDISMODULE_ERR ;
124+ goto cleanup ;
117125 }
118126 int * instance = AI_dictGetVal (mangled_entry );
119127 char buf [16 ];
@@ -130,25 +138,25 @@ int _MangleTensorsNames(RedisModuleCtx *ctx, RedisAI_RunInfo *rinfo) {
130138 AI_dictRelease (rinfo -> dagTensorsPersistedContext );
131139 rinfo -> dagTensorsPersistedContext = mangled_persisted ;
132140
133- {
134- AI_dictIterator * iter = AI_dictGetSafeIterator (mangled_tensors );
135- AI_dictEntry * entry = AI_dictNext (iter );
136- while (entry ) {
137- int * val = (int * )AI_dictGetVal (entry );
138- RedisModule_Free (val );
139- entry = AI_dictNext (iter );
140- }
141- AI_dictReleaseIterator (iter );
142- }
143- AI_dictRelease (mangled_tensors );
144- mangled_tensors = NULL ;
145-
146141 for (long long i = 0 ; i < array_len (rinfo -> dagOps ); i ++ ) {
147142 if (rinfo -> dagOps [i ]-> devicestr == NULL ) {
148143 rinfo -> dagOps [i ]-> devicestr = "CPU" ;
149144 }
150145 }
151- return REDISMODULE_OK ;
146+ res = REDISMODULE_OK ;
147+
148+ cleanup : {
149+ AI_dictIterator * iter = AI_dictGetSafeIterator (mangled_tensors );
150+ AI_dictEntry * entry = AI_dictNext (iter );
151+ while (entry ) {
152+ int * val = (int * )AI_dictGetVal (entry );
153+ RedisModule_Free (val );
154+ entry = AI_dictNext (iter );
155+ }
156+ AI_dictReleaseIterator (iter );
157+ }
158+ AI_dictRelease (mangled_tensors );
159+ return res ;
152160}
153161
154162/**
@@ -277,19 +285,18 @@ static int _parseTimeout(RedisModuleCtx *ctx, RedisModuleString **argv, int argc
277285static RAI_DagOp * _AddEmptyOp (RedisAI_RunInfo * rinfo ) {
278286 RAI_DagOp * currentDagOp ;
279287 RAI_InitDagOp (& currentDagOp );
280- currentDagOp -> argv = (RedisModuleString * * )array_new (RedisModuleString * , 1 );
281288 rinfo -> dagOps = array_append (rinfo -> dagOps , currentDagOp );
282289 return currentDagOp ;
283290}
284291
285292// Go over the args and save them in the current op until we see another "|>" or finish.
286293int _CollectOpArgs (RedisModuleString * * argv , int argc , int arg_pos , RAI_DagOp * op ) {
287294
295+ op -> argv = & argv [arg_pos ];
288296 while (arg_pos < argc ) {
289297 const char * arg_string = RedisModule_StringPtrLen (argv [arg_pos ], NULL );
290298 if (!strcasecmp (arg_string , "|>" ))
291299 return op -> argc ;
292- op -> argv = array_append (op -> argv , argv [arg_pos ]);
293300 op -> argc ++ ;
294301 arg_pos ++ ;
295302 }
@@ -413,7 +420,8 @@ int ParseDAGRunCommand(RedisAI_RunInfo *rinfo, RedisModuleCtx *ctx, RedisModuleS
413420 goto cleanup ;
414421 }
415422 rinfo -> dagOpCount = array_len (rinfo -> dagOps );
416- if (rinfo -> dagOpCount < 1 ) goto cleanup ;
423+ if (rinfo -> dagOpCount < 1 )
424+ goto cleanup ;
417425 if (_ParseDAGOps (ctx , rinfo ) != REDISMODULE_OK )
418426 goto cleanup ;
419427 if (_MangleTensorsNames (ctx , rinfo ) != REDISMODULE_OK )
0 commit comments