-
Notifications
You must be signed in to change notification settings - Fork 71
Fix: non-map state creates lost runs every time #4626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
taylordowns2000
wants to merge
5
commits into
main
Choose a base branch
from
return-42-for-lost
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−1
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,197 @@ | ||
| defmodule Lightning.WorkflowEdgeCasesTest do | ||
| @moduledoc """ | ||
| Integration tests for "lost run" scenarios — single-step workflows where | ||
| the job code exercises edge cases around run completion and state handling. | ||
|
|
||
| To add a new test case, define a function that returns the job body string, | ||
| then call `run_single_step_workflow/2` with your webhook input and job body. | ||
| Assert against the returned `%{run: run, step: step, work_order: work_order}`. | ||
| """ | ||
| use LightningWeb.ConnCase, async: false | ||
|
|
||
| import Ecto.Query | ||
| import Lightning.Factories | ||
| import Mox | ||
|
|
||
| alias Lightning.Runs | ||
| alias Lightning.Runs.Events | ||
| alias Lightning.Invocation | ||
| alias Lightning.Repo | ||
| alias Lightning.Runtime.RuntimeManager | ||
| alias Lightning.WorkOrders | ||
| alias Lightning.Workflows.Snapshot | ||
|
|
||
| setup :set_mox_from_context | ||
| setup :verify_on_exit! | ||
|
|
||
| setup_all do | ||
| Mox.stub_with(Lightning.MockConfig, Lightning.Config.API) | ||
| Mox.stub_with(LightningMock, Lightning.API) | ||
|
|
||
| Mox.stub_with( | ||
| Lightning.Extensions.MockUsageLimiter, | ||
| Lightning.Extensions.UsageLimiter | ||
| ) | ||
|
|
||
| start_runtime_manager() | ||
|
|
||
| uri = LightningWeb.Endpoint.url() | ||
|
|
||
| %{uri: uri} | ||
| end | ||
|
|
||
| setup [:register_and_log_in_superuser, :stub_rate_limiter_ok] | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Test cases — each one only needs to supply the job body and assertions. | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @tag :integration | ||
| @tag timeout: 10_000 | ||
| test "job that returns 42 completes successfully", %{uri: uri} do | ||
| job_body = """ | ||
| fn(state => { | ||
| // if you return a non-map, it used to cause lost | ||
| // now it gets wrapped in a map ¯\_(ツ)_/¯ | ||
| return 42; | ||
| }); | ||
| """ | ||
|
|
||
| result = run_single_step_workflow(uri, %{"x" => 1}, job_body) | ||
|
|
||
| assert result.run.state == :success | ||
| assert result.step.exit_reason == "success" | ||
|
|
||
| assert %{"value" => 42} = | ||
| select_dataclip_body(result.step.output_dataclip_id) | ||
| end | ||
|
|
||
| @tag :integration | ||
| @tag timeout: 10_000 | ||
| test "job that uses too much memory very quickly is properly killed", %{ | ||
| uri: uri | ||
| } do | ||
| job_body = """ | ||
| fn(state => { | ||
| const arr = []; | ||
| while (true) { arr.push(new Array(1e6).fill('x')); } | ||
| return state; | ||
| }); | ||
| """ | ||
|
|
||
| result = run_single_step_workflow(uri, %{"x" => 1}, job_body) | ||
|
|
||
| assert result.run.state == :killed | ||
| assert result.run.error_type == "OOMError" | ||
| end | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # Helpers | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| @doc """ | ||
| Creates a single-step webhook workflow, fires it, waits for completion, | ||
| and returns a map with the fully-loaded run, step, and work_order. | ||
|
|
||
| ## Parameters | ||
| - `uri` — the endpoint base URL (from setup_all) | ||
| - `webhook_body` — the JSON map to POST to the webhook | ||
| - `job_body` — the JavaScript expression string for the job | ||
| - `opts` — keyword list of options: | ||
| - `:adaptor` — adaptor string (default `"@openfn/language-common@latest"`) | ||
| - `:timeout` — ms to wait for run completion (default `15_000`) | ||
|
|
||
| ## Returns | ||
| %{run: run, step: step, work_order: work_order} | ||
| """ | ||
| def run_single_step_workflow(uri, webhook_body, job_body, opts \\ []) do | ||
| adaptor = Keyword.get(opts, :adaptor, "@openfn/language-common@latest") | ||
| timeout = Keyword.get(opts, :timeout, 15_000) | ||
|
|
||
| project = insert(:project) | ||
|
|
||
| webhook_trigger = build(:trigger, type: :webhook, enabled: true) | ||
|
|
||
| job = | ||
| build(:job, | ||
| adaptor: adaptor, | ||
| body: job_body, | ||
| name: "test-job" | ||
| ) | ||
|
|
||
| workflow = | ||
| build(:workflow, project: project) | ||
| |> with_trigger(webhook_trigger) | ||
| |> with_job(job) | ||
| |> with_edge({webhook_trigger, job}, condition_type: :always) | ||
| |> insert() | ||
|
|
||
| Snapshot.create(workflow) | ||
|
|
||
| # Fire the webhook | ||
| response = | ||
| Tesla.client( | ||
| [ | ||
| {Tesla.Middleware.BaseUrl, uri}, | ||
| Tesla.Middleware.JSON | ||
| ], | ||
| {Tesla.Adapter.Finch, name: Lightning.Finch} | ||
| ) | ||
| |> Tesla.post!("/i/#{webhook_trigger.id}", webhook_body) | ||
|
|
||
| assert response.status == 200 | ||
| assert %{"work_order_id" => workorder_id} = response.body | ||
|
|
||
| assert %{runs: [run]} = | ||
| WorkOrders.get(workorder_id, include: [:runs]) | ||
|
|
||
| # Subscribe and wait for completion | ||
| Events.subscribe(run) | ||
| run_id = run.id | ||
|
|
||
| assert_receive %Events.RunUpdated{run: %{id: ^run_id, state: final_state}} | ||
| when final_state in [ | ||
| :success, | ||
| :failed, | ||
| :crashed, | ||
| :killed, | ||
| :lost | ||
| ], | ||
| timeout | ||
|
|
||
| # Reload with associations | ||
| run = | ||
| Runs.get(run.id, | ||
| include: [steps: [:job, :log_lines], work_order: :workflow] | ||
| ) | ||
|
|
||
| [step] = run.steps | ||
|
|
||
| work_order = WorkOrders.get(workorder_id) | ||
|
|
||
| %{run: run, step: step, work_order: work_order} | ||
| end | ||
|
|
||
| defp start_runtime_manager(_context \\ nil) do | ||
| opts = | ||
| Application.get_env(:lightning, RuntimeManager) | ||
| |> Keyword.merge( | ||
| name: LostRunRedTeamRuntimeManager, | ||
| start: true, | ||
| endpoint: LightningWeb.Endpoint, | ||
| log: :debug, | ||
| port: Enum.random(2223..3333), | ||
| worker_secret: Lightning.Config.worker_secret() | ||
| ) | ||
|
|
||
| start_supervised!({RuntimeManager, opts}, restart: :transient) | ||
| end | ||
|
|
||
| defp select_dataclip_body(uuid) do | ||
| from(d in Invocation.Dataclip, | ||
| where: d.id == ^uuid, | ||
| select: d.body | ||
| ) | ||
| |> Repo.one!() | ||
| end | ||
| end |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
42is valid JSON AFAIK. Why do we need to ensure a map? Like where does Lightning actually care? Isn't the value arbitrary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be the philosophical question :-)
Since the very beginning, AFAIK, we've said that state is a JSON object (not just JSON) so I'm not actually sure what might break if we allowed
nullortrueor42. Let's see what @stuartc has to say here!