Skip to content

Commit 28ebecc

Browse files
Release: worker@1.22.1 cli@1.30.3 (#1314)
* Fix worker crash caused by async promise antipattern and event ordering (#1311) * fix(ws-worker): remove async promise antipattern in run-log.ts The non-batch log path wrapped an async function in `new Promise(async (resolve) => { ... })`. If sendEvent rejected (e.g. channel timeout), the outer Promise never settled and the rejection was unhandled. This was the direct cause of the worker crash: LightningTimeoutError on run:log became an uncaught exception that killed the container. The function is already async so the wrapper was unnecessary — replaced with a plain for-loop that properly propagates rejections. * fix(ws-worker): remove async promise antipattern in try-with-backoff.ts Same `new Promise(async ...)` antipattern as run-log.ts. If the isCancelled callback or any other code in the catch block threw, the error became an unhandled rejection instead of propagating to the caller. Replaced with an inner async function that returns its promise directly. setTimeout retry replaced with awaited promise to keep the flow async-native. * fix(ws-worker): remove async promise antipattern in destroy.ts Same `new Promise(async (resolve) => { ... })` pattern — if engine.destroy() or waitForRunsAndClaims rejected, the outer promise never settled. Replaced with an async IIFE so errors propagate through Promise.all to the caller. * fix(engine-multi): emit compilation log before workflow-error During a compilation failure, engine-multi emitted WORKFLOW_ERROR (from the worker:error handler) before the "Error occurred during compilation" WORKFLOW_LOG (from the .catch handler). The ws-worker tears down the channel on WORKFLOW_ERROR, so the subsequent log push had nowhere to go — triggering the LightningTimeoutError that crashed the container. Moved the compilation log into the worker:error handler so it emits before the error event. Guarded the .catch handler's log with !didError to avoid duplication. Added COMPILE_START/COMPLETE events to mock-run.ts to match real worker behavior. * chore: add changesets for worker crash fixes * chore: exclude engine-multi tmp from pnpm workspace Test fixture directories under packages/engine-multi/tmp/ were being picked up as workspace packages by the packages/** glob, pulling in phantom dependencies (ava@6.x and its full transitive tree) that caused persistent lockfile churn. * remove duplicate log, tidy types * update changelogs * tidy test * revert diff on destroy * keep destroy change after all and fix test * relax runtime test it just went flaky --------- Co-authored-by: Joe Clark <jclark@openfn.org> * Compile errors step name (#1313) * engine: report compile errors with step name, not id * changeset * remove debug code * Lazy state errors (#1312) * compiler: improve error messages for lazy state * compiler: report position on lazy state reporting * update extra errors * update changeset * types * versions --------- Co-authored-by: Stuart Corbishley <corbish@gmail.com>
1 parent f33a93d commit 28ebecc

27 files changed

Lines changed: 295 additions & 475 deletions

File tree

‎packages/cli/CHANGELOG.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @openfn/cli
22

3+
## 1.30.3
4+
5+
### Patch Changes
6+
7+
- c687ef5: Improve messaging and position reporting in lazy state compilation errors
8+
- Updated dependencies [c687ef5]
9+
- @openfn/compiler@1.2.3
10+
311
## 1.30.2
412

513
### Patch Changes

‎packages/cli/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/cli",
3-
"version": "1.30.2",
3+
"version": "1.30.3",
44
"description": "CLI devtools for the OpenFn toolchain",
55
"engines": {
66
"node": ">=18",

‎packages/cli/src/util/abort.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,35 @@ class AbortError extends Error {
77
handled = true;
88
}
99

10+
interface CLIFriendlyError extends Error {
11+
fix?: string;
12+
details?: string;
13+
}
14+
1015
// This is always the CLI logger, can I trap it?
1116
export default (
1217
logger: Logger,
1318
reason: string,
14-
error?: Error,
19+
error?: CLIFriendlyError,
1520
help?: string
1621
) => {
1722
const e = new AbortError(reason);
1823
logger.break();
1924
logger.error(reason);
2025
if (error) {
2126
logger.error(error.message);
27+
logger.break();
28+
29+
if (error.details) {
30+
logger.error('ERROR DETAILS:');
31+
logger.error(error.details);
32+
logger.break();
33+
}
34+
if (error.fix) {
35+
logger.error('FIX HINT:');
36+
logger.error(error.fix);
37+
logger.break();
38+
}
2239
}
2340
if (help) {
2441
logger.always(help);

‎packages/compiler/CHANGELOG.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# @openfn/compiler
22

3+
## 1.2.3
4+
5+
### Patch Changes
6+
7+
- c687ef5: Improve messaging and position reporting in lazy state compilation errors
8+
39
## 1.2.2
410

511
### Patch Changes

‎packages/compiler/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/compiler",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Compiler and language tooling for openfn jobs.",
55
"author": "Open Function Group <admin@openfn.org>",
66
"license": "ISC",

‎packages/compiler/src/transforms/lazy-state.ts‎

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,32 @@ import type { NodePath } from 'ast-types/lib/node-path';
1111
import type { Transformer } from '../transform';
1212
import IgnoreRules from '../transform-ignore';
1313

14+
type LazyStateErrorOptions = {
15+
details?: string;
16+
fix?: string;
17+
pos?: any;
18+
};
19+
20+
export class LazyStateError extends Error {
21+
fix?: string;
22+
details?: string;
23+
pos?: any;
24+
25+
constructor(
26+
message: string,
27+
{ details, fix, pos }: LazyStateErrorOptions = {}
28+
) {
29+
const posStr = (pos && `(${pos.start.line}:${pos.start.column})`) ?? '';
30+
super(`Lazy State Error: ${message} ${posStr}`);
31+
32+
this.fix = fix;
33+
this.details = details;
34+
const { start, end } = pos;
35+
this.pos = { start, end };
36+
delete this.stack;
37+
}
38+
}
39+
1440
// Walk up the AST and work out where the parent arrow function should go
1541
const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
1642
let root = path;
@@ -23,9 +49,12 @@ const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
2349
root = root.parent;
2450

2551
// if this is any kind of statement, we should throw
26-
// TODO we may relax this, see https://github.com/OpenFn/kit/issues/660
2752
if (n.Statement.check(root.node) || n.Declaration.check(root.node)) {
28-
throw new Error(`invalid state operator: must be inside an expression`);
53+
throw new LazyStateError('Must be inside an operation', {
54+
pos: path.node.loc,
55+
details:
56+
'The Lazy State operation must be used inside a top-level operation, like fn(). It cannot be used inside a regular JavaScript statement because no valid state reference is available.',
57+
});
2958
}
3059
}
3160

@@ -47,9 +76,9 @@ const ensureParentArrow = (path: NodePath<n.MemberExpression>) => {
4776
}
4877
} else {
4978
// Actually I don't think we'll ever get here
50-
throw new Error(
51-
`invalid state operator: must be be passed as an argument to an operator`
52-
);
79+
throw new LazyStateError('must be passed as an argument to an operator', {
80+
pos: path.node.loc,
81+
});
5382
}
5483
};
5584

@@ -66,11 +95,11 @@ const isOpenFunction = (path: NodePath) => {
6695
// We already have a valid open function here
6796
return true;
6897
}
69-
throw new Error(
70-
`invalid state operator: parameter "${name}" should be called "state"`
71-
);
98+
throw new LazyStateError(`parameter "${name}" should be called "state"`, {
99+
pos: path.node.loc,
100+
});
72101
}
73-
throw new Error('invalid state operator: parent has wrong arity');
102+
throw new LazyStateError('parent has wrong arity', { pos: path.node.loc });
74103
}
75104

