Commit d9d9e5c
feat: add running operations tracking to prevent unsafe termination (#10)
## Running Operations Tracking
### **What We Built:**
1. hasRunningOperations() method - Clean API to check if critical
operations are running
2. Internal tracking system - addRunningOperation() /
removeRunningOperation() with Set-based storage
3. Automatic cleanup - try/finally blocks ensure operations are removed
even on errors
### **Where We Added Tracking (Only 2 Operations):**
✅ ctx.step:
• **Executes user code**
• **Critical for workflow state** - if interrupted, workflow state
becomes inconsistent
✅ ctx.waitForCondition:
• **Executes user condition checks**
• **Critical for workflow logic** - if interrupted, condition evaluation
becomes unreliable
### **Why NOT Other Operations:**
❌ ctx.runInChildContext - Safe to interrupt, just context management
❌ ctx.wait - Simple timer, no user code execution
❌ ctx.createCallback - Just creates callback config, no execution
❌ ctx.waitForCallback - Uses ctx.step internally, so already tracked
automatically
❌ ctx.map/parallel/executeConcurrently - Use ctx.runInChildContext
internally, which is safe to interrupt
### **Key Insight:**
We only needed to add tracking to the 2 fundamental operations that
execute user code:
• ctx.step - Direct user code execution
• ctx.waitForCondition - User condition evaluation
All other operations either:
• Are safe to interrupt (context/config operations)
• Delegate to tracked operations (so get protection automatically)
### **The Result:**
```typescript
// Safe termination check
if (durableContext.hasRunningOperations()) {
return; // Critical user code is running - wait
}
terminate(); // Safe to terminate
```
Surgical precision - Only the 2 operations that actually execute user
code are tracked! 🎯
Co-authored-by: Pooya Paridel <parpooya@amazon.com>1 parent 62c11d6 commit d9d9e5c
File tree
7 files changed
+65
-6
lines changed- packages/lambda-durable-functions-sdk-js/src
- context/durable-context
- handlers
- step-handler
- wait-for-condition-handler
- types
7 files changed
+65
-6
lines changedLines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
138 | 138 | | |
139 | 139 | | |
140 | 140 | | |
| 141 | + | |
| 142 | + | |
141 | 143 | | |
142 | 144 | | |
143 | 145 | | |
144 | 146 | | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
145 | 156 | | |
146 | 157 | | |
147 | 158 | | |
| |||
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
63 | 77 | | |
64 | 78 | | |
65 | 79 | | |
| |||
71 | 85 | | |
72 | 86 | | |
73 | 87 | | |
| 88 | + | |
| 89 | + | |
74 | 90 | | |
75 | 91 | | |
76 | 92 | | |
| |||
177 | 193 | | |
178 | 194 | | |
179 | 195 | | |
| 196 | + | |
180 | 197 | | |
181 | 198 | | |
182 | 199 | | |
| |||
185 | 202 | | |
186 | 203 | | |
187 | 204 | | |
| 205 | + | |
| 206 | + | |
188 | 207 | | |
189 | 208 | | |
190 | 209 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
| |||
Lines changed: 15 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
| 51 | + | |
| 52 | + | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
| |||
166 | 168 | | |
167 | 169 | | |
168 | 170 | | |
| 171 | + | |
| 172 | + | |
169 | 173 | | |
170 | 174 | | |
171 | 175 | | |
| |||
205 | 209 | | |
206 | 210 | | |
207 | 211 | | |
| 212 | + | |
| 213 | + | |
208 | 214 | | |
209 | 215 | | |
210 | 216 | | |
| |||
248 | 254 | | |
249 | 255 | | |
250 | 256 | | |
251 | | - | |
252 | | - | |
253 | | - | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
254 | 266 | | |
255 | 267 | | |
256 | 268 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| 84 | + | |
| 85 | + | |
84 | 86 | | |
85 | 87 | | |
86 | 88 | | |
| |||
Lines changed: 15 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
| 46 | + | |
| 47 | + | |
46 | 48 | | |
47 | 49 | | |
48 | 50 | | |
| |||
111 | 113 | | |
112 | 114 | | |
113 | 115 | | |
| 116 | + | |
| 117 | + | |
114 | 118 | | |
115 | 119 | | |
116 | 120 | | |
| |||
150 | 154 | | |
151 | 155 | | |
152 | 156 | | |
| 157 | + | |
| 158 | + | |
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| |||
215 | 221 | | |
216 | 222 | | |
217 | 223 | | |
218 | | - | |
219 | | - | |
220 | | - | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
221 | 233 | | |
222 | 234 | | |
223 | 235 | | |
| |||
Lines changed: 1 addition & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
87 | 87 | | |
88 | 88 | | |
89 | 89 | | |
| 90 | + | |
90 | 91 | | |
91 | 92 | | |
92 | 93 | | |
| |||
0 commit comments