Skip to content

Fix SSHRemoteJobOperator on Windows hosts - #73352

Merged
kaxil merged 7 commits into
apache:mainfrom
timo-astronomer:fix-sshremotejoboperator-windows
Nov 4, 2026
Merged

kaxil merged 7 commits into
apache:mainfrom
timo-astronomer:fix-sshremotejoboperator-windows

Conversation

@timo-astronomer

Copy link
Copy Markdown
Contributor

Fix SSHRemoteJobOperator for windows.
Current code fails, since paths are enclosed in single quotes and PowerShell requires double quotes.
In addition, command fails to start, because stdout and stderr are redirected to the same file.

Both issues are fixed in this commit.
In addition, the payload is decreased by using variables for base path instead of writing the same, long path multiple times.
Command is written to job.ps1 file for execution to avoid double encoding.
Finally, command is executed as Win32_Process to prevent in from dying when the SSH session terminates.


Everything is tested with this environment

OS Name:                   Microsoft Windows Server 2022 Datacenter
OS Version:                10.0.20348 N/A Build 20348

PSVersion                      5.1.20348.5622
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.20348.5622
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Was generative AI tooling used to co-author this PR?
  • Yes (Claude code with Sonnet 4.6)

@boring-cyborg

boring-cyborg Bot commented Sep 18, 2026

Copy link
Copy Markdown

Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide
Here are some useful points:

  • Pay attention to the quality of your code (ruff, mypy and type annotations). Our prek-hooks will help you with that.
  • In case of a new feature add useful documentation (in docstrings or in docs/ directory). Adding a new operator? Check this short guide Consider adding an example Dag that shows how users should use it.
  • Consider using Breeze environment for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
  • Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
  • Please follow ASF Code of Conduct for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
  • Be sure to read the Airflow Coding style.
  • Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
    Apache Airflow is a community-driven project and together we are making it better 🚀.
    In case of doubts contact the developers at:
    Mailing List: dev@airflow.apache.org
    Slack: https://s.apache.org/airflow-slack

@timo-astronomer
timo-astronomer marked this pull request as ready for review September 18, 2026 18:18
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed in 5 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale Stale PRs per the .github/workflows/stale.yml policy file label Nov 4, 2026
@kaxil kaxil removed the stale Stale PRs per the .github/workflows/stale.yml policy file label Nov 4, 2026
kaxil added 5 commits November 4, 2026 02:00
Follow-up fixes on top of the Windows wrapper rewrite, each checked on
windows-2022 and windows-latest runners (Windows PowerShell 5.1):

- Inline the command into the job script as written. The script travels
  in a verbatim here-string, so backtick escaping reached PowerShell
  literally and broke quoted arguments.
- Run the command inside & { } so every statement's output is logged,
  not only the last one's.
- Write the log through a shared, auto-flushed UTF-8 writer. Add-Content
  kept the file locked until the command ended, so the trigger could not
  tail it while the job ran.
- Fail the submission when Win32_Process.Create cannot start the job,
  and start it in the SSH session's working directory, not System32.
- Pass job paths to PowerShell literally: single-quote everything except
  a leading $env:NAME, double PowerShell's typographic quotes, and use
  -LiteralPath so brackets are not read as wildcards. The same quoting
  now applies to environment values.
…nging

The command is written to cmd.ps1 and called from the job script inside
try/catch/finally, so the exit code is recorded however the command ends:
an inline `exit N` keeps N (skip_on_exit_code depends on it), a throw or
a parse error fails the job with the message in the log, and a crashed
process's negative code is recorded unsigned so the trigger accepts it.
A here-string in the command no longer ends the wrapper's own.

The job script is started with -Command instead of -File, so a Group
Policy that only allows signed scripts cannot stop the job's bookkeeping;
under such a policy the job fails with the policy error in its log.

*>&1 keeps Write-Host and warnings, and objects are formatted the way a
console shows them. The wrapper stops on the first setup error and
reports it on stderr, so a bad remote_base_dir fails the submission
instead of reporting a job that never starts. A leading $Env:NAME is
expanded in any casing.

Checked on windows-2022 and windows-latest runners, including a machine
policy of AllSigned.
The log tail now writes the chunk to stdout as UTF-8 bytes. Printed as a
PowerShell string, it was re-encoded in the SSH session's console code
page (437 on a default install), which the trigger's strict UTF-8
decoding rejects, so any non-ASCII log line put the trigger into a
reconnect loop until the task failed.

The job script opens its log through a local path, so an environment
entry named LOG_FILE can no longer redirect it, and the launch command
quotes the already-resolved job directory instead of re-expanding
$env:NAME in the new process's environment.

Document the Windows exit code rules, the Group Policy requirement and
the inline command length that fits through cmd.exe.
Emulate the wrapper's runtime quoting of the job directory and read the
launch command back, pin the log tail's byte write, and document what
shares the cmd.exe command line budget on Windows.
@kaxil
kaxil enabled auto-merge (squash) November 4, 2026 02:07
@kaxil kaxil changed the title Fix SSHRemoteJobOperator for windows Fix SSHRemoteJobOperator on Windows hosts Nov 4, 2026
@kaxil
kaxil disabled auto-merge November 4, 2026 11:39
@kaxil
kaxil merged commit b503e69 into apache:main Nov 4, 2026
76 checks passed
@boring-cyborg

boring-cyborg Bot commented Nov 4, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request! You are invited to check our Issue Tracker for additional contributions.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSHRemoteJobOperator: the default Windows remote_base_dir is never expanded, so the job directory cannot be created

2 participants