-
Notifications
You must be signed in to change notification settings - Fork 8.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Windows Terminal leaks a file handle to the starting directory #10849
Comments
It's still the current working directory of windowsterminal.exe and openconsole.exe. |
Possibly, so? |
A process always has an open handle to its current working directory. |
And the current working directory stays the same forever because...? |
A process keeps its working directory open without shared delete access, which prevents the directory from being deleted or renamed until the directory is closed (e.g. the working directory changes or the process terminates). Notably, the base API uses the handle for the working directory to open relative file paths (see NTAPI
Because "WindowsTerminal.exe" and "OpenConsole.exe" never change their inherited working directory? I guess they could change it to the system directory. |
I think there are situations in which windowsterminal.exe changes its CWD ... using "Open in Windows Terminal" with a re-used instance. IIRC, folks wanted new shells opened in "." to follow the "open in" mechanism around. (yes/no?) |
So how do I force Windows Terminal to change the current working directory when I change the directory in the command prompt? |
The short answer is you can't. Why would you want to? |
Consider the following scenario:
That's because someone else started the terminal from that directory a few hours ago. At this point your option is to look for the handle, then try to match the PID of the process that owns it with the right instance of the terminal and close it, otherwise you won't be able run the automated build process. Not the best user experience. And if that first terminal was actually used to start some long running process (e.g. a regression suite for another build configuration, 8+ hours total and you're on hour 4), then you're out of luck. Mind you, all of this works with the old conhost just fine -- there are no issues with deleting the starting directory, so from our point of view, this is effectively a regression. Not sure how it fits with the plans for making Windows Terminal the default terminal in the future. |
Now, it doesn't have to actually track the changes to the current directory in the shell. If there were an option to launch the terminal with the current working directory set to say, the system directory, but the current directory in the shell set to ".", then that would work just fine for me... |
I'm not sure what you're saying. If the terminal's CWD is the system directory, what is "."? |
Terminal already has some provisions for caching that it thinks the current working directory should be, and it already forcibly resolves relative paths. It would be trivial for us to:
This would allow us to unlock the working directory and still work normally. |
The directory from which I want to launch the terminal. In the example at the top of the thread it is |
Again, when I launch a new instance of the terminal from an arbitrary directory, I want either:
The reason is that, currently, if you launch the terminal from an arbitrary directory, you cannot delete it until you close that instance of the terminal, and for various reasons you might actually want to delete it at some point. This is even more confusing, if you started your terminal some time ago (or, for example, it was launched by someone else the previous week), because there is absolutely nothing in the UI that indicates/reminds you that this is the case. |
Okeydokey well it seems like Dustin's got a good idea up in #10849 (comment) so let's just do that. |
I think that's what @DHowett is talking about. It'd probably also make sense to ensure that OpenConsole.exe's CWD is something innocuous. Off-topic, but somewhat interesting ... I never noticed until today that when a console is started, conhost.exe's CWD is c:\windows. You can get some dicey situations in a console. Start a CMD.EXE console with CWD = directory_A. At the CMD prompt, issue |
When an application creates a console session at startup, or via In Windows 10, conhost.exe can also be executed directly, which defaults to spawning cmd.exe as the client application, unless a different executable is passed in the command line string. I think for this case conhost.exe should query and save the working directory in order to spawn the client with the desired working directory, but then change its working directory to
CMD uses the process working directory to track its CLI working directory, i.e. the directory that's set by |
Hi! We've identified this issue as a duplicate of another one that already exists on this Issue Tracker. This specific instance is being closed in favor of tracking the concern over on the referenced thread. Thanks for your report! |
Windows Terminal version (or Windows build number)
1.9.1942.0 (Windows version: 10.0.19043.1110)
Other Software
No response
Steps to reproduce
wt -d .
to launch a new instance of Windows Terminal with Command Prompt as the default profile.cd ..
to go the the directory above (c:\temp
).rmdir rmdir_test
to attempt to delete the emptyrmdir_test
directoryExpected Behavior
I was expecting to be able to delete the
rmdir_test
directory without having to close the terminal window.If I repeat the above steps using conhost instead of Windows Terminal (i.e. typing
cmd.exe
in the address bar instead ofwt -d .
), I can delete that directory just fine, with the command prompt still open.Actual Behavior
I get the following error:
The process cannot access the file because it is being used by another process.
If I look for the handle in Resource Monitor, I get this:

It seems that the Terminal retains that file handle for as long as the process is alive, even if I navigate away from the starting directory.
Seems to be similar to #9664, which was marked as resolved and closed, although I don't really see any solutions in there that would be applicable in my case...
The text was updated successfully, but these errors were encountered: