Skip to content

Commit 2174958

Browse files
Buristanigormunkin
authored andcommitted
core: fix resources leak in memory profiler
When memory profiler fails to start with PROFILE_ERRRUN status both stream and ctx are not released. At the same time when memory profiler fails to start with the PROFILE_ERRIO status both stream and ctx are released twice. Both cases occur due to invalid return status checking. To fix the leakage on_stop callback is called manually inside the profiler when error on start occurs. Checks in misc.memprof.start() are omitted. Follows up tarantool/tarantool#5442 Reviewed-by: Igor Munkin <[email protected]> Reviewed-by: Sergey Ostanevich <[email protected]> Signed-off-by: Igor Munkin <[email protected]>
1 parent 6d94424 commit 2174958

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/lib_misc.c

-4
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,6 @@ LJLIB_CF(misc_memprof_start)
177177
memprof_status = lj_memprof_start(L, &opt);
178178

179179
if (LJ_UNLIKELY(memprof_status != PROFILE_SUCCESS)) {
180-
if (memprof_status == PROFILE_ERRIO) {
181-
fclose(ctx->stream);
182-
lj_mem_free(ctx->g, ctx, sizeof(*ctx));
183-
}
184180
switch (memprof_status) {
185181
case PROFILE_ERRUSE:
186182
lua_pushnil(L);

src/lj_memprof.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,11 @@ int lj_memprof_start(struct lua_State *L, const struct lj_memprof_options *opt)
228228
lua_assert(opt->buf != NULL);
229229
lua_assert(opt->len != 0);
230230

231-
if (mp->state != MPS_IDLE)
231+
if (mp->state != MPS_IDLE) {
232+
/* Clean up resourses. Ignore possible errors. */
233+
opt->on_stop(opt->ctx, opt->buf);
232234
return PROFILE_ERRRUN;
235+
}
233236

234237
/* Discard possible old errno. */
235238
mp->saved_errno = 0;
@@ -331,7 +334,8 @@ int lj_memprof_stop(struct lua_State *L)
331334
int lj_memprof_start(struct lua_State *L, const struct lj_memprof_options *opt)
332335
{
333336
UNUSED(L);
334-
UNUSED(opt);
337+
/* Clean up resourses. Ignore possible errors. */
338+
opt->on_stop(opt->ctx, opt->buf);
335339
return PROFILE_ERRUSE;
336340
}
337341

0 commit comments

Comments
 (0)