防止MSYS'bash'杀死捕获^ C的进程 [英] Preventing MSYS 'bash' from killing processes that trap ^C

查看:118
本文介绍了防止MSYS'bash'杀死捕获^ C的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制台模式的Windows应用程序(从Unix移植),最初设计为在收到 ^ C (Unix SIGINT)时执行干净退出.在这种情况下,干净的退出可能需要等待很长一段时间才能关闭远程网络连接. (我知道这不是 ^ C 的正常行为,但是我无法更改它.)该程序是单线程的.

I have a console-mode Windows application (ported from Unix) that was originally designed to do a clean exit when it received ^C (Unix SIGINT). A clean exit in this case involves waiting, potentially quite a long time, for remote network connections to close down. (I know this is not the normal behavior of ^C but I am not in a position to change it.) The program is single-threaded.

我可以使用signal(SIGINT)(在Unix下)或 ^ C 之后的100毫秒).这是不可接受的,因为如上所述,该程序需要等待远程网络连接关闭.

I can trap ^C with either signal(SIGINT) (as under Unix) or with SetConsoleCtrlHandler. Either works correctly when the program is run under CMD.EXE. However, if I use the "bash" shell that comes with MSYS (I am using the MinGW environment to build the program, as this allows me to reuse the Unix makefiles) then the program is forcibly terminated some random, short time (less than 100 milliseconds) after the ^C. This is unacceptable, since as I mentioned, the program needs to wait for remote network connections to close down.

人们很可能希望在MSYS bash下运行该程序.而且,这种效果破坏了测试套件.从程序内部(理想)或外壳程序上的设置(可接受),我都无法找到解决此问题的任何方法.有人可以推荐什么吗?

It is very likely that people will want to run this program under MSYS bash. Also, this effect breaks the test suite. I have not been able to find any way to work around the problem either from within the program (ideal) or by settings on the shell (acceptable). Can anyone recommend anything?

推荐答案

这可能是由于臭名昭著的问题(又称为小问题#56 ).在这种情况下,它表现为Ctrl-C突然杀死程序,而不是作为要捕获和处理的信号传递给程序.该理论的证据基于zwol的广泛解释:控制台模式Windows应用程序","[应用程序被设计为在收到 ^ C 时执行干净退出","[[应用程序]正常工作当程序在CMD.EXE下运行时",但"[[使用终端时]随MSYS程序一起被强制终止"(在撰写本文时(2018年),MSYS默认使用薄荷作为其终端)

This could be due to the infamous mintty "Input/Output interaction with alien programs" problem (aka mintty issue #56). In this case it is manifesting as Ctrl-C abruptly killing the program rather than being passed down to the program as a signal to be caught and handled. Evidence for this theory is based on zwol's extensive explanation: "console-mode Windows application", "[application is] designed to do a clean exit when it received ^C", "[application] works correctly when the program is run under CMD.EXE" but "[when using the terminal] that comes with MSYS [...] program is forcibly terminated" (at the time of writing (2018) MSYS defaults to using mintty as its terminal).

不幸的是, mintty不是完整的Windows控制台替代品,并且存在各种行为未实现本机" Windows程序所期望的.但是,当您在mintty中运行此类本机程序时,将它们包装在 winpty 中可能会感到有些高兴...

Unfortunately mintty isn't a full Windows console replacement and various behaviours expected by "native" Windows programs are not implemented. However, you might have some joy wrapping such native programs in winpty when running them within mintty...

其他问题也描述了这种行为:请参见 https://superuser .com/questions/606201/how-to-politely-kill-windows-process-from-cygwin

Other questions also describe this behaviour: see https://superuser.com/questions/606201/how-to-politely-kill-windows-process-from-cygwin and https://superuser.com/questions/1039098/how-to-make-mintty-close-gracefully-on-ctrl-c .

这篇关于防止MSYS'bash'杀死捕获^ C的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