Skip to content

feat(cli): report the real exit code and really stop what uipath server runs - #1842

Merged
robert-ursu merged 6 commits into
mainfrom
feat/python-job-cancellation
Nov 24, 2026
Merged

robert-ursu merged 6 commits into
mainfrom
feat/python-job-cancellation

Conversation

@robert-ursu

@robert-ursu robert-ursu commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Pairs with UiPath/hdens#7868, which makes the handler send the stop, act on the answer, and kill and restart the server when a job cannot be stopped.

Why

Exit codes

A failed execution inside uipath server was reported as a success, on both transports:

  1. ConsoleLogger.error ends in click.get_current_context().exit(1). Under standalone_mode=False click returns that code instead of raising (click/core.py, except Exit). _run_command_isolated took the return value as the result and hard-coded ExitCode: 0. Every console.error path (runtime errors, unexpected exceptions, bad --simulation) came back as exit code 0.
  2. The HTTP /jobs/{key}/start reply had no exitCode. The .NET PythonRuntimeClient reads exitCode into an int and treats a missing one as 0, so every HTTP 200 counted as a success, even when the body said success: false.

The handler usually overrides the exit code with the status in output.json or the IPC result. The exit code decides the outcome when neither exists: failures before or outside UiPathRuntimeContext, and crashes.

Stop

uipath server could not stop what it was running:

  • IPC StopJob was a no-op that answered True.
  • HTTP had no stop route at all.
  • Cancelling the awaiting task was worse than doing nothing. When the handler's IPC connection drops, uipath-ipc cancels every in-flight handler (uipath_ipc/client/connection.py, _teardown). await asyncio.to_thread(...) raised, the lock was released and env/cwd were restored while the execution's thread kept running. The next execution then ran concurrently in a mutated process.

What: exit codes

  • _server_core: an int returned by cmd.main is the exit code. run, debug and eval never return an int of their own.
  • HTTP start reply: exitCode is added to every response. An unchanged handler becomes correct against this runtime with no .NET change.
  • _job_api: a result status the sink does not know is reported as Faulted, not Successful. "stopped" maps to the wire's Stopped (4), which the handler already accepts.
  • uipath run / uipath eval: entrypoint or eval-set discovery failure exits 1 instead of 0.

What: how an execution is stopped

run, debug and eval each drive their own event loop on the worker thread, so an execution is an event loop. They now call run_execution_loop instead of asyncio.run. It publishes the loop and its root task to the ExecutionControl carried on a ContextVar. Outside the server (uipath run on a terminal) it is asyncio.run unchanged.

stop_execution(job_key, resume_version, force) is shared by IPC StopJob and the new POST /jobs/{key}/stop (200 {"stopped": bool}).

Both a stop and a kill are cooperative and run the same sequence; a kill (forceStop: true) only has tighter windows:

  1. Cancel the execution's root task. The runtime unwinds cooperatively, and UiPathRuntimeContext.__exit__ still writes the result. Wait 30 s, or 5 s for a kill.
  2. Cancel every task on the execution's loop. This gives up on a clean cleanup. Wait 10 s, or 5 s for a kill.
  3. Answer False if the execution still runs. It is typically blocked inside a call that cancellation cannot reach (a sync socket read, a lock wait, a busy loop with no await), and only ending the process stops it. Whether to do that is the handler's call, since the process and the execution instance belong to it.

So a stop answers within about 40 s and a kill within about 10 s, both within the handler's 60 s StopJobTimeout.

Cases that answer True:

  • A queued execution (waiting for the lock) is dropped before it runs.
  • An attempt (job key and resume version) that is not queued or running. The match is exact: the first attempt has no resume version, and a stop for it must not reach a resumed run. Answering False there would invite the handler to kill a resumed run for a stop aimed at the previous one.
  • A stop that arrives before its start. The handler starts a job on a background task, so its stop can reach the server first. The stop is kept for 60 s, and a start of that exact attempt within that time ends as stopped without running.

