Commit 4db309d
[SPARK-56330][CORE][FOLLOWUP] Use dedicated loop for TaskInterruptListeners to avoid blocking completion listeners
### What changes were proposed in this pull request?
Followup to #55151.
Changed `invokeTaskInterruptListeners` in `TaskContextImpl` to use a dedicated loop instead of routing through the shared `invokeListeners()` method. Also removed the now-unnecessary `markTaskFailedOnListenerError` parameter from `invokeListeners` since it is always `true` (only completion and failure listeners use this method now).
### Why are the changes needed?
The current implementation routes `TaskInterruptListener`s through `invokeListeners()`, which uses `listenerInvocationThread` to serialize all listener invocations (completion, failure, and interrupt). This creates a problem when `markInterrupted()` is called on the kill thread while completion listeners need to run on the task thread:
1. The kill thread enters `invokeListeners()` for interrupt callbacks and acquires `listenerInvocationThread`.
2. While the interrupt listener runs, the task thread completes and calls `markTaskCompleted()` → `invokeTaskCompletionListeners()` → `invokeListeners()` for completion callbacks.
3. `invokeListeners()` sees `listenerInvocationThread` is held by the kill thread and returns immediately — **silently skipping all completion listeners**.
This causes resource leaks because cleanup logic registered via `addTaskCompletionListener` (e.g., closing file handles, releasing caches, freeing native memory) never executes.
The fix uses a dedicated loop for interrupt listeners with independent serialization (synchronized access to the callback stack, but no `listenerInvocationThread` gate), so interrupt listeners and completion/failure listeners can run on different threads without blocking each other.
### Does this PR introduce _any_ user-facing change?
No.
### How was this patch tested?
Added a regression test `"SPARK-56330: task completion during interrupt listener execution"` that:
1. Registers a completion listener and a blocking interrupt listener.
2. Interrupts the task from a separate thread and waits for the interrupt listener to start.
3. Marks the task completed on the main thread while the interrupt listener is still running.
4. Verifies the completion listener was called (it would be silently skipped with the shared `invokeListeners()` approach).
### Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code
Closes #55292 from cloud-fan/SPARK-56330-followup.
Authored-by: Wenchen Fan <wenchen@databricks.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>1 parent 638ca41 commit 4db309d
2 files changed
Lines changed: 84 additions & 45 deletions
File tree
- core/src
- main/scala/org/apache/spark
- test/scala/org/apache/spark/scheduler
Lines changed: 49 additions & 45 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
| 80 | + | |
81 | 81 | | |
82 | 82 | | |
83 | 83 | | |
| |||
131 | 131 | | |
132 | 132 | | |
133 | 133 | | |
134 | | - | |
135 | | - | |
136 | | - | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
137 | 137 | | |
138 | | - | |
139 | | - | |
| 138 | + | |
140 | 139 | | |
141 | 140 | | |
142 | 141 | | |
| |||
172 | 171 | | |
173 | 172 | | |
174 | 173 | | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
| 174 | + | |
180 | 175 | | |
181 | 176 | | |
182 | 177 | | |
183 | 178 | | |
184 | 179 | | |
185 | 180 | | |
186 | 181 | | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
| 182 | + | |
192 | 183 | | |
193 | 184 | | |
194 | 185 | | |
195 | 186 | | |
196 | 187 | | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
208 | 219 | | |
209 | 220 | | |
210 | 221 | | |
211 | 222 | | |
212 | 223 | | |
213 | 224 | | |
214 | | - | |
215 | | - | |
| 225 | + | |
216 | 226 | | |
217 | 227 | | |
218 | 228 | | |
| |||
255 | 265 | | |
256 | 266 | | |
257 | 267 | | |
258 | | - | |
259 | | - | |
| 268 | + | |
260 | 269 | | |
261 | 270 | | |
262 | 271 | | |
| |||
290 | 299 | | |
291 | 300 | | |
292 | 301 | | |
293 | | - | |
294 | | - | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | | - | |
299 | | - | |
300 | | - | |
301 | | - | |
302 | | - | |
303 | | - | |
304 | | - | |
305 | | - | |
306 | | - | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
307 | 311 | | |
308 | 312 | | |
309 | 313 | | |
| |||
Lines changed: 35 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
199 | 199 | | |
200 | 200 | | |
201 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
202 | 237 | | |
203 | 238 | | |
204 | 239 | | |
| |||
0 commit comments