Skip to content

Conversation

@giuseppe
Copy link
Member

@giuseppe giuseppe commented Dec 17, 2025

Summary by Sourcery

Ensure libkrun contexts and associated handles are properly cleaned up on errors and unload.

Bug Fixes:

  • Prevent handle leaks by centralizing error cleanup in libkrun_load and closing all opened handles on failure.
  • Close sev and nitro libkrun handles in libkrun_unload to avoid leaking library handles and memory.

Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
@sourcery-ai
Copy link

sourcery-ai bot commented Dec 17, 2025

Reviewer's Guide

Adjusts krun context creation and teardown logic to ensure dynamic library handles are correctly closed both on error paths in libkrun_load and during normal unloading in libkrun_unload, preventing handle leaks.

File-Level Changes

Change Details Files
Add centralized error path in libkrun_load to close any opened libkrun handles before returning on context creation failure.
  • Replace early returns on libkrun_create_context failure with a jump to a shared error label
  • Introduce an error label that conditionally dlclose each non-null handle (handle, handle_sev, handle_nitro) before freeing kconf
  • Ensure the function returns the last error code stored in ret after cleanup
src/libcrun/handlers/krun.c
Extend libkrun_unload to properly close all libkrun handles and free configuration.
  • Add dlclose calls for handle_sev and handle_nitro mirroring the existing handle cleanup pattern
  • Propagate dlclose failures for the new handles using crun_make_error with specific error messages
  • Free kconf after all handles have been successfully closed
src/libcrun/handlers/krun.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes - here's some feedback:

  • The libkrun_load error path now mirrors parts of libkrun_unload; consider factoring the common handle cleanup (dlclose/free) into a shared helper to avoid duplication and keep the unload behavior consistent across normal and error paths.
  • In libkrun_unload, the three dlclose blocks are nearly identical aside from the handle and error message; consolidating them (e.g., via a small helper or macro) would reduce repetition and make it easier to keep error handling for all handles in sync.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The `libkrun_load` error path now mirrors parts of `libkrun_unload`; consider factoring the common handle cleanup (`dlclose`/`free`) into a shared helper to avoid duplication and keep the unload behavior consistent across normal and error paths.
- In `libkrun_unload`, the three `dlclose` blocks are nearly identical aside from the handle and error message; consolidating them (e.g., via a small helper or macro) would reduce repetition and make it easier to keep error handling for all handles in sync.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@packit-as-a-service
Copy link

TMT tests failed. @containers/packit-build please check.

Copy link
Collaborator

@flouthoc flouthoc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@flouthoc
Copy link
Collaborator

flouthoc commented Jan 3, 2026

@slp PTAL

@slp
Copy link
Contributor

slp commented Jan 7, 2026

LGTM, thanks!

@giuseppe giuseppe merged commit e0a6f43 into containers:main Jan 7, 2026
41 of 44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants