Fix caffeinate -disu: POSIX-style bundled flags support#2
Merged
Conversation
…tyle bundled flags
PowerShell does not support POSIX-style flag bundling natively, so
-disu was treated as a single unknown parameter name. Replace the
[Alias('caffeinate')] with a standalone caffeinate wrapper function
that parses bundled flags, builds a hashtable using Invoke-Caffeinate
parameter metadata to distinguish switches from value-taking params,
and splats correctly.
Fixes #1
SetThreadExecutionState returns 0 (failure) when ES_USER_PRESENT (0x04) is included in any flag combination on modern Windows. This caused ALL assertions to silently fail, so the system was never actually prevented from sleeping. Changes: - Never pass ES_USER_PRESENT to the API; -u now substitutes ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED instead - Add Set-SleepAssertion helper that checks the return value and warns on failure - Re-assert execution state every 30s in all wait modes to guard against thread-state loss - WaitPid mode now polls with WaitForExit(30000) instead of blocking indefinitely, enabling periodic re-assertion
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.
Summary
[Alias('caffeinate')]with a standalonecaffeinatewrapper function that supports POSIX-style bundled flags (-disu,-di,-ds, etc.) via hashtable splatting with parameter metadata introspectioncaffeinateas a function instead of an alias in the manifestES_USER_PRESENT(0x04) causingSetThreadExecutionStateto silently return 0 (failure), leaving the system completely unprotected.-unow substitutesES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIREDSet-SleepAssertionhelper that checks the API return value and warns on failureWaitForExit(30000)instead of blocking indefinitelyFixes #1, fixes #3
Test plan
caffeinate -disu -t 1-- bundled flags with timeoutcaffeinate -di -t 2-- partial bundled flags with timeoutcaffeinate -d -i -s -u -t 2-- individual switches (no longer returns 0)caffeinate -d -t 1 -Verbose-- verbose pass-throughcaffeinate -t 1 -WhatIf-- WhatIf pass-throughInvoke-Caffeinate -u -Timeout 2 -Verbose-- shows ES_USER_PRESENT substitution messagecaffeinate cmd.exe /c echo hello-- command mode pass-through