ingelabs / classpath

GNU Classpath, Essential Libraries for Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

VMProcess.<init>()/destroy() affect thread interrupt flag

ingebot opened this issue · comments

Note: this issue was migrated automatically using bugzilla2github

Original bug ID: BZ#109152
From: @lgcat76
Reported version: 0.99
CC: gnu_andrew@member.fsf.org

Comment author: @lgcat76

VMProcess.<init>()/destroy() affect thread may disable the interrupt flag.

Both methods contain a block of code like this for performing an uninterruptible wait:

    while (state != TERMINATED) /* or == INITIAL */
      {
        try
          {
            wait();
          }
        catch (InterruptedException e)
          {
            /* ignore */
          }
      }

If the thread is already interrupted when that point is reached, or if it is interrupted while waiting the interrupt flag will be stay cleared after the condition is fulfilled and the rest of the code executed by the thread won't have a chance to know that it has been interrupted.

Comment author: @lgcat76

Possible solution can be using a method like this:

// Must be called with this instance's lock held (i.e. synchronized on this)
private void waitStateUninterruptibly(int state, boolean value)
{
  boolean interrupted = Thread.interrupted();

  while ((this.state == state) != value)
    {
      try
        {
          wait();
        }
      catch (InterruptedException e)
        {
          interrupted = true;
        }
    }

  // Keep interrupt flag unaltered
  if (interrupted)
    Thread.currentThread().interrupt();
}

And replacing the current wait loops with calls to that method:

waitStateUninterruptibly(INITIAL, false);

and

waitStateUninterruptibly(TERMINATED, true);

Comment author: Andrew John Hughes <gnu_andrew@member.fsf.org>

Makes sense. I'll look at changing this.

Note that the VMProcess class in GNU Classpath is meant as a template for VMs, so it may be that individual VMs use a different implementation of their own (and hence another reason this may not have been spotted before)