The lock waits for the thread

_run_command_isolated now returns or raises only once the worker thread has exited. Cancelling the caller, for example on a dropped connection, stops the execution. The CancelledError is re-raised only after that, so the lock, env and cwd are never handed on while the job still runs.

It also waits for the threads the execution started. run_execution_loop gives the loop its own default executor and returns only once that executor's threads have exited. From Python 3.12 asyncio.Runner stops waiting for them after five minutes, and a sync node still blocked in asyncio.to_thread would then see the next job's env and cwd.

The job-scope teardown also completes before the env is restored, so the xfail in test_server_job_core.py is removed.

Outcomes

How the execution ended ExitCode Error
Stopped on request, or before it started 143 Stopped on request
Its own code let a CancelledError escape 1 (unexpected) The execution cancelled itself
Otherwise click's exit code, 0 on success Exit code: N

An execution counts as stopped only if the stop's cancellation reached its root task while that task still ran. A stop that arrives after the execution cancelled itself does not turn the fault into a stop.

A stopped execution's own result now says stopped too. The execution's loop installs uipath-runtime's stop-requested probe, answered by whether a stop reached it, and uipath-runtime 0.13.6 then writes the stopped status, with no error, to output.json and the result sink (UiPath/uipath-runtime-python#171). With an older uipath-runtime the result still says Faulted/ERROR_CancelledError, and the handler reports Stopped for an Orchestrator stop regardless.

Worth a careful look

  • The worker is a plain run_in_executor future, not a task. A task re-raises the execution's SystemExit into the server loop, and _run_command_isolated turns SystemExit into an exit code.
  • An execution waiting on its own to_thread work (sync LangGraph nodes, sync HTTP clients) runs its finally at once. But the execution's asyncio.Runner does not close until that call returns. run_execution_loop withdraws the loop before the runner closes, so the sweep in step 2 cannot cancel the runner's wait on those threads, which would leak them. Such an execution answers False if the call outlives the grace, and holds the lock until the threads exit, past the runner's own five-minute limit on Python 3.12+.
  • ExecutionControl.cancel() delivers at most once. A stop followed by a force stop is ordinary, and a second delivery would land inside the cleanup that writes output.json.
  • Delivery is recorded on the execution's loop, only when task.cancel() lands on a task that is still running. Both the outcome and the runtime's probe read it, so a stop that arrives after the task finished, or while Runner.close waits for its threads, does not turn a self-cancellation into a stop.

Testing

  • uv run pytest tests/cli: 1622 passed, 1 skipped. One local failure, test_signalr_debug_hub.py::test_websocket_upgrade_sends_the_negotiate_cookie (a TimeoutError on Windows), is in code this PR does not touch.
  • The server and cancellation tests also pass on Python 3.13.
  • ruff check, ruff format --check and mypy are clean.
  • New tests are in tests/cli/test_server_cancellation.py (31). They use click commands shaped like the real ones and cover:
    • a running execution stops, its cleanup runs and the lock is freed;
    • a stop that arrives before the execution has a loop is applied when it gets one;
    • a repeated or forced stop does not abort a slow cleanup;
    • a force stop gives up within its own, shorter window;
    • an execution waiting on a nested thread stops once that call returns;
    • an uninterruptible execution answers False and keeps the lock until its thread exits;
    • a self-inflicted cancel is a fault, also when a stop arrives after it;
    • a queued execution is dropped, and cancelling a queued execution's caller does not leak the lock;
    • a stop for another attempt leaves the live run alone: another resume version, and no resume version against a positive one in both directions;
    • a stop that arrives before its start stops that attempt only, once, and expires;
    • an execution holds the lock until the threads it started exit (fails on Python 3.13 without the fix);
    • a cancelled caller stops the execution and waits for its thread, keeping the lock and the execution's env meanwhile;
    • IPC StopJob stops a running RunJob;
    • the HTTP stop route stops a running /start, and the routes reject a boolean resumeVersion and a forceStop that is not a boolean.
  • Exit-code tests:
    • a real click command that calls ConsoleLogger.error reports exit code 1 through _run_command_isolated;
    • an execution started over HTTP whose entrypoint raises replies success: false, exitCode: 1;
    • an unknown result status maps to Faulted, and "stopped" to Stopped;
    • discovery failures exit 1.
  • Tested end to end in alpha, on a pooled Serverless pod with a dev build of this PR and UiPath/hdens#7868:
    • Stop: the root task was cancelled and the run stopped within 0.3 s; the server answered stopped and the job ended Stopped.
    • Kill: the same within the kill window; stopped within 0.5 s.
    • A stop the server could not honour (simulated): the handler killed the server and restarted it. The new server was ready again about 16 s later, and the job ended Stopped.
    • A stop or kill reaches a Serverless job with its next heartbeat, so up to about 30 s after it is issued.
  • tests/cli/test_execution_control.py: the runtime learns a stop reached the execution, a stop that reaches a finished execution is not delivered, the probe stays inside the execution, an uncontrolled run installs none, and a runtime without the probe still runs.
  • Version 2.15.7 → 2.15.8.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Fmj9j6BHFgyskAHtyrUX7i

