Skip to content

Improve test server signal handling and select_ws on Windows - #5260

Closed
mback2k wants to merge 4 commits into
curl:masterfrom
mback2k:add-exit-event
Closed

mback2k wants to merge 4 commits into
curl:masterfrom
mback2k:add-exit-event

Conversation

@mback2k

@mback2k mback2k commented Apr 18, 2020 •

Copy link
Copy Markdown
Member
  • tests/server/util.c: use raise instead of calling signal handler

    Use raise to trigger signal handler instead of calling it
    directly and causing potential unexpected control flow.

  • tests/server/util.c: fix thread handle not being closed

  • tests/server/util.[ch]: add exit event to stop waiting on Windows

    This commit adds a global exit event to the test servers that
    Windows-specific wait routines can use to get triggered if the
    program was signaled to be terminated, eg. select_ws in sockfilt.c

    The exit event will be managed by the signal handling code and is
    set to not reset automatically to support multiple wait routines.

  • sockfilt: make select_ws stop waiting on exit signal event

    This makes sure that select_ws behaves similar to real select
    which stops waiting on a signal handler being triggered.

    This makes it possible to gracefully stop sockfilt.exe on
    Windows with taskkill /IM sockfilt.exe (without /F force flag).

Comment thread tests/server/sockfilt.c Resolved
mback2k added 4 commits April 26, 2020 19:17
Use raise to trigger signal handler instead of calling it
directly and causing potential unexpected control flow.

Part of curl#5260
This commit adds a global exit event to the test servers that
Windows-specific wait routines can use to get triggered if the
program was signaled to be terminated, eg. select_ws in sockfilt.c

The exit event will be managed by the signal handling code and is
set to not reset automatically to support multiple wait routines.

Closes curl#5260
This makes sure that select_ws behaves similar to real select
which stops waiting on a signal handler being triggered.

This makes it possible to gracefully stop sockfilt.exe on
Windows with taskkill /IM sockfilt.exe (without /F force flag).

Part of curl#5260
@mback2k mback2k changed the title [WIP] Improve test server signal handling and select_ws on Windows Improve test server signal handling and select_ws on Windows Apr 26, 2020
@mback2k
mback2k marked this pull request as ready for review April 26, 2020 17:20
@mback2k
mback2k requested review from bagder and jay April 26, 2020 17:20

@jay jay left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine, if I was going to nitpick I wouldn't call the event got_exit_event, that implies a boolean that it "got" the exit event. How about exit_event or exit_event_hnd.

@mback2k

mback2k commented Apr 27, 2020

Copy link
Copy Markdown
Member Author

I think it's fine,

👍

if I was going to nitpick I wouldn't call the event got_exit_event, that implies a boolean that it "got" the exit event. How about exit_event or exit_event_hnd.

if you compare it to the existing boolean, yes, but to me it is the event that tells us that it got_exit_event/signal. I don't mind changing it during the merge, though.

mback2k added a commit that referenced this pull request May 2, 2020
Use raise to trigger signal handler instead of calling it
directly and causing potential unexpected control flow.

Reviewed-by: Jay Satiro
Part of #5260
mback2k added a commit that referenced this pull request May 2, 2020
@mback2k mback2k closed this in 7dc8a98 May 2, 2020
mback2k added a commit that referenced this pull request May 2, 2020
This makes sure that select_ws behaves similar to real select
which stops waiting on a signal handler being triggered.

This makes it possible to gracefully stop sockfilt.exe on
Windows with taskkill /IM sockfilt.exe (without /F force flag).

Reviewed-by: Jay Satiro
Part of #5260
@mback2k

mback2k commented May 2, 2020

Copy link
Copy Markdown
Member Author

@jay Thanks for the review!

vszakats added a commit that referenced this pull request Aug 6, 2026
…dd exit message

On Windows, the init code calls `SetConsoleCtrlHandler()`, and before
this patch also set handlers for all Unixy signals. Of these, `SIGBREAK`
(used on Windows-only), `SIGINT`, `SIGABRT` and `SIGTERM` were also
setting up a `SetConsoleCtrlHandler()`, in addition to the call made
directly. (The rest, `SIGHUP`, `SIGPIPE`, `SIGALRM` are either missing
the macros, or ignored by `signal()` on Windows.)

As per WINE sources, `SetConsolCtrlHandler(<h>, TRUE)` calls are
additive, which means the test server set up two console ctrl handlers.

Then the ctrl handler set directly (`ctrl_event_handler()`), was
triggering the other signal handler via `raise()`, for the 'initiate
exit' logic, which in turn triggered exiting a wait within `select_ws()`
and other loops. The Windows window handler also made use of the
`SIGTERM` event to initiate exit via `raise()` and the second signal
handler.

To simplify, de-duplicate the ctrl handlers by dropping `signal()` calls
and keeping the direct Win32 call with `ctrl_event_handler()` doing all
the signal handling on Windows. Break out the 'initiate exit' logic into
a function and call it from both Unix and Windows signal/ctrl/window
handlers. Also drop calling `raise()` on exit, because it's a no-op
without a `signal()` pair.

Also:
- drop logging the actual ctrl type number, replace with just logging
  whether we handled the event, in `ctrl_event_handler()`. To avoid
  using non-signal-safe functions (e.g. `fprintf()`) from the handler.
- also replace `logmsg()` with `WriteFile()` to prevent regressions.
  Ref: #22045
- replace `logmsg()` with `WriteFile()` in `main_window_proc()`.
- fix to forward ctrl handling to the OS in the rare case of failed
  `exit_event` initialization on startup. To swap a possible hang
  (within `WaitForMultipleObjectsEx()`) with an ungraceful shutdown.
- add support for an 'exit message' string, set by signal/ctrl handlers,
  and log it on app exit. To avoid the need to deal with logging within
  the handlers, yet have a static trace message about the event.
  Complementing the already logged signal number.
- drop stderr trace message from `exit_signal_handler()` in favor of an
  exit message. runtests triggers it frequantly, which added much noise
  to stderr. As a bonus, this also allows dropping the compiler warning
  suppression.
  Reported-by: Stefan Eissing
  Bug: #22487 (comment)
  Follow-up to 3aae64e #22507

Refs:
https://learn.microsoft.com/windows/console/setconsolectrlhandler
https://learn.microsoft.com/windows/console/registering-a-control-handler-function
https://learn.microsoft.com/cpp/c-runtime-library/reference/raise
https://learn.microsoft.com/cpp/c-runtime-library/reference/signal
https://gitlab.winehq.org/wine/wine/-/blob/wine-11.14/dlls/kernelbase/console.c#L1517-1526
https://github.com/huangqinjin/ucrt/blob/d6e817a4cc90f6f1fe54f8a0aa4af4fff0bb647d/misc/signal.cpp#L286-L348

Follow-up to fe28fcf 7dc8a98 0e05877 #5260

Closes #22487
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

2 participants