Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/2957#discussion_r19455903
  
    --- Diff: core/src/main/scala/org/apache/spark/FutureAction.scala ---
    @@ -210,7 +210,9 @@ class ComplexFutureAction[T] extends FutureAction[T] {
           } catch {
             case e: Exception => p.failure(e)
           } finally {
    -        thread = null
    +        ComplexFutureAction.this.synchronized {
    --- End diff --
    
    Here is the codes of `cancel`:
    ```Scala
    1 override def cancel(): Unit = this.synchronized {
    2    _cancelled = true
    3    if (thread != null) {
    4      thread.interrupt()
    5    }
    6  }
    ```
    Without `ComplexFutureAction.this.synchronized`, the codes can run in the 
following order:
    
    1. In thread T1, `cancel` is called.
    2. After T1 finishes L3, it's suspended.
    3. Assume T2 is the thread that runs the action. Now T2 is set `thread` to 
null.
    4. T1 is resumed and runs L4 (thread.interrupt()), because `thread` is 
null, it will throw a NPE.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to