Development Packages

uipath

[project]
dependencies = [
  # Exact version (copy-paste ready):
  "uipath==2.15.8.dev1018428151",

  # Any version from this PR (uncomment to use a range instead):
  # "uipath>=2.15.8.dev1018420000,<2.15.8.dev1018430000",
]

[[tool.uv.index]]
name = "testpypi"
url = "https://test.pypi.org/simple/"
publish-url = "https://test.pypi.org/legacy/"
explicit = true

[tool.uv.sources]
uipath = { index = "testpypi" }

@github-actions github-actions Bot added test:uipath-langchain Triggers tests in the uipath-langchain-python repository test:uipath-integrations labels Aug 4, 2026
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch 2 times, most recently from ffcf810 to bca74f9 Compare August 4, 2026 11:09
@robert-ursu
robert-ursu force-pushed the feat/async-job-dispatch-and-result-push branch from 114a204 to 71c81e9 Compare August 4, 2026 11:13
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from bca74f9 to b05d195 Compare August 4, 2026 11:18
@robert-ursu robert-ursu changed the title feat(cli): really stop a job that is already executing feat(cli): really stop a job that is already executing [PC-4873] Aug 4, 2026
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from b05d195 to 1df44eb Compare August 4, 2026 16:00
@robert-ursu
robert-ursu force-pushed the feat/async-job-dispatch-and-result-push branch from 6c8af15 to e509941 Compare August 19, 2026 19:13
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 1df44eb to e14f4b2 Compare August 19, 2026 19:13
@robert-ursu
robert-ursu force-pushed the feat/async-job-dispatch-and-result-push branch from e509941 to db4653c Compare September 24, 2026 15:12
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from e14f4b2 to 50e0cc7 Compare September 24, 2026 15:26
@robert-ursu robert-ursu changed the title feat(cli): really stop a job that is already executing [PC-4873] feat(cli): really stop a job that uipath server is running [PC-4873] Sep 24, 2026
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 50e0cc7 to 0654cec Compare September 28, 2026 08:18
@robert-ursu robert-ursu changed the title feat(cli): really stop a job that uipath server is running [PC-4873] feat(cli): report a job's real exit code and really stop running jobs [PC-4873] Sep 28, 2026
@robert-ursu
robert-ursu changed the base branch from feat/async-job-dispatch-and-result-push to main September 28, 2026 08:19
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 0654cec to 7e4af34 Compare September 28, 2026 08:29
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 7e4af34 to 5c4ea34 Compare September 28, 2026 11:33
@robert-ursu robert-ursu changed the title feat(cli): report a job's real exit code and really stop running jobs [PC-4873] feat(cli): report a job's real exit code and really stop running jobs Sep 28, 2026
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 5c4ea34 to 37cc492 Compare September 28, 2026 11:46
@github-actions

