FrankYang0529 commented on code in PR #74690: URL: https://github.com/apache/airflow/pull/74690#discussion_r4332520808
########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/Context.kt: ########## @@ -127,10 +129,49 @@ data class Context( /** Registration of the executing task; resolves wired data inputs. */ internal var taskDef: TaskDef? = null + internal var cancellation = Cancellation() + + /** + * Whether Airflow has asked this task to stop. + * + * Airflow asks a running task to stop when, for example, a user marks its task + * instance as failed. The supervisor, which is the Airflow process that started + * the JVM, then sends the JVM SIGTERM. If the JVM is still running after a + * grace period, the supervisor sends SIGKILL. + * + * Once a task has called this method or [awaitCancellation], the SDK catches + * the first SIGTERM or SIGINT. The SDK records the stop request instead of + * letting the JVM exit, so the task can stop its work before SIGKILL arrives. + * From then on the task must keep checking until it returns. + * + * If a task has called neither method, or if a second signal arrives, the JVM + * handles the signal as it would without the SDK. By default the JVM runs its + * shutdown hooks and exits. + * + * A context made with [copy] does not see the stop request. Review Comment: Changed to share the same `Cancellation` instance for the `Context`. ########## java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/Context.kt: ########## @@ -127,10 +129,49 @@ data class Context( /** Registration of the executing task; resolves wired data inputs. */ internal var taskDef: TaskDef? = null + internal var cancellation = Cancellation() Review Comment: Thanks for the review. I agree that only the instance from `runTaskAndReport` was connected to the signal trap, so a caller that forgot to pass it would get an unconnected one without any error. I used another way further than making the parameter required. A task JVM runs one task, and SIGTERM reaches the whole JVM anyway. The SDK therefore keeps the running task's `Cancellation` in one JVM-wide field, `Cancellation.running`. `runTaskAndReport` creates the instance, stores it in that field before it opens the trap. Set the field back to null when it is done with the task. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