76105
// if we get here, then path is:

‎packages/compiler/test/transforms/lazy-state.test.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { namedTypes, NodePath, builders as b } from 'ast-types';
55
import parse from '../../src/parse';
66

77
import transform from '../../src/transform';
8-
import visitors from '../../src/transforms/lazy-state';
8+
import visitors, { LazyStateError } from '../../src/transforms/lazy-state';
99

1010
test('convert a simple dollar reference', (t) => {
1111
const ast = parse('get($.data)');
@@ -86,7 +86,8 @@ test('throw if a $ is already inside a non-compatible arrow (state name)', (t) =
8686
const ast = parse(src);
8787

8888
t.throws(() => transform(ast, [visitors]), {
89-
message: `invalid state operator: parameter "s" should be called "state"`,
89+
instanceOf: LazyStateError,
90+
message: /parameter "s" should be called "state" \(1:4\)/i,
9091
});
9192
});
9293

@@ -96,7 +97,8 @@ test('throw if a $ is already inside a non-compatible arrow (arity)', (t) => {
9697
const ast = parse(src);
9798

9899
t.throws(() => transform(ast, [visitors]), {
99-
message: 'invalid state operator: parent has wrong arity',
100+
instanceOf: LazyStateError,
101+
message: /parent has wrong arity \(1:4\)/i,
100102
});
101103
});
102104

@@ -106,7 +108,8 @@ test('throw if $ is not inside an operation', (t) => {
106108
const ast = parse(src);
107109

108110
t.throws(() => transform(ast, [visitors]), {
109-
message: 'invalid state operator: must be inside an expression',
111+
instanceOf: LazyStateError,
112+
message: /must be inside an operation \(1:10\)/i,
110113
});
111114
});
112115

@@ -116,7 +119,8 @@ test('throw if $ is on the left hand side of an assignment', (t) => {
116119
const ast = parse(src);
117120

118121
t.throws(() => transform(ast, [visitors]), {
119-
message: 'invalid state operator: must be inside an expression',
122+
instanceOf: LazyStateError,
123+
message: /must be inside an operation \(1:0\)/i,
120124
});
121125
});
122126

@@ -126,7 +130,8 @@ test('throw if $ is on the left hand side of a nested assignment', (t) => {
126130
const ast = parse(src);
127131

128132
t.throws(() => transform(ast, [visitors]), {
129-
message: 'invalid state operator: must be inside an expression',
133+
instanceOf: LazyStateError,
134+
message: /must be inside an operation \(1:11\)/i,
130135
});
131136
});
132137

@@ -136,7 +141,8 @@ test('throw if $ is on the left hand side of a multi assignment', (t) => {
136141
const ast = parse(src);
137142

138143
t.throws(() => transform(ast, [visitors]), {
139-
message: 'invalid state operator: must be inside an expression',
144+
instanceOf: LazyStateError,
145+
message: /must be inside an operation \(1:10\)/i,
140146
});
141147
});
142148

‎packages/engine-multi/CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# engine-multi
22

3+
## 1.10.5
4+
5+
### Patch Changes
6+
7+
- 6fd3942: Emit compilation failure log before workflow-error event. Previously the error
8+
event arrived first, causing the worker to tear down the channel before the
9+
log could be delivered.
10+
- 32b43cb: When reporting compilation errors, prefer the step name to the id
11+
- Updated dependencies [c687ef5]
12+
- @openfn/compiler@1.2.3
13+
314
## 1.10.4
415

516
### Patch Changes

‎packages/engine-multi/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openfn/engine-multi",
3-
"version": "1.10.4",
3+
"version": "1.10.5",
44
"description": "Multi-process runtime engine",
55
"main": "dist/index.js",
66
"type": "module",

‎packages/engine-multi/src/api/execute.ts‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ const execute = async (context: ExecutionContext) => {
131131
},
132132
[workerEvents.ERROR]: (evt: workerEvents.ErrorEvent) => {
133133
didError = true;
134+
if (compileStatus === 'started') {
135+
log(context, {
136+
type: workerEvents.LOG,
137+
workflowId: state.plan.id!,
138+
threadId: evt.threadId || '-',
139+
log: {
140+
level: 'info',
141+
message: ['Error occurred during compilation'],
142+
name: 'RTE',
143+
time: timestamp().toString(),
144+
},
145+
});
146+
}
134147
error(context, {
135148
workflowId: state.plan.id,
136149
error: evt.error,
@@ -155,21 +168,6 @@ const execute = async (context: ExecutionContext) => {
155168
events,
156169
workerOptions
157170
).catch(async (e: any) => {
158-
if (compileStatus === 'started') {
159-
// Try and alert users that the error occurred at compile-time
160-
// Not super keen on adding this down in the engine but it may help app users
161-
await log(context, {
162-
type: workerEvents.LOG,
163-
workflowId: state.plan.id!,
164-
threadId: '-',
165-
log: {
166-
level: 'info',
167-
message: [`Error occurred during compilation`],
168-
name: 'RTE',
169-
time: timestamp().toString(),
170-
},
171-
});
172-
}
173171
// An error should:
174172
// a) emit an error event (and so be handled by the error() function
175173
// b) reject the task in the pool

0 commit comments

Comments
 (0)