You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "sadikovi (via GitHub)" <gi...@apache.org> on 2026/04/02 03:40:51 UTC
[PR] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
sadikovi opened a new pull request, #55151:
URL: https://github.com/apache/spark/pull/55151
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on code in PR #55151:
URL: https://github.com/apache/spark/pull/55151#discussion_r3043048830
##########
core/src/main/scala/org/apache/spark/TaskContextImpl.scala:
##########
@@ -166,6 +183,14 @@ private[spark] class TaskContextImpl(
}
}
+ private def invokeTaskInterruptListeners(error: Throwable): Unit = {
+ // It is safe to access the reference to `onInterruptCallbacks` without holding the TaskContext
+ // lock. `invokeListeners()` acquires the lock before accessing the contents.
+ invokeListeners(onInterruptCallbacks, "TaskInterruptListener", Option(error)) {
Review Comment:
This PR reuses the existing machinery of invoking listeners, I decided to keep the code as is to simplify the changes in the PR.
##########
core/src/test/scala/org/apache/spark/scheduler/TaskContextSuite.scala:
##########
@@ -150,6 +150,32 @@ class TaskContextSuite extends SparkFunSuite with BeforeAndAfter with LocalSpark
assert(e.getMessage.contains("exception in task"))
}
+ test("all TaskInterruptListeners should be called even if some fail") {
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on PR #55151:
URL: https://github.com/apache/spark/pull/55151#issuecomment-4210283688
Doc build error was fixed.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on code in PR #55151:
URL: https://github.com/apache/spark/pull/55151#discussion_r3043046952
##########
core/src/main/scala/org/apache/spark/TaskContext.scala:
##########
@@ -169,6 +169,17 @@ abstract class TaskContext extends Serializable {
})
}
+ /**
+ * Adds a listener to be executed when the task is interrupted.
Review Comment:
Updated.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on PR #55151:
URL: https://github.com/apache/spark/pull/55151#issuecomment-4203517074
@cloud-fan @HyukjinKwon Can you review again please?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #55151:
URL: https://github.com/apache/spark/pull/55151#discussion_r3042700338
##########
core/src/main/scala/org/apache/spark/TaskContext.scala:
##########
@@ -169,6 +169,17 @@ abstract class TaskContext extends Serializable {
})
}
+ /**
+ * Adds a listener to be executed when the task is interrupted.
Review Comment:
The implementation fires the listener immediately if the task is already interrupted (matching the completion/failure pattern), but the Javadoc doesn't mention this. Consider adding: "Adding a listener to an already interrupted task will result in that listener being called immediately." Compare the docs for `addTaskCompletionListener` (line 119) and `addTaskFailureListener` (line 152).
##########
core/src/main/scala/org/apache/spark/TaskContextImpl.scala:
##########
@@ -272,6 +297,7 @@ private[spark] class TaskContextImpl(
private[spark] override def markInterrupted(reason: String): Unit = {
reasonIfKilled = Some(reason)
+ invokeTaskInterruptListeners(new TaskKilledException(reason))
Review Comment:
`markInterrupted` was previously a simple setter that could never throw. Now it invokes listeners via `invokeListeners`, which throws `TaskCompletionListenerException` if any listener fails. The call chain `CoarseGrainedExecutorBackend.receive(KillTask)` → `Executor.killTask()` → `TaskRunner.kill()` → `Task.kill()` → `markInterrupted()` has no exception handling for this. A failing interrupt listener would propagate an exception to the executor's RPC handler thread.
Should `markInterrupted` catch and log `TaskCompletionListenerException` rather than propagating it? The task is already marked as failed via the `markTaskFailed(e)` call inside `invokeListeners`'s catch block, so the exception has already done its job by the time it propagates.
##########
core/src/main/scala/org/apache/spark/util/taskListeners.scala:
##########
@@ -44,6 +44,16 @@ trait TaskFailureListener extends EventListener {
def onTaskFailure(context: TaskContext, error: Throwable): Unit
}
+/**
+ * :: DeveloperApi ::
+ *
+ * Listener providing a callback function to invoke when a task's execution is interrupted.
+ */
+@DeveloperApi
+trait TaskInterruptListener extends EventListener {
+ def onTaskInterrupted(context: TaskContext): Unit
Review Comment:
`TaskFailureListener.onTaskFailure(context, error)` passes the error. The interrupt listener only receives `context`, and the kill reason is only accessible via `getKillReason()` which is `private[spark]`. Since this is a `@DeveloperApi` interface, external users can't access the reason in their callback. This limits usefulness for logging or reason-specific cleanup. Consider either adding the reason as a parameter or making `getKillReason()` public.
##########
core/src/test/scala/org/apache/spark/scheduler/TaskContextSuite.scala:
##########
@@ -150,6 +150,32 @@ class TaskContextSuite extends SparkFunSuite with BeforeAndAfter with LocalSpark
assert(e.getMessage.contains("exception in task"))
}
+ test("all TaskInterruptListeners should be called even if some fail") {
Review Comment:
Consider adding a test that calls `markInterrupted` first, then registers a listener, and verifies the listener fires immediately. This late-registration path is implemented (via the `reasonIfKilled` check in `addTaskInterruptListener`) but not covered.
##########
core/src/main/scala/org/apache/spark/TaskContextImpl.scala:
##########
@@ -166,6 +183,14 @@ private[spark] class TaskContextImpl(
}
}
+ private def invokeTaskInterruptListeners(error: Throwable): Unit = {
+ // It is safe to access the reference to `onInterruptCallbacks` without holding the TaskContext
+ // lock. `invokeListeners()` acquires the lock before accessing the contents.
+ invokeListeners(onInterruptCallbacks, "TaskInterruptListener", Option(error)) {
Review Comment:
`invokeListeners` uses a shared `listenerInvocationThread` flag to enforce sequential execution across all listener types. If `markInterrupted` is called from the kill thread while the task thread is running completion or failure listeners, `invokeListeners(onInterruptCallbacks, ...)` sees `listenerInvocationThread.nonEmpty` and returns immediately — interrupt listeners are silently skipped. The reverse scenario (task thread trying to run failure listeners while the kill thread runs interrupt listeners) is also possible.
The existing code avoids this because `markTaskCompleted`/`markTaskFailed` are only called from the task thread. Is this race acceptable given that interrupt + completion overlap implies the task is already finishing?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on PR #55151:
URL: https://github.com/apache/spark/pull/55151#issuecomment-4206268587
LGTM, please fix the doc build error
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "sadikovi (via GitHub)" <gi...@apache.org>.
sadikovi commented on PR #55151:
URL: https://github.com/apache/spark/pull/55151#issuecomment-4191226479
Thanks @HyukjinKwon! Yes, that file was added by mistake, I removed it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon commented on PR #55151:
URL: https://github.com/apache/spark/pull/55151#issuecomment-4210300469
Merged to master.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications [spark]
Posted by "HyukjinKwon (via GitHub)" <gi...@apache.org>.
HyukjinKwon closed pull request #55151: [SPARK-56330][CORE] Add TaskInterruptListener to TaskContext for interrupt notifications
URL: https://github.com/apache/spark/pull/55151
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org