Skip to content

fix(session): replace unreliable OpenInputDesktop with WTSQuerySessionInformation - #194

Merged
dongle-china merged 2 commits into
SnishaperTeam:mainfrom
wuguo13842:fix/session-guard-use-wts-api
Nov 16, 2026
Merged

dongle-china merged 2 commits into
SnishaperTeam:mainfrom
wuguo13842:fix/session-guard-use-wts-api

Conversation

@wuguo13842

Copy link
Copy Markdown
Contributor

Context

This is a follow-up to the early-logon autostart bug (#192) and the wait-loop fix in the launch sequence. While the launch sequence now waits for the desktop, the underlying NonInteractiveSession() check still relies on OpenInputDesktop, which is notoriously unreliable for detecting if the desktop is truly ready.

OpenInputDesktop frequently returns 0 (failure) during the early logon phase before the shell is fully initialized. It is also bound to user input states (such as the lock screen), making it a poor choice for determining session readiness. Microsoft's own recommendation for polling session state is WTSQuerySessionInformation.

Changes

  • Removed the OpenInputDesktop logic from NonInteractiveSession().
  • Integrated WTSQuerySessionInformationW via wtsapi32.dll to query WTSConnectState.
  • The function now accurately reports whether the session is truly interactive by checking if the state is WTSActive (0).
  • If the session is still WTSConnected or WTSInitializing, it correctly identifies that the desktop is not yet ready.
  • Retained the Session 0 check (ProcessIdToSessionId) as the first line of defense.
  • Maintained the original declaration order in the var block to keep the diff clean.

This ensures that the system will not mistakenly fall back to CLI mode during the critical window of the user logging in, especially when combined with the --startup wait loop.

Verification Needed

The changes are based on Microsoft's documented behavior for WTSQuerySessionInformationW and WTSConnectState. However, I have not been able to run a full test matrix across all scenarios (e.g., fast user switching, remote desktop, and actual session 0 automation).

Reviewers/maintainers should verify:

  1. The reliability of the WTSActive check across different Windows versions.
  2. That the fallback return false (defaulting to GUI when API fails) behaves correctly in edge cases.
  3. That normal interactive launches and CLI commands still behave identically to before.

Platform note

Windows-only change. Linux and macOS rely on environment variables (DISPLAY / WAYLAND_DISPLAY) and are unaffected by this PR.

@snishaper-policy-bot

Copy link
Copy Markdown

📝 Contributor agreement required

Thank you for opening this pull request! Before CI can run, please sign the SniShaper contributor agreement.

Reply to this pull request with a comment containing the word agree (e.g. "I agree") to confirm that you have read and accept:

Once you reply, the cla-signed label is applied and CI starts automatically.

@snishaper-policy-bot snishaper-policy-bot Bot added the cla-pending Contributor agreement pending signature (policy-bot) label Nov 16, 2026
@dongle-china
dongle-china enabled auto-merge (squash) November 16, 2026 12:02
@dongle-china

Copy link
Copy Markdown
Contributor

agree

@snishaper-policy-bot snishaper-policy-bot Bot added cla-signed Contributor agreement signed (policy-bot) and removed cla-pending Contributor agreement pending signature (policy-bot) labels Nov 16, 2026
@snishaper-policy-bot

Copy link
Copy Markdown

🔓 CI gate unlocked by maintainer @dongle-china — the cla-signed label has been applied and CI will start automatically.

@snishaper-policy-bot snishaper-policy-bot Bot added the reviewers-assigned Reviewers assigned (policy-bot) label Nov 16, 2026
The secrets context cannot be referenced in a job-level if: the workflow fails to start with no jobs scheduled. Detect fork PRs via github.event.pull_request.head.repo.fork instead; it is null on push events, so direct runs keep the syntax-bot reporting.

Co-authored-by: SniShaper <SniShaper@proton.me>
@dongle-china
dongle-china force-pushed the fix/session-guard-use-wts-api branch from 8756f72 to 6c6a5ad Compare November 16, 2026 12:20
@dongle-china

Copy link
Copy Markdown
Contributor

感谢修复此问题,我们将会在检查通过后合并。

@dongle-china
dongle-china merged commit 73e4ddf into SnishaperTeam:main Nov 16, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed Contributor agreement signed (policy-bot) reviewers-assigned Reviewers assigned (policy-bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants