Repository navigation
Conversation
|
and a broken fork/pipe, then hang: |
|
Abandoned? |
|
@jay I'm not sure if this is just overkill/unnecessary or potentially fixing edge cases. 1 edit: re-reading my own assesment in the PR, I'd find it quite unlikely if CRT calls Footnotes
|
There was a problem hiding this comment.
Pull request overview
This PR experiments with minimizing potentially unsafe logging/diagnostic calls inside Windows callback contexts in the test server utilities, replacing them with direct Win32 GetStdHandle() + WriteFile() output (and allowing opt-in restoration via DEBUG_WIN32_CALLBACKS).
Changes:
- Add an opt-in
DEBUG_WIN32_CALLBACKStoggle for restoring prior detailed logging in Windows callbacks. - Replace
logmsg()/fprintf()-based logging in Windows CTRL handler, window proc, and message loop error paths withWriteFile()to stderr. - Reduce dependency on CRT/internal helper APIs in these callback paths.
Comments suppressed due to low confidence (5)
tests/server/util.c:476
- WriteFile() expects a DWORD byte count; CURL_CSTRLEN() is size_t. Adding an explicit cast and ignoring the return value explicitly avoids potential conversion warnings in Windows builds.
WriteFile(out, msgend, CURL_CSTRLEN(msgend), &dwWritten, NULL);
tests/server/util.c:514
- WriteFile() expects a DWORD byte count; CURL_CSTRLEN() is size_t. Adding an explicit cast (and explicitly discarding the return value) helps avoid conversion warnings on Windows toolchains.
WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, CURL_CSTRLEN(str),
&dwWritten, NULL);
tests/server/util.c:547
- WriteFile() expects a DWORD byte count; CURL_CSTRLEN() is size_t. Adding an explicit cast (and explicitly discarding the return value) helps avoid conversion warnings on Windows toolchains.
WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, CURL_CSTRLEN(str),
&dwWritten, NULL);
tests/server/util.c:567
- WriteFile() expects a DWORD byte count; CURL_CSTRLEN() is size_t. Adding an explicit cast (and explicitly discarding the return value) helps avoid conversion warnings on Windows toolchains.
WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, CURL_CSTRLEN(str),
&dwWritten, NULL);
tests/server/util.c:582
- WriteFile() expects a DWORD byte count; CURL_CSTRLEN() is size_t. Adding an explicit cast (and explicitly discarding the return value) helps avoid conversion warnings on Windows toolchains.
WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, CURL_CSTRLEN(str),
&dwWritten, NULL);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
but do you know that this actually fixes anything |
I don't, I just have this long-term desire to fix potential non-correctness to sort out Initially this was pointed out by an LLM, but hard facts that I could find, are sparse. |
|
the ctrl handler in windows runs in its own thread so as long as the functions called are thread safe it should be ok. I took a look at logmsg and nothing stands out, I don't see any use of global variables etc. however... the way logmsg works where it opens and closes the logfile for every call does not seem good. opening a FILE for logfile at the start would make more sense, then windows closes it automatically when the program ends. currently though issue would be if logmsg in the handler thread can't open the logfile because it's already open in another thread. generally with the tests a possible issue that could cause crashes in windows is if tool code is being tested and tool_stderr is not initialized yet so maybe some tool function writes to tool_stderr while it's still null? though tool_stderr does appear to be initialized at the start so I guess ok. tool_stderr starts off null because in windows technically stderr is a function iob_func(2) or something and so we can't assign that to tool_stderr at compile time. |
Sensible point. This is fixed by this patch, but in general it'd be (In my understanding the logging within the handler doesn't serve Another concern of mine is that a relatively complex function such
Maybe we could check this and somehow fail gracefully but visibly |
|
The only issue we know this solves though is the logmsg contention if it's called from multiple threads, and IMO that's more of a logmsg issue, it doesn't need a bypass of the function. Unless we know this PR solves an issue in correctness.. because I can't point to what's not correct or what this fixes (sans logmsg file contention). Also, creating a zero length logfile I don't think would be a problem unless there are scripts that rely on the file existence for detection, not sure.
Yes is a point but I mean for posterity we could just add a comment that logmsg is run multi-threaded., really that seems to me to be the solution here rather than gate everything separately with the WriteFiles
Yeah I was being theoretical here I don't know it's an issue but I was thinking in general if we see a null dereference crash in Windows it could possibly be that. I wouldn't worry about it unless we start seeing that... |
I'd also be guessing it's okay.
I have doubts it helps in practice. It's super easy to overlook, |
|
It doesn't look like what I suggested earlier (to open the logfile at the beginning) would be simple because the signal handler If you are concerned about calling logmsg multi-threaded like in the control handler or the window thread then you could call For logmsg calls for example rather than all this: #ifdef DEBUG_WIN32_CALLBACKS
logmsg("main_window_proc: %u -> %d", uMsg, signum);
#else
static const char str[] = "main_window_proc\n";
DWORD dwWritten;
WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, CURL_CSTRLEN(str),
&dwWritten, NULL);
#endifI suggest this: fprintf(stderr, "main_window_proc: %u -> %d", uMsg, signum);and a note in those functions not to call logmsg since those functions run in separate threads |
|
I'm proposing #22454 to replace this. Basically it syncs logmsg in Windows, and that should take care of allowing logmsg to be called from multiple threads in Windows. |
|
edit: re |
|
The solution is simple and I disagree that it's overkill. logmsg has the advantage that it logs to the file, of course. fprintf is fine too. |
…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
main_window_loop() (Windows)
|
main_window_loop() (Windows)main_window_loop() (Windows)
curl already built like this in UWP mode; add a macro to allow building regular Win32 targets too, meaning without the hidden GUI window, window handler, and thus without support for shutting down `servers.exe` gracefully. To test the server in CI as a plain console app and remove one component/variable from the system, in an effort chasing flaky runs. The graceful option was used by `taskkill` (without `-f`) in runtests, and it has been disabled (in favor of always `-f` (forced) kill), in an attempt to avoid taskkill hanging. Those resolved, but general CI flakiness remained. We see `taskkill` invoked seldom per job, and in most of these cases it doesn't find the PID requested. It means the graceful option is not actually used since 2025-04, and rather rarely before that. Also just today, the callback and main loop received a few updates to avoid potentially unsafe calls and interaction with the ctrl handler. Not expecting anything breakthrough here. Also graceful shutdown is a useful feature to cleanup properly and to be in sync with other platforms. Also: GHA/windows: enable in Windows CI jobs. Ref: https://learn.microsoft.com/windows/console/registering-a-control-handler-function#listen-with-hidden-window-example Follow-up to 7db9947 #22045 Follow-up to 9ea4881 #22487 Follow-up to b11e002 #22495 Follow-up to 208b877 #21039 Follow-up to f450f38 #19897 Follow-up to 2701ac6 #19421 Follow-up to 4e203f6 #17054 Closes #22496
To simplify and to avoid the chance of potential interference or
thread-safety issues. If one these 3 Win32 API calls fail, there is
likely a serious problem, out of the code's control. Knowing
GetLastError()is unlikely to help.Refs:
https://learn.microsoft.com/windows/win32/api/winuser/nc-winuser-wndproc
https://learn.microsoft.com/previous-versions/windows/desktop/legacy/ms686736(v=vs.85)
https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-getmessage
https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-createwindowexa
https://learn.microsoft.com/windows/win32/api/winuser/nf-winuser-registerclassa
Ref: 9ea4881 #22487
Ref: 1c49f2f #18451
Follow-up to ac1e206