JDK-8016247 : ThreadPoolExecutor may interrupt the wrong task
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Affected Version: 7u13
  • Priority: P3
  • Status: Resolved
  • Resolution: Duplicate
  • OS: windows_7
  • Submitted: 2013-06-08
  • Updated: 2014-02-06
  • Resolved: 2014-02-06
Related Reports
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version  " 1.7.0_13 " 
Java(TM) SE Runtime Environment (build 1.7.0_13-b20)
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)

(although it is present in every version of the JDK I have seen since 1.5.

ADDITIONAL OS VERSION INFORMATION :
Not OS specific

A DESCRIPTION OF THE PROBLEM :
Canceling a task submitted to ThreadPoolTaskExecutor with  " mayInterruptIfRunning = true "  may interrupt other tasks. This may cause the other task to erroneously interpret the interrupt as cancellation.

For example, in the following code, it is possible (though extremely unlikely), that task2 gets interrupted:

    ThreadPoolExecutor executor = ...;
    executor.submit(task1).cancel(true);
    executor.submit(task2);

The problem is in the implementation of FutureTask. That is, FutureTask.Sync.innerCancel contains this code:

    if (mayInterruptIfRunning) {
        Thread r = runner;
        if (r != null)
            r.interrupt();
    }

Notice that it is possible that the thread executing this code gets paused just before calling  " r.interrupt() " . Waiting here does not prevent the executor from completing this task and start executing the other one (task2). If the thread executing  " r.interrupt() "  continues executing just after task2 has been started, nothing will prevent task2 from being interrupted.

ps.: I have seen this issue reported by someone else, I just cannot find the report anymore.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
I could never reproduce this issue in practice, as it needs very exceptional conditions.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Calling cancel(true) on the Future of a submitted task will not interrupt other tasks submitted to the executor.
ACTUAL -
Calling cancel(true) may cancel other tasks submitted to the executor.

REPRODUCIBILITY :
This bug can be reproduced rarely.

CUSTOMER SUBMITTED WORKAROUND :
There is no workaround I know of but this issue is near impossible to reproduce in practice.
Comments
This was fixed in 8 by the re-design done under 7132378.
10-06-2013