Skip to content

Fixes calling cleanup method when post/auxiliary modules fail#21036

Open
EclipseAditya wants to merge 1 commit into
rapid7:masterfrom
EclipseAditya:fix/18138-cleanup-called-twice
Open

Fixes calling cleanup method when post/auxiliary modules fail#21036
EclipseAditya wants to merge 1 commit into
rapid7:masterfrom
EclipseAditya:fix/18138-cleanup-called-twice

Conversation

@EclipseAditya
Copy link
Copy Markdown
Contributor

Fixes #18138

Post and Auxiliary modules were invoking mod.cleanup twice when exiting through error paths (fail_with, Timeout, Interrupt, or unhandled exceptions). Cleanup was first triggered inside the rescue blocks of job_run_proc and then executed again by job_cleanup_proc, which run_simple calls unconditionally.

Root cause: job_run_proc explicitly called mod.cleanup in each rescue clause, while run_simple always invoked job_cleanup_proc afterward, resulting in duplicate cleanup.

Resolution: Removed direct mod.cleanup calls from all rescue blocks in job_run_proc for Msf::Simple::Post and Msf::Simple::Auxiliary. The synchronous execution path is now wrapped in begin/ensure so job_cleanup_proc runs exactly once regardless of how execution exits. Applied the same fix in ExploitDriver, where an Interrupt rescue caused duplicate cleanup during multi-target runs.

Verification: Executed the updated test suite (24 examples), all passing:
bundle exec rspec spec/lib/msf/base/simple/

Comment thread lib/msf/base/simple/auxiliary.rb Outdated
Comment thread lib/msf/base/simple/auxiliary.rb
@EclipseAditya EclipseAditya force-pushed the fix/18138-cleanup-called-twice branch from 191b247 to c3d91a1 Compare March 2, 2026 13:32
@msutovsky-r7 msutovsky-r7 self-assigned this Mar 2, 2026
@msutovsky-r7 msutovsky-r7 changed the title Fix #18138: cleanup called twice when Post/Auxiliary modules fail Fixes calling cleanup method when post/auxiliary modules fail Apr 13, 2026
@github-project-automation github-project-automation Bot moved this from Todo to In Progress in Metasploit Kanban Apr 13, 2026
@msutovsky-r7 msutovsky-r7 added the rn-fix release notes fix label Apr 13, 2026
Copy link
Copy Markdown
Contributor

@msutovsky-r7 msutovsky-r7 left a comment

Choose a reason for hiding this comment

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

So the fix here seems odd - it does not take into account some categories of modules, where the bug persists and for some, it removes calling cleanup entirely when module throw some specific error(?). Not really sure, I'll investigate more here.

Copy link
Copy Markdown
Contributor

@msutovsky-r7 msutovsky-r7 left a comment

Choose a reason for hiding this comment

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

I think this issue requires little bit more testing and more time - I've opened new PR, but I would like to keep the specs from this PR.

@msutovsky-r7 msutovsky-r7 added the blocked Blocked by one or more additional tasks label Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked Blocked by one or more additional tasks bug rn-fix release notes fix

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Cleanup method is called twice when a Post module fails

3 participants