Skip to content

fix(init): prevent binfmt_misc handler loss on distro termination#14443

Draft
yeelam-gordon wants to merge 1 commit intomicrosoft:masterfrom
yeelam-gordon:workitem/13885-fix-binfmt-config-cleared-on-termination
Draft

fix(init): prevent binfmt_misc handler loss on distro termination#14443
yeelam-gordon wants to merge 1 commit intomicrosoft:masterfrom
yeelam-gordon:workitem/13885-fix-binfmt-config-cleared-on-termination

Conversation

@yeelam-gordon
Copy link

Summary of the Pull Request

Fixes the issue where terminating a WSL2 distro with systemd clears binfmt_misc handlers across all running distros, breaking Windows interop. Changes:

  • Add mount unit override to prevent binfmt_misc unmount during late shutdown
  • Fix FP vs P flag inconsistency in WSLInterop registration (\�infmt.h\)
  • Use declarative \/run/binfmt.d/\ approach for handler persistence
  • Ensure \protectBinfmt=false\ in wsl.conf still works

PR Checklist

Validation Steps Performed

  • CMake configure + build of init and wsltests targets succeeded
  • Unit tests verify generated binfmt files and overrides
  • protectBinfmt=false path preserved

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

Fixes the issue where terminating a WSL2 distro with systemd clears
binfmt_misc handlers across all running distros. Changes:
- Add mount unit override to prevent binfmt_misc unmount during shutdown
- Fix FP vs P flag inconsistency in WSLInterop registration
- Use declarative /run/binfmt.d/ approach for handler persistence

Fixes microsoft#13885

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 16, 2026 10:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a WSL2/systemd shutdown interaction where terminating one distro could clear binfmt_misc handlers (including WSLInterop) for other running distros, breaking Windows interop.

Changes:

  • Generate a declarative systemd-binfmt config in /run/binfmt.d/WSLInterop.conf for persistent WSLInterop registration.
  • Add systemd generator drop-in overrides to prevent systemd-binfmt stop actions and keep binfmt_misc mounted during shutdown.
  • Align the WSLInterop binfmt registration string to consistently use :FP.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
test/windows/UnitTests.cpp Adds assertions that the generated /run/binfmt.d config and systemd generator overrides exist (and stay consistent across restarts/reloads), and that protectBinfmt=false disables generation.
src/linux/init/main.cpp Switches BINFMT_REGISTER_STRING to use the shared registration macro (now consistent with :FP).
src/linux/init/init.cpp Implements generation of /run/binfmt.d/WSLInterop.conf and generator drop-ins for systemd-binfmt and the binfmt_misc mount unit when protectBinfmt is enabled.
src/linux/init/binfmt.h Updates the shared binfmt registration macro to use :FP (fixing prior inconsistency).

You can also share your feedback on Copilot code review. Take the survey.

#define BINFMT_MISC_MOUNT_TARGET "/proc/sys/fs/binfmt_misc"
#define BINFMT_MISC_REGISTER_FILE BINFMT_MISC_MOUNT_TARGET "/register"
#define BINFMT_INTEROP_REGISTRATION_STRING(Name) ":" Name ":M::MZ::" LX_INIT_PATH ":P"
#define BINFMT_INTEROP_REGISTRATION_STRING(Name) ":" Name ":M::MZ::" LX_INIT_PATH ":FP"
Copy link
Member

Choose a reason for hiding this comment

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

This will break WSL1 which does not support the 'F' flag.

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.

binfmt config cleared on all distro when distro with systemd is terminated

3 participants