Copy link
Copy Markdown

🚨 Heads up: uipath-integrations cross-tests are FAILING 🚨

Your changes may break one or more integrations in uipath-integrations-python:

  • uipath-openai-agents
  • uipath-google-adk
  • uipath-agent-framework
  • uipath-llamaindex
  • uipath-pydantic-ai

⚠️ These checks are NOT enforced by branch protection rules. Please review the failures before merging.

🔍 Inspect the failed run →

@robert-ursu robert-ursu changed the title feat(cli): report a job's real exit code and really stop running jobs feat(cli): report the real exit code and really stop what uipath server runs Sep 28, 2026
@robert-ursu robert-ursu added the build:dev Create a dev build from the pr label Oct 2, 2026
@robert-ursu
robert-ursu marked this pull request as ready for review October 2, 2026 07:50
Copilot AI balanced review requested due to automatic review settings October 2, 2026 07:50

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

A cancellation race can incorrectly report self-cancellation as an externally requested stop, and HTTP stop validation has type-safety gaps.

Review effort: Balanced
Findings: 3 Medium severity · 1 Low severity

Open (4)
What changed in this PR

Improves CLI server exit-code reporting and adds cooperative job cancellation across HTTP and IPC transports.

Changes:

  • Propagates real Click exit codes and maps runtime statuses safely.
  • Adds shared execution control and stop endpoints.
  • Expands cancellation, transport, and discovery-failure tests.
File Description
packages/​uipath/​pyproject.toml Bumps package version.
packages/​uipath/​uv.lock Updates locked package version.
packages/​uipath/​src/​uipath/​_cli/​_execution_control.py Adds cross-thread execution cancellation.
packages/​uipath/​src/​uipath/​_cli/​_server_core.py Tracks, stops, and reports executions.
packages/​uipath/​src/​uipath/​_cli/​_job_api.py Maps stopped and unknown statuses.
packages/​uipath/​src/​uipath/​_cli/​cli_server.py Adds HTTP stop and exit-code responses.
packages/​uipath/​src/​uipath/​_cli/​cli_server_ipc.py Implements IPC job stopping.
packages/​uipath/​src/​uipath/​_cli/​cli_run.py Enables controlled run cancellation.
packages/​uipath/​src/​uipath/​_cli/​cli_eval.py Enables controlled evaluation cancellation.
packages/​uipath/​src/​uipath/​_cli/​cli_debug.py Enables controlled debug cancellation.
packages/​uipath/​tests/​cli/​test_server_cancellation.py Covers cancellation behavior and transports.
packages/​uipath/​tests/​cli/​test_server.py Tests HTTP exit codes.
packages/​uipath/​tests/​cli/​test_server_job_core.py Tests core outcomes and teardown ordering.
packages/​uipath/​tests/​cli/​test_server_ipc.py Updates IPC stop and mock coverage.
packages/​uipath/​tests/​cli/​test_run.py Verifies discovery failures exit nonzero.
packages/​uipath/​tests/​cli/​test_job_api.py Tests new status mappings.
packages/​uipath/​tests/​cli/​eval/​test_eval_discovery.py Verifies evaluation discovery failures.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@sonarqubecloud

sonarqubecloud Bot commented Oct 5, 2026

Copy link
Copy Markdown

@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 842268b to 90ea2d8 Compare November 23, 2026 14:53
@github-actions

Copy link
Copy Markdown

🚨 Heads up: uipath-langchain cross-tests are FAILING 🚨

Your changes may break the uipath-langchain-python integration.

⚠️ These checks are NOT enforced by branch protection rules. Please review the failures before merging.

🔍 Inspect the failed run →

robert-ursu and others added 5 commits November 24, 2026 11:34
Under standalone_mode=False click returns ctx.exit(N)'s code instead of
raising, so every ConsoleLogger.error path came back as a result and
_run_command_isolated reported ExitCode 0. Map a returned int to the exit
code.

