glassfish
  1. glassfish
  2. GLASSFISH-20508

Attempt to cancel a future returned by @Asynchronous call returns false

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Works as designed
    • Affects Version/s: 4.0_dev
    • Fix Version/s: None
    • Labels:
      None
    • Environment:

      64-bit JVM from Oracle Java SE 7 JDK, war built in Eclipse Kepler m6, Linux Fedora 18 64-bit

      Description

      Attempt to cancel a future returns false, see extremely simplified example source code along with actual and expected output. The cancel is called from a @PreDestroy method which is invoked by UNDEPLOYING the application. Is it the case that undeployment prevents cancellation? In fact cancellation is important during undeployment so that blocking calls can be interrupted for cleanup and wind-down.

      Please note that there is no client, this is all within the same server container; both beans are deployed as a single .war file.

      Myself.java

      package com.example.cancelbug;

      import java.util.concurrent.ExecutionException;
      import java.util.concurrent.Future;

      import javax.annotation.PostConstruct;
      import javax.annotation.PreDestroy;
      import javax.ejb.Singleton;
      import javax.ejb.Startup;
      import javax.inject.Inject;

      @Singleton
      @Startup
      public class Myself {
      @Inject Other other;
      private Future < Integer > future;

      @PostConstruct
      public void post_construct ()

      { System.out.println("post_construct started"); future = other.ton_of_work(); System.out.println("post_construct ended"); }

      @PreDestroy
      public void pre_destroy () {
      System.out.println("pre_destroy started");
      System.out.println("cancel result: " + Boolean.toString(future.cancel(true)));
      try

      { System.out.println("future result: " + future.get().toString()); }

      catch (InterruptedException | ExecutionException e)

      { System.out.println("future result: interrupted"); Thread.currentThread().interrupt(); System.out.println("thread reinterrupted"); }

      System.out.println("pre_destroy ended");
      }
      }

      Other.java

      package com.example.cancelbug;

      import java.util.concurrent.Future;

      import javax.ejb.AsyncResult;
      import javax.ejb.Asynchronous;
      import javax.ejb.Stateless;

      @Stateless
      public class Other {
      @Asynchronous
      public Future < Integer > ton_of_work () {
      System.out.println("other: ton_of_work started");
      int i;
      for (i = 0; i < 10; ++i) {
      try

      { System.out.println("other: take a nap"); Thread.sleep(1000L); System.out.println("other: woke up: " + Integer.toString(i)); }

      catch (InterruptedException e)

      { System.out.println("other: ton_of_work interrupted"); Thread.currentThread().interrupt(); break; }

      }
      System.out.println("other: ton_of_work returning");
      return new AsyncResult < Integer >(new Integer);
      }
      }

      Output

      post_construct started
      post_construct ended
      other: ton_of_work started
      other: take a nap
      other: woke up: 0
      other: take a nap
      other: woke up: 1
      other: take a nap
      other: woke up: 2
      other: take a nap
      pre_destroy started
      cancel result: false <<<<<<< expected true; ton_of_work: interrupted
      other: woke up: 3 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 4 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 5 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 6 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 7 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 8 <<<<<<< expected no such output
      other: take a nap <<<<<<< expected no such output
      other: woke up: 9 <<<<<<< expected no such output
      other: ton_of_work returning
      future result: 10 <<<<<<< expected 2
      pre_destroy ended

      *Expected Output:*

      post_construct started
      post_construct ended
      other: ton_of_work started
      other: take a nap
      other: woke up: 0
      other: take a nap
      other: woke up: 1
      other: take a nap
      other: woke up: 2
      other: take a nap
      pre_destroy started
      cancel result: true <<<<<<< actual false
      other: ton_of_work interrupted <<<<<<< actual missing
      other: ton_of_work returning
      future result: 2 <<<<<<< actual 10
      pre_destroy ended

        Activity

        Hide
        agksmehx added a comment -

        Sorry, this is not a bug per https://java.net/projects/ejb-spec/lists/jsr345-experts/archive/2012-09/message/7 and others.

        Marina, thanks for pushing this feature – it would make for incredibly cleaner programming without any downside that I can think of (including backward compatibility with opt-in).

        I cannot close this bug report so please do so on my behalf.

        Show
        agksmehx added a comment - Sorry, this is not a bug per https://java.net/projects/ejb-spec/lists/jsr345-experts/archive/2012-09/message/7 and others. Marina, thanks for pushing this feature – it would make for incredibly cleaner programming without any downside that I can think of (including backward compatibility with opt-in). I cannot close this bug report so please do so on my behalf.
        Hide
        marina vatkina added a comment -

        You might want to be on the users alias to participate in the EG discussions next time

        Show
        marina vatkina added a comment - You might want to be on the users alias to participate in the EG discussions next time

          People

          • Assignee:
            marina vatkina
            Reporter:
            agksmehx
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: