upssched: rewrite WIN32 timer daemon spawning code path - #3549
Open
jimklimov wants to merge 2 commits into
Open
Conversation
…de path [networkupstools#3525] Co-authored-by: @alexdruqn-cpu Co-authored-by: Claude Sonnet 4.6 Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
…ols#3525] Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
|
A ZIP file with standard source tarball and another tarball with pre-built docs for commit 57e63a2 is temporarily available: NUT-tarballs-PR-3549.zip. |
|
✅ Build nut 2.8.5.4992-master completed (commit 8a19fd3f01 by @jimklimov)
|
|
✅ Build nut 2.8.5.4992-master completed (commit 8a19fd3f01 by @jimklimov) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Expected to help with issue #3525, code proposed by Claude Sonnet and @alexdruqn-cpu in that discussion.
Original description from #3525 (comment) follows:
Summary of All Fixes to
upssched.c(Win32)1. Process-cascade fix (
start_daemon()/main()/conf_arg()/checkconf())CreateProcess()spawned an unmarked clone of the executable, which inheritedUPSNAME/NOTIFYTYPEand re-ran the full CLI path (re-parsing config, re-callingsendcmd()), combined with a racy, unconditional lock-file deletion — causing a runaway spawn cascade instead of one client + one daemon.WIN32_DAEMON_MARKER) and flag (is_win32_timer_daemon) so a spawned process is explicitly told "you are the daemon," intercepted at the top ofmain(), bypassing normal CLI/option parsing.conf_arg()now also gates on this flag (same aslist_timers) to parse global directives but skipAT-line actions.start_daemon()(Win32 branch) rewritten: clearsUPSNAME/NOTIFYTYPE/NOTIFYMSGbefore spawning, passes a readiness-eventHANDLEto the child, waits on it (CreateEvent/WaitForSingleObject) instead of becoming the daemon itself, then returns — mirroring the non-WIN32 parent branch.win32_run_timer_daemon(), entered only by the marked child; it callsSetEvent()once its named pipe is listening.pinfo.hProcess/hThreadare now closed afterCreateProcess().2. Overlapped-read fix (
sendcmd(), Win32 branch) — explains"read confirmation got []"while (ReadFile(...))used the call's return value as the loop condition. For an overlapped pipe handle,FALSE+ERROR_IO_PENDINGis the normal pending state, not failure — so the loop body (which waited on the event and parsed the reply) was almost never entered, leavingbufempty.continue/breakinside that loop were meant to retry/give up on the outerfor (i < MAX_TRIES)loop but only affected the inner (dead) loop — and did so afterCloseHandle(pipefd), risking a read on an already-closed handle.GetOverlappedResult()was never called, and bothpipefd(on success) andread_overlapped.hEvent(always) leaked.ERROR_IO_PENDING, callGetOverlappedResult()to get the actual byte count and NUL-terminate properly, replace the nested loop with awin32_outer_actionflag sobreak/continuesit directly in the outer loop, and close both handles exactly once on every exit path.Not a bug (clarified, no code change):
FILE_FLAG_OVERLAPPED+PIPE_WAITinCreateNamedPipe()— correct, documented combination;PIPE_WAITonly affects non-overlapped calls, which this code never makes.[OK\n]in the logs — literal\nbyte inside the real 3-byte"OK\n"payload, just printed unescaped byupsdebugx.Both patches were verified for brace/paren balance and cross-checked against
common.h/wincompat.h/upssched.h(confirmedTYPE_FD == HANDLE,<windows.h>availability, etc.), but not compiled — no Win32 cross-compiler available in this environment, so a real build/test on your side is still needed.