diff options
author | Spencer Low <CompareAndSwap@gmail.com> | 2018-08-25 23:34:33 -0700 |
---|---|---|
committer | Spencer Low <CompareAndSwap@gmail.com> | 2018-08-25 23:46:48 -0700 |
commit | 84fc27159a60e439f391e439c923cdf393f522f6 (patch) | |
tree | 80a3a4df121ca895d7d3aa0d16d6c2240f004339 | |
parent | 6d2ace3684a06ea81d853657cbc6f5f0682667bc (diff) | |
download | system_core-84fc27159a60e439f391e439c923cdf393f522f6.tar.gz system_core-84fc27159a60e439f391e439c923cdf393f522f6.tar.bz2 system_core-84fc27159a60e439f391e439c923cdf393f522f6.zip |
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 d51c6df1ef98f2b57916ddefc80afda5f1eecb59.
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>
-rw-r--r-- | adb/client/main.cpp | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/adb/client/main.cpp b/adb/client/main.cpp index 095ad98ff..a7e454d92 100644 --- a/adb/client/main.cpp +++ b/adb/client/main.cpp @@ -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) { |