The HTTP start reply now carries exitCode, the field the .NET HTTP client
already reads (it defaulted a missing one to 0, so every 200 was a success).

StopJob returns False: this server cannot stop what it runs, and the handler
must not be told it did.

A result status the IPC sink does not know is reported as Faulted instead of
Successful, and "stopped" maps to the wire's Stopped.

Entrypoint discovery failures in `uipath run` and `uipath eval` exit 1.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
run/debug/eval drive their own event loop on the server's worker thread. They
now publish it through run_execution_loop to an ExecutionControl, so a stop
cancels the execution's root task and the runtime unwinds cooperatively,
still writing its result.

stop_execution backs both IPC StopJob and the new POST /jobs/{key}/stop. It
cancels the root task, waits a grace period, cancels every task on the
execution's loop, and answers False if it is still running, because it is
blocked in a call that only ending the process can interrupt. forceStop
shortens the waits. A queued execution is dropped before it runs. A stop that
targets another resume version, or an unknown key, answers True: that run is
not running.

The lock, env and cwd are no longer handed on while the execution's thread
still runs. A cancelled caller (a dropped IPC connection, a shutdown) stops
the execution and re-raises only once the thread has exited, and the job
scope's teardown completes before the env is restored.

A stopped execution ends with exit code 143 and "Stopped on request"; a
CancelledError it raised on its own is reported as an unexpected failure.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A stopped execution's own result said faulted with ERROR_CancelledError,
because the runtime could not tell the stop from a crash. The execution's
loop now installs uipath-runtime's stop-requested probe, answered by its
ExecutionControl, so the runtime writes the stopped status and hands it to
the result sink. With uipath-runtime before 0.13.6 nothing changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
A stop that arrived after the execution had already cancelled itself, while
Runner.close waited for its threads, was reported as stopped (143) because the
outcome read cancel_requested. The control now records delivery on the
execution's loop, only when task.cancel() lands on a task that is still
running, and both the server's outcome and the runtime's stop probe read that.

The HTTP routes also reject a boolean resumeVersion, which would otherwise
pass as 1, and a forceStop that is not a boolean, which was silently read as
an ordinary stop.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fmj9j6BHFgyskAHtyrUX7i
A stop matched any resume version when either side had none, so a stale stop
for a job's first attempt (no resume version) cancelled its resumed run. Stops
now match the job key and resume version exactly.

The handler starts a job on a background task, so its stop can reach the
server before the start. That stop answered True and the job then ran to
completion. A stop for an attempt that is not queued or running is now kept
for 60 seconds, and a start of that attempt within that window ends as stopped
without running.

From Python 3.12, asyncio.Runner stops waiting for the default executor after
five minutes. A sync node still blocked in asyncio.to_thread then outlived the
execution: the lock was released and the node saw the next job's environment
and working directory. run_execution_loop now gives the loop its own executor
and returns only once that executor's threads have exited, so the lock stays
held and a stop answers False, which makes the handler end the process.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fmj9j6BHFgyskAHtyrUX7i
@robert-ursu
robert-ursu force-pushed the feat/python-job-cancellation branch from 2912ada to b40ee05 Compare November 24, 2026 09:37
mypy does not see asyncio.constants as an attribute of asyncio.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Fmj9j6BHFgyskAHtyrUX7i
@sonarqubecloud

Copy link
Copy Markdown

@robert-ursu
robert-ursu merged commit c28536c into main Nov 24, 2026
220 of 221 checks passed
@robert-ursu
robert-ursu deleted the feat/python-job-cancellation branch November 24, 2026 11:52
RaresUiPath added a commit that referenced this pull request Nov 24, 2026
Main took 2.15.8 in #1842, which is already on PyPI.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build:dev Create a dev build from the pr test:uipath-integrations test:uipath-langchain Triggers tests in the uipath-langchain-python repository test:uipath-runtime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants