WSL1: exiting process hangs forever in LXCORE!LxpInotifyNtWatchDecrementWatchCount (DrvFs inotify); `wsl --shutdown` hangs, only a reboot recovers
Windows Version
Microsoft Windows 11 Enterprise 24H2, 10.0.26100.9448
WSL Version
2.7.14.0 (WSL1 distro; lxcore.sys 10.0.26100.9278, uname -r = 4.4.0-26100-Microsoft #9278)
Are you using WSL 1 or WSL 2?
- WSL 2
- WSL 1
Kernel Version
4.4.0-26100-Microsoft
Distro Version
Ubuntu 22.04 (custom image, imported with wsl --import ... --version 1)
Other Software
- VS Code with the WSL extension 0.104.3. The VS Code server's file watcher (node, inotify) watches a large source tree on
/mnt/c. - Parallel C/C++ builds (
make -j) in the same WSL1 distro, on the same tree. - Endpoint security: FortiClient minifilters (FortiShield, fortimon3, FortiAptFilter). Defender real-time protection is off.
- CPU: Intel i7-12700K (20 logical processors). The problem appears much more often on many-core machines.
Repro Steps
This is not yet reduced to a minimal repro. It happens reliably enough in this setup:
- Keep a large source tree on the Windows file system and use it from a WSL1 distro through
/mnt/c(DrvFs). - Connect VS Code to the WSL1 distro. Its file watcher adds inotify watches recursively over the tree.
- Run a parallel build in WSL1 that deletes and regenerates a directory tree the watcher is watching (a generated-code folder).
- Close the VS Code window, or reload it. The VS Code server stops its file-watcher process.
Expected Behavior
The file-watcher process exits, and its inotify watches are removed.
Actual Behavior
- Linux side: the watcher process never finishes exiting. It stays in state
Zwith PPID 1 andFDSize: 0(all fds closed), but its memory is still mapped (VmSize about 1.6 GB, RSS about 51 MB). init cannot reap it. - Windows side: the pico process has one thread left, in
Wait:Executive, KernelMode, non-alertable. - Shutdown:
wsl --shutdownandwsl --terminatehang forever. Killing or restarting WSLService does not help. Only a Windows reboot recovers. - Rest of the instance: new sessions (
wsl -d <distro> -e ...) keep working until the reboot.
Diagnostic Logs
I took a live kernel memory dump while the instance was wedged (Task Manager, "Create live kernel memory dump file") and analysed it with public symbols. It is available privately on request, since it contains kernel memory. I can also capture collect-wsl-logs.ps1 (see CONTRIBUTING.md) during a reproduction. A reproduction needs a reboot afterwards.
Stuck thread (the last thread of the exiting process; wait started about 34 min before the dump):
THREAD ffff96804a0af080 Cid 5c80.8e10 WAIT: (Executive) KernelMode Non-Alertable
ffff968011ad4f60 NotificationEvent <- 0x30-byte 'LxIn' pool block
nt!KiSwapContext+0x76
nt!KiSwapThread+0x248
nt!KiCommitThreadWait+0x225
nt!KeWaitForSingleObject+0x846
LXCORE!LxpInotifyNtWatchDecrementWatchCount+0x73
LXCORE!LxpDrvFsInodeInotifyNtWatchDecrementWatchCount+0x10
LXCORE!LxpInotifyRemoveNtWatch+0x7e
LXCORE!LxpInotifyRemoveWatchHelper+0xe9
LXCORE!LxpInotifyFileDelete+0x47
LXCORE!VfsFileObjectLastDereference+0x64
LXCORE!VfsFileLifetimeDereference+0x2b
LXCORE!LxpFileDescriptorCloseInternal+0xbb
LXCORE!LxpFileDescriptorTableClean+0x5a
LXCORE!LxpFileDescriptorTableDereference+0x2b
LXCORE!LxpThreadCleanup+0x21
LXCORE!LxpThreadEnd+0x72
LXCORE!LxpThreadSignalApc+0xd3
LXCORE!LxpNtStateThreadApcUser+0x4e
nt!KiDeliverApc+0x748
nt!KiInitiateUserApc+0x70
nt!KiSystemServiceExitPico+0x54What LxpInotifyNtWatchDecrementWatchCount does, from the disassembly of lxcore 10.0.26100.9278:
VfsLockAcquireExclusive(inodeWatch) ; still held while waiting
if (--inodeWatch->Count == 0) {
VfsLockAcquireExclusive(ntWatch)
ZwClose(ntWatch->DirectoryHandle); ntWatch->DirectoryHandle = NULL
VfsLockReleaseExclusive(ntWatch)
KeWaitForSingleObject(ntWatch->Event, Executive, KernelMode, FALSE, NULL) ; <-- stuck here, no timeout
...free...
}
VfsLockReleaseExclusive(inodeWatch)State of the objects at the time of the dump:
- inode watch
ffffd48923a49c68: lock = 1 (held by the stuck thread), count = 0. - NT watch
ffffd48894cf8b90('LxIn', paged pool):- lock = 0;
- DirectoryHandle = 0 (already closed);
- Event =
ffff968011ad4f60, not signalled; - IoStatusBlock = {STATUS_SUCCESS, 0x1a}, the last completion;
- buffer length 0x600, CompletionFilter 0x17f.
- Watched directory: a build-generated directory under
/mnt/cthat the build deletes and regenerates.
The only code that sets that event is the inotify completion thread, and it is idle:
THREAD ffff968004ef7080 Cid 0004.27a4 WAIT: (WrQueue) KernelMode Non-Alertable
Win32 Start Address LXCORE!LxpInotifyProcessNtWatchWorker
nt!KeRemoveQueueEx+0x896
nt!IoRemoveIoCompletion+0x92
nt!NtRemoveIoCompletion+0x12f
LXCORE!LxpInotifyProcessNtWatchWorker+0x52Its wait began about 4 seconds after the stuck thread's wait began.
How the directory watch is set up and completed:
- Issued with no APC:
ZwNotifyChangeDirectoryFile(handle, NULL, NULL, ntWatch, &ntWatch->Iosb, ...)is issued on a handle associated with the completion port created inLxpInotifyNtThreadIncrementUserCount. - Completion thread loop:
LxpInotifyProcessNtWatchWorkersetsntWatch->Eventon STATUS_NOTIFY_CLEANUP, STATUS_DELETE_PENDING and other errors. It also sets it when, at re-arm time, it finds the handle already closed. - Conclusion: no completion packet for this NT watch ever reached the port after the
ZwClose.
Most likely cause (not proven):
- In
LxpInotifyProcessNtWatchWorker, the re-arm callZwNotifyChangeDirectoryFilecan fail synchronously. An example is STATUS_DELETE_PENDING while the build deletes the directory. - That failure is only traced (
LxpTraceLoggingBreakPoint). Nothing marks the watch as dead or sets its event. - A synchronous error on a completion-port handle queues no packet, so no request is left outstanding.
- When the watch is later removed,
ZwCloseproduces no completion, andKeWaitForSingleObjectwaits forever. It does so while holding the inode-watch lock, in an unkillable, non-alertable wait.
The less likely alternative is that the directory-watch IRP was pending and never completed on cleanup by the file-system or filter stack. I could not check for an outstanding IRP: scanning the pool of the dump (!irpfind) made the analysing PC hang.
Suggested fix:
- If the re-arm fails synchronously, treat the NT watch as terminated: set its event, or record that no request is outstanding so the close path does not wait.
- Do not wait unbounded and non-alertable, with a lock held, in the exit path of a process.
Related issues
- #12639: same symptom (WSL1 and VS Code; an unkillable zombie
nodewhose parent is init;wsl --shutdownhangs). Closed without a fix. - #41148: a pico process with one thread in Wait/Executive, and WslService stuck until reboot. There the cause was a file lock that endpoint security did not release (NTFS event 152). No event 152 on this machine.
- #41646: a WSL1 bugcheck in
LXCORE!LxpNtStateThreadQueueApc, triggered by the VS Code server, on the same lxcore build.
Source: microsoft/WSL