Skip to content

Commit 023ca42

Browse files
committed
util/async: print leaked BH name when AioContext finalizes
BHs must be deleted before the AioContext is finalized. If not, it's a bug and probably indicates that some part of the program still expects the BH to run in the future. That can lead to memory leaks, inconsistent state, or just hangs. Unfortunately the assert(flags & BH_DELETED) call in aio_ctx_finalize() is difficult to debug because the assertion failure contains no information about the BH! Use the QEMUBH name field added in the previous patch to show a useful error when a leaked BH is detected. Suggested-by: Eric Ernst <[email protected]> Signed-off-by: Stefan Hajnoczi <[email protected]> Message-Id: <[email protected]>
1 parent 0f08586 commit 023ca42

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

util/async.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,20 @@ aio_ctx_finalize(GSource *source)
344344
assert(QSIMPLEQ_EMPTY(&ctx->bh_slice_list));
345345

346346
while ((bh = aio_bh_dequeue(&ctx->bh_list, &flags))) {
347-
/* qemu_bh_delete() must have been called on BHs in this AioContext */
348-
assert(flags & BH_DELETED);
347+
/*
348+
* qemu_bh_delete() must have been called on BHs in this AioContext. In
349+
* many cases memory leaks, hangs, or inconsistent state occur when a
350+
* BH is leaked because something still expects it to run.
351+
*
352+
* If you hit this, fix the lifecycle of the BH so that
353+
* qemu_bh_delete() and any associated cleanup is called before the
354+
* AioContext is finalized.
355+
*/
356+
if (unlikely(!(flags & BH_DELETED))) {
357+
fprintf(stderr, "%s: BH '%s' leaked, aborting...\n",
358+
__func__, bh->name);
359+
abort();
360+
}
349361

350362
g_free(bh);
351363
}

0 commit comments

Comments
 (0)