adb: win32: fix Ctrl-C of adb server nodaemon

On Windows, when running adb server nodaemon and pressing Ctrl-C,
adb_server_cleanup (an atexit handler) would call kick_all_transports()
which would eventually fail a CHECK because the current thread was not
equal to the main thread. This is because Ctrl-C is implemented in
Windows by the OS creating a new thread in the process and calling the
Ctrl-C handler from there.

The CHECK fail would print out the CHECK expression and call abort()
which would record a crash in the Windows Event Log, plus would
potentially upload a crashdump to Microsoft's Watson service.

This might be a regression from d51c6df1ef.

The fix is to share more code between platforms, removing the call to
Win32 SetConsoleCtrlHandler() and just use the C Runtime's signal()
implementation which is built upon SetConsoleCtrlHandler(). The signal
handler still ends up being called from another thread, but the handler
is thread-safe enough so this seems to work.

Test: On Win10 and Vista, run adb server nodaemon and then try Ctrl-C,
Ctrl-Break and close console window.

Change-Id: I6603970616098d2b3ce68f2a3d4e5515ec859811
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
This commit is contained in:
Spencer Low 2018-08-25 23:34:33 -07:00
parent 6d2ace3684
commit 84fc27159a
1 changed files with 7 additions and 12 deletions

View File

@ -56,15 +56,6 @@ static void setup_daemon_logging() {
LOG(INFO) << adb_version();
}
#if defined(_WIN32)
static BOOL WINAPI ctrlc_handler(DWORD type) {
// TODO: Consider trying to kill a starting up adb server (if we're in
// launch_server) by calling GenerateConsoleCtrlEvent().
exit(STATUS_CONTROL_C_EXIT);
return TRUE;
}
#endif
void adb_server_cleanup() {
// Upon exit, we want to clean up in the following order:
// 1. close_smartsockets, so that we don't get any new clients
@ -97,12 +88,16 @@ int adb_server_main(int is_daemon, const std::string& socket_spec, int ack_reply
}
}
SetConsoleCtrlHandler(ctrlc_handler, TRUE);
#else
// TODO: On Ctrl-C, consider trying to kill a starting up adb server (if we're in
// launch_server) by calling GenerateConsoleCtrlEvent().
// On Windows, SIGBREAK is when Ctrl-Break is pressed or the console window is closed. It should
// act like Ctrl-C.
signal(SIGBREAK, [](int) { raise(SIGINT); });
#endif
signal(SIGINT, [](int) {
fdevent_run_on_main_thread([]() { exit(0); });
});
#endif
char* leak = getenv("ADB_LEAK");
if (leak && strcmp(leak, "1") == 0) {