FrankYang0529 commented on code in PR #75538:
URL: https://github.com/apache/airflow/pull/75538#discussion_r4572741784


##########
java-sdk/sdk/src/main/kotlin/org/apache/airflow/sdk/execution/Cancellation.kt:
##########
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.airflow.sdk.execution
+
+import org.apache.airflow.sdk.Context
+import org.apache.airflow.sdk.StoppableTask
+import sun.misc.Signal
+import sun.misc.SignalHandler
+import java.time.Duration
+import java.util.concurrent.CompletableFuture
+import java.util.concurrent.CountDownLatch
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.atomic.AtomicBoolean
+import java.util.concurrent.atomic.AtomicReference
+
+/**
+ * Records whether Airflow has asked the running task to stop. For a 
[StoppableTask], also catches the
+ * signal that carries that request.
+ */
+internal class Cancellation : AutoCloseable {
+  private val claimed = AtomicBoolean()
+  private val requested = CountDownLatch(1)
+  private val signalHandled = CompletableFuture<Unit>()
+
+  @Volatile
+  private var held = false
+
+  @Volatile
+  private var trap: AutoCloseable? = null
+
+  @Volatile
+  private var hook: (() -> Unit)? = null
+
+  fun check(): Boolean {
+    throwIfTaskCannotSeeRequest()
+    return requested.count == 0L
+  }
+
+  fun await(timeout: Duration): Boolean {
+    throwIfTaskCannotSeeRequest()
+    return requested.await(TimeUnit.NANOSECONDS.convert(timeout), 
TimeUnit.NANOSECONDS)
+  }
+
+  private fun throwIfTaskCannotSeeRequest() {
+    check(held) {
+      "Only a StoppableTask can see a stop request. Implement StoppableTask, 
or give an annotated task an @Airflow.OnKill method"
+    }
+  }
+
+  /** Catches the first stop signal for [task] until [close], or throws 
[CannotCatchStopSignalsException]. */
+  fun hold(
+    task: StoppableTask,
+    context: Context,
+  ) {
+    val classLoader = Thread.currentThread().contextClassLoader
+    hook = {
+      // On Java 11 and 17, a signal handler thread has no context class 
loader, and libraries can need one.
+      Thread.currentThread().contextClassLoader = classLoader
+      task.onKill(context)
+    }
+    // Set first: a signal can run onKill, which may check for a stop request, 
before trapStopSignals returns.
+    held = true
+    trap = trapStopSignals(::takeSignal)
+      ?: throw CannotCatchStopSignalsException(
+        "The SDK cannot catch SIGTERM or SIGINT in this JVM, so the task 
cannot see a stop request. The JVM might run with -Xrs, " +
+          "or without the jdk.unsupported module",
+      )
+  }
+
+  /**
+   * Stops later signals from calling onKill and waits for a call that already 
started.
+   * The wait has no timeout because the supervisor's SIGKILL ends a call that 
hangs.
+   */
+  fun finish() {
+    hook = null
+    // join() keeps waiting if the thread is interrupted, and a task can leave 
the thread's interrupt flag set.
+    if (claimed.get()) signalHandled.join()
+  }

Review Comment:
   Once the supervisor has set `_terminal_state` to `SERVER_TERMINATED`, it 
drops the task's result (`_handle_task_state` and `_send_terminal_state_msg` 
skip it), so an earlier report changes nothing. When it stops a task for 
`execution_timeout` and gets no result, it marks the task UP_FOR_RETRY or 
FAILED from the exit code, which is what a task that throws on the stop request 
would report. Python's `_on_term` also calls `on_kill()` inside the SIGTERM 
handler, so a hung `on_kill` holds back the result there too, and I'd keep the 
Java SDK the same.



-- 
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]

Reply via email to