Skip to content

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

Closed

Description

Windows Version

Microsoft Windows [Version 10.0.26200.7462]

WSL Version

WSL version: 2.6.2.0

Are you using WSL 1 or WSL 2?

  • WSL 2
  • WSL 1

Kernel Version

6.6.87.2-microsoft-standard-WSL2

Distro Version

Ubuntu 24.04

Other Software

No response

Repro Steps

  1. Install two distros with systemd. For example Ubuntu 24.04 and Ubuntu 22.04
  2. Run both of them in separate terminals
  3. terminate one of those in a third terminal (wsl.exe -t "Ubuntu 22.04")
  4. Observe ls /proc/sys/fs/binfmt_misc in the one left open

Expected Behavior

In the distro that is left open, the registered binfmt handlers aren't cleared

Actual Behavior

All binfmt handlers in the non-terminated distro are gone. Including WSLInterop. As if the binfmt handlers were shared somehow.

Diagnostic Logs

No response

Activity

  1. github-actions commented on Dec 12, 2025

    @github-actions
    Contributor

    Logs are required for review from WSL team

    If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'.
    Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

    How to collect WSL logs

    Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

    Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
    Set-ExecutionPolicy Bypass -Scope Process -Force
    .\collect-wsl-logs.ps1
    

    The script will output the path of the log file once done.

    If this is a networking issue, please use collect-networking-logs.ps1, following the instructions in Collect WSL logs for networking issues

    Once completed please upload the output files to this GitHub issue.

    See Collect WSL logs (recommended method).

    If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.

  2. MiKom commented on Dec 12, 2025

    @MiKom
    Author

    /emailed-logs

  3. github-actions commented on Dec 12, 2025

    @github-actions
    Contributor
    Diagnostic information
    Found '/emailed-logs', adding tag 'emailed-logs'
    
  4. added
    emailed-logs
    on Dec 12, 2025
  5. MiKom commented on Dec 28, 2025

    @MiKom
    Author

    Pinging to make sure the bot doesn't close it. Issue still persists

  6. MiKom commented on Jan 2, 2026

    @MiKom
    Author

    Pinging again to fend off the bot

  7. J0F3 commented on Feb 11, 2026

    @J0F3

    I have the same issue multiple times per day, and it is very annoying.

  8. philippe-granet commented on Feb 11, 2026

    @philippe-granet

    The only reliable workaround I’ve found so far is this one: #8843 (comment)

  9. J0F3 commented on Feb 12, 2026

    @J0F3

    Ah, that's a good hint. Thank you Philippe GRANET (@philippe-granet)

    Regarding to #8843 (comment) it should be fixed now. But it is clearly not or at least not always. Because what i noticed on my Ubuntu 22.04 is that the systemd-binfmt.service has an additional wsl.conf which prevent it to actually run and so the fix from #8843 seems to not have the desired effect.

    systemd-binfmt.service - Set Up Additional Binary Formats
         Loaded: loaded (/lib/systemd/system/systemd-binfmt.service; static)
        Drop-In: /run/systemd/generator/systemd-binfmt.service.d
                 └─override.conf
                 /usr/lib/systemd/system/systemd-binfmt.service.d
                 └─wsl.conf
         Active: inactive (dead)
      Condition: start condition failed at Thu 2026-02-12 10:52:37 CET; 3min 10s ago
                 └─ ConditionVirtualization=!wsl was not met
    

    S, it seems that ExecStop=/lib/systemd/systemd-binfmt --unregister is still executed on stop. At least I can reproduce it by running sudo systemctl stop systemd-binfmt.service or by terminating the WSL Distor with wsl.exe -t. Afterward /proc/sys/fs/binfmt_misc/ is basically empty (also in all other still running Distros).

    On Ubuntu 24.04 I cannot reproduce it but there is only the override.conf from the fix and not the wsl.conf.

    systemd-binfmt.service - Set Up Additional Binary Formats
         Loaded: loaded (/usr/lib/systemd/system/systemd-binfmt.service; static)
        Drop-In: /run/systemd/generator/systemd-binfmt.service.d
                 └─override.conf
    

    Furthermore, at least in my case, the WSL integration of Docker Desktop seems also to have some influence. At least it also modifies the entries of /proc/sys/fs/binfmt_misc/.

    Before Docker start:

    drwxr-xr-x 2 root root 0 Feb 12 11:04 ./
    dr-xr-xr-x 1 root root 0 Feb 12 11:10 ../
    -rw-r--r-- 1 root root 0 Feb 12 11:10 WSLInterop
    -rw-r--r-- 1 root root 0 Feb 12 11:10 python3.10
    --w------- 1 root root 0 Feb 12 11:10 register
    -rw-r--r-- 1 root root 0 Feb 11 20:05 status
    

    After Docker started:

    drwxr-xr-x 2 root root 0 Feb 12 11:04 ./
    dr-xr-xr-x 1 root root 0 Feb 12 11:10 ../
    -rw-r--r-- 1 root root 0 Feb 12 11:10 WSLInterop
    -rw-r--r-- 1 root root 0 Feb 12 11:12 aarch64
    -rw-r--r-- 1 root root 0 Feb 12 11:12 arm
    -rw-r--r-- 1 root root 0 Feb 12 11:12 mips64
    -rw-r--r-- 1 root root 0 Feb 12 11:12 mips64le
    -rw-r--r-- 1 root root 0 Feb 12 11:12 ppc64le
    -rw-r--r-- 1 root root 0 Feb 12 11:10 python3.10
    --w------- 1 root root 0 Feb 12 11:12 register
    -rw-r--r-- 1 root root 0 Feb 12 11:12 riscv64
    -rw-r--r-- 1 root root 0 Feb 12 11:12 s390x
    -rw-r--r-- 1 root root 0 Feb 11 20:05 status
    

    And when stopped again is goes back to the list of before and sometimes WSLInterops is also to gone (but not always). But I did not find out what (in Docker Desktop) is exactly responsible for that.

  10. puetzk commented on Mar 9, 2026

    @puetzk

    They replaced #8843 (comment) with yet another workaround that is fighting with systemd rather than working with it:

    Now,

    WSL/src/linux/init/init.cpp

    Lines 369 to 396 in 218b71b

    auto systemdBinfmtContent = std::format(
    R"(# Note: This file is generated by WSL to prevent binfmt.d from overriding WSL's binfmt interpreter.
    # To disable this unit, add the following to /etc/wsl.conf:
    # [boot]
    # protectBinfmt=false
    [Service]
    ExecStop=
    ExecStart=/bin/sh -c '(echo -1 > {}/{}) ; (echo "{}" > {})' )",
    BINFMT_MISC_MOUNT_TARGET,
    LX_INIT_BINFMT_NAME,
    BINFMT_INTEROP_REGISTRATION_STRING(LX_INIT_BINFMT_NAME),
    BINFMT_MISC_REGISTER_FILE);
    // Install the override for systemd-binfmt.service.
    {
    auto overrideFolder = std::format("{}/systemd-binfmt.service.d", installPath);
    THROW_LAST_ERROR_IF(UtilMkdirPath(overrideFolder.c_str(), 0755) < 0);
    THROW_LAST_ERROR_IF(WriteToFile(std::format("{}/override.conf", overrideFolder).c_str(), systemdBinfmtContent.c_str()) < 0);
    }
    // Install the override for binfmt-support.service.
    {
    auto overrideFolder = std::format("{}/binfmt-support.service.d", installPath);
    THROW_LAST_ERROR_IF(UtilMkdirPath(overrideFolder.c_str(), 0755) < 0);
    THROW_LAST_ERROR_IF(WriteToFile(std::format("{}/override.conf", overrideFolder).c_str(), systemdBinfmtContent.c_str()) < 0);
    }
    }
    is creating an override /run/systemd/generator/systemd-binfmt.service.d/override.conf

    # Note: This file is generated by WSL to prevent binfmt.d from overriding WSL's binfmt interpreter.
    # To disable this unit, add the following to /etc/wsl.conf:
    # [boot]
    # protectBinfmt=false
    
    [Service]
    ExecStop=
    ExecStart=/bin/sh -c '(echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop) ; (echo ":WSLInterop:M::MZ::/init:P" > /proc/sys/fs/binfmt_misc/register)'
    

    Which serves to break systemd-binfmt.service, reventing the real service from starting and replacing it with a one-time shell script that registers WSLInterop (and only WSLInterop).

    What they should be doing is writing /run/binfmt.d/WSLInterop.conf

    :WSLInterop:M::MZ::/init:P
    

    and then just letting systemd-binfmt.conf register that, along with any other requested interpreters.

  11. Tycorc commented on Mar 12, 2026

    @Tycorc

    Confirming on WSL 2.6.3.0 / kernel 6.6.87.2 / Windows 10.0.26200.8037 / Ubuntu 24.04.4 LTS.

    Building on Jonas Feller (@J0F3)'s ExecStop analysis and Kevin Puetz (@puetzk) s protectBinfmt override analysis — the override successfully prevents systemd-binfmt --unregister, but the handlers still disappear because systemd's late shutdown unmounts the binfmt_misc filesystem itself.

    Evidence

    Shutdown monitor service (Before=shutdown.target) planted inside the test distro captured state during shutdown:

    binfmt status: enabled
    WSLInterop: enabled, interpreter /init, flags: P
    mount: binfmt_misc on /proc/sys/fs/binfmt_misc type binfmt_misc (rw,relatime)
    

    Handler present when services stop. After wsl --terminate completes, from the host distro:

    status: enabled
    mountinfo: 119 112 0:33 / /proc/sys/fs/binfmt_misc rw,relatime shared:22 - binfmt_misc binfmt_misc rw
    handlers:
    (empty)
    

    binfmt_misc still mounted, but all handlers gone — WSLInterop, python3.12, everything. The handlers are cleared during systemd-shutdown's final unmount phase, which protectBinfmt can't prevent because it only overrides systemd-binfmt.service.

    State progression (from host, observed via snapshots)

    baseline (after wsl --shutdown):  WSLInterop flags: PF
    after test distro boot:           WSLInterop flags: P, python3.12 added
    after wsl --terminate:            (all handlers gone)
    

    The protectBinfmt override's ExecStart changes flags from PF to P on boot (deletes handler, re-registers with :P). This part works. The problem is the terminate.

    Confirmed mitigation

    Setting [boot] systemd=false in the distro's wsl.conf prevents the handler loss on terminate.

    Note on the manual workaround

    echo :WSLInterop:M::MZ::/init:PF > register uses :PF flags but the override expects :P. Next systemd boot's ExecStart fails with Permission denied on re-register. wsl --shutdown is the only safe recovery.

    Agree with Kevin Puetz (@puetzk) that writing /run/binfmt.d/WSLInterop.conf and letting systemd-binfmt handle it properly would be cleaner, but the filesystem unmount during late shutdown still needs addressing — via mount propagation, a mount unit override, or binfmt_misc namespace support from kernel 6.7(

  12. added a commit that references this issue on Mar 16, 2026
    6042cde
  13. benhillis commented on May 20, 2026

    @benhillis
    Member

    Gordon Lam (@yeelam-gordon) — I dug into this issue and found your branch workitem/13885-fix-binfmt-config-cleared-on-termination already has solid fixes for the multi-distro / F-flag-loss / stale-config scenarios. It would be great to get a PR opened from it.

    After reviewing the branch I had a few small suggestions (happy to drop them as review comments once a PR is open):

    1. Race tolerance in ExecStartPost — if another distro registers WSLInterop concurrently, the echo -1 > .../WSLInterop or echo ... > .../register can fail and mark systemd-binfmt.service as failed. Suggest swallowing those errors (2>/dev/null || true) and instead verifying the result with a grep on the live entry, e.g.:

      /bin/sh -c '(echo -1 > /proc/sys/fs/binfmt_misc/WSLInterop 2>/dev/null || true) ; \
                  (echo ":WSLInterop:M::MZ::/init:FP" > /proc/sys/fs/binfmt_misc/register 2>/dev/null || true) ; \
                  grep -q "^interpreter /init$" /proc/sys/fs/binfmt_misc/WSLInterop && \
                  grep -q "^flags:.*F" /proc/sys/fs/binfmt_misc/WSLInterop'
      
    2. Drop the unused [Mount] Options= block in the proc-sys-fs-binfmt_misc.mount.d override — the unmount-prevention comes from DefaultDependencies=no + Before=umount.target, the Options field isn't doing anything.

    3. BinfmtSurvivesDistroTermination test:

      • VERIFY_ARE_EQUAL the return code of the second distro's wsl.conf write so a setup failure isn't silently swallowed.
      • Wait for systemd to actually reach running/degraded in both distros before terminating, otherwise the shutdown path isn't really being exercised.
      • Assert the live kernel entry retains the F flag (grep ^flags:.*F /proc/sys/fs/binfmt_misc/WSLInterop), not just that it's enabled — losing F is the core symptom for Docker / chroot interop and is what regressions would look like.

    Repro I was originally chasing: customer hits exec format error running Windows binaries from a WSL2 distro that has other distros also using systemd. Your fix covers it.

    Closed my draft (#40610) since it was just your branch + the above polish — better to land it from your branch with the credit.

  14. added a commit that references this issue on May 21, 2026
    5ee1442
  15. benhillis commented on Jun 5, 2026

    @benhillis
    Member

    This should be fixed by #40621 (merged in WSL 2.7.6+). The init now protects binfmt_misc registrations from being wiped when another distro with systemd is terminated. Please update to the latest WSL and let us know if the issue persists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    emailed-logs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions