-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
CTRL-C event kills the application #205
Conversation
git-cmd.exe has a bad behavior when it receives CTRL-C event. A command line shell must handle the events. For example, CTRL-C should be used to terminate the current command and not to terminate the shell himself. When git-cmd.exe is launched, git-cmd.exe launches a child process (for example cmd.exe). git-cmd.exe monitors the child process. When the child process exits, git-cmd.exe exits too. When the user presses CTRL-C, the Windows console receives Windows messages for WM_KEYDOWN/WM_KEYUP. Then the TranslateMessage function generates a CTRL-C event. The CTRL-C event is handled by the Control Handler Function. When a CTRL_BREAK_EVENT, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated. cmd.exe Control Handler function terminates the current command and returns FALSE. git-cmd.exe has no Control Handler Function registered. So git-cmd.exe is terminated. When git-cmd.exe is used with programs like ConsoleZ, ConsoleZ injects a dll in git-cmd.exe and monitors this process. When the user presses CTRL-C, git-cmd is terminated and so the ConsoleZ's tab is closed. To fix this behavior, we can simply register a Control Handler function that returns TRUE (for all events). In this case the only way to exit this program is the launched command end. Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net>
I've tested this patch, rebuilt mingw-w64-git using the 64 bit Git SDK, replaced the stock git-cmd.exe with the newly built one and it seems to fix the issue. No regressions so far. |
This is almost the solution I was looking for. I will reformat the commit message to make it comply with Git's standards, no need to worry about that. The bigger worry is that there is a chance that we need to be able to disable the new behavior, most likely by introducing an appropriate command-line option. I will still need to test whether everything is working alright (I could imagine that Ctrl+C is no longer handled correctly when trying to interrupt a runaway executable spitting out millions of lines). However, I tested that the current |
@dscho I hope you do realize that |
I just created a shortcut, launched it, and it still does exactly what I want it to do: if I press Ctrl+C at the Bash prompt, it cancels the current command line and gives me a new one. If I run So yes, I think I realize how you run |
@dscho press Win+R (Run command), start Press Win+R, start cmd.exe, |
Unfortunately, my concerns were legitimate: if I run the patches |
FWIW I get the exact same freezing behavior with the patches |
@dscho sure, just give me a repo and the exact command you're running :) |
Actually, I've found a very simple way of triggering freezes: just start Here's the funny thing: if you let it run for like a minute, it'll eventually return the prompt and even display what you've typed during the freeze. Note: this only happens with Now the bad news: this happens both with and without the patch, so I'm not quite sure the freeze you encountered was caused by the patch, it may have always been present... I assume you don't usually invoke the shell with |
@cbucher I could imagine that something like this is what you actually need: diff --git a/compat/win32/git-wrapper.c b/compat/win32/git-wrapper.c
index 396a4ee..27b7777 100644
--- a/compat/win32/git-wrapper.c
+++ b/compat/win32/git-wrapper.c
@@ -535,6 +535,8 @@ int main(void)
si.dwFlags |= STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
}
+ if (wait)
+ creation_flags |= CREATE_NEW_PROCESS_GROUP;
br = CreateProcess(/* module: null means use command line */
exep,
cmd, /* modified command line */ |
@dscho for me all the difference this patch makes is that the first freeze kicks in after like 10 seconds instead of just 1. After that, additional freezes again take just 1 second. |
RC3 introduced the redirectors. This means that I can use just Now the funny thing is that even tho git-cmd.exe is no longer involved,
So basically, So after all, it may not be git-cmd's fault at all. At this point, it seems more like a bash problem. |
No, it is still the Git wrapper's problem (or ConsoleZ's, still unsure), because the redirector is simply an adapted copy of the Git wrapper, just like |
@bviktor did you have a chance to test the |
Sure, I already tried that. |
Hmm. This makes me believe that there is another, completely separate issue going on there, that is not helped by the ctrl handler nor the new process group? |
Should I open a separate ticket about Ctrl+C spam freezing? |
@bviktor I think this should be a separate ticket, because the symptom is already different, therefore I suspect the cause to be different, too. If this still happens even with the ctrl handler patch of @cbucher, I strongly suspect that it is a ConsoleZ issue, either. In any case, it will require quite a lot of effort to figure out, I am afraid. |
ConsoleZ issue for an issue you can reproduce without ConsoleZ?!? |
Lol, I believe it was a typo :D |
Well, given that I cannot reproduce that freeze after 10 seconds, I assume that it is a ConsoleZ issue. |
@dscho this happens with standard Git2, zero ConsoleZ involved. But I'll have to check it on a Win8 comp, too. If you can't repro, this may be Win10-specific then. I'll let you know in a new ticket. |
@cbucher BTW I think that we should introduce a new command-line option to install that ctrl handler and not do that by default, seeing as it would cause problems with MinTTY if we do so. I trust you agree that this is a good compromise. @bviktor I cannot reproduce. Git2, zero ConsoleZ. No freeze. I also got that exact same behavior on 8.1, 7 and XP. |
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
... while waiting for the child process to finish. The Git wrapper serves, among other things, as git-cmd.exe. In that role, its primary purpose is to provide an interactive cmd window that knows where to find Git. A secondary use of git-cmd.exe is to be able to launch other console processes that know about Git, e.g. when ConsoleZ wants to call an interactive Bash (it cannot call git-bash.exe because that would open a new MinTTY window). To this end, git-cmd.exe supports the --command=... command-line option. The interactive bash would be called like this: git-cmd --command=usr\bin\bash.exe -l -i The command-line arguments after the --command=... options are simply passed through to the command itself. If no --command=... option is specified, git-cmd.exe defaults to cmd.exe. Once git-cmd.exe is launched, it finds the top-level directory of the Git for Windows installation and then launches the command as a child process. And this is where things get a little bit tricky: When the user presses CTRL-C, the cmd window receives WM_KEYDOWN/WM_KEYUP messages which are then handled by the TranslateMessage function that generates a CTRL-C event that is sent to the console processes running in the console window (i.e. both git-cmd.exe and the child process). If no Console Ctrl Handlers have been registered, the git-cmd.exe process will simply be terminated, without having waited for the interactive Bash to quit (it does not quit, of course, because it handles Ctrl+C by terminating any process launched from within the Bash). Now both cmd and the Bash compete for user input. Luckily, the solution is very easy: the Win32 API sports a SetConsoleCtrlHandler() function to register/unregister Console Ctrl Handlers. When the NULL pointer is registered as "handler", it "causes the calling process to ignore CTRL+C input": https://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx This is exactly what we need here: while waiting for the child processes to finish, the git-cmd.exe process itself should not be interruptible by the user. Immediately after the child process terminates, we unregister the Console Ctrl Handler. Note: we need to be careful with changes to the Git wrapper as it serves many other purposes in addition to git-cmd.exe. For example, it serves as the cmd\git.exe as well as all of the git-<builtin>.exe stand-ins. So do we want the same Ctrl+C behavior even in those instances? Yes: If the user interrupts using Ctrl+C, the child process should terminate before the Git wrapper. Also note: We cannot override the Console Ctrl Handler with a function that simply always returns TRUE: this would prevent the console window opened via git-cmd.exe from closing, since the Console Ctrl Handler *also* handles "signals generated by the system when the user closes the console, logs off, or shuts down the system." [jes: changed the patch to conform with the surrounding coding style, to pass NULL as Console Ctrl Handler and unregister it as soon as appropriate, fixed commit message to be more accurate and informative, added link to the SetConsoleCtrlHandler() documentation.] This fixes #205 Signed-off-by: Christophe Bucher Developer <christophe.bucher@laposte.net> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
…on when creating loose object dirs When two gvfs-helper processes are the first to create a loose object directory, the processes (A and B in the timeline below) could have the following race: 1. A sees that the directory does not exist. 2. B sees that the directory does not exist. 3. A creates the directory with success. 4. B fails to create the directory and fails. Instead of having B fail here, just check for the directory's existence before reporting an error. That solves the race and allows tests to pass.
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
Includes commits from these pull requests: git-for-windows#191 git-for-windows#205 git-for-windows#206 git-for-windows#207 git-for-windows#208 git-for-windows#215 git-for-windows#220 git-for-windows#221 Signed-off-by: Derrick Stolee <dstolee@microsoft.com>
git-cmd.exe has a bad behavior when it receives CTRL-C event. A command line shell must handle the events.
For example, CTRL-C should be used to terminate the current command and not to terminate the shell himself.
When git-cmd.exe is launched, git-cmd.exe launches a child process (for example cmd.exe).
git-cmd.exe monitors the child process. When the child process exits, git-cmd.exe exits too.
When the user presses CTRL-C, the Windows console receives Windows messages for WM_KEYDOWN/WM_KEYUP.
Then the TranslateMessage function generates a CTRL-C event.
The CTRL-C event is handled by the Control Handler Function.
When a CTRL_BREAK_EVENT, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated.
cmd.exe Control Handler function terminates the current command and returns FALSE.
git-cmd.exe has no Control Handler Function registered. So git-cmd.exe is terminated.
When git-cmd.exe is used with programs like ConsoleZ, ConsoleZ injects a dll in git-cmd.exe and monitors this process.
When the user presses CTRL-C, git-cmd is terminated and so the ConsoleZ's tab is closed.
To fix this behavior, we can simply register a Control Handler function that returns TRUE (for all events). In this case the only way to exit this program is the launched command end.
Signed-off-by: Christophe Bucher Developer christophe.bucher@laposte.net