Skip to content

InputStreamMonitor.writeNext() wait() not in loop — vulnerable to spurious wakeups #2881

Description

@robstryker

In InputStreamMonitor.writeNext(), the fLock.wait() call at line 189 is guarded by an if check:

synchronized(fLock) {
    // Queue could receive more input between last empty check and
    // lock acquire. See https://bugs.eclipse.org/550834
    if (fQueue.isEmpty()) {
        fLock.wait();
    }
}

Per the Java specification, Object.wait() can return spuriously — without a corresponding notify()/notifyAll(). The standard pattern is to use a while loop instead of if:

synchronized(fLock) {
    while (fQueue.isEmpty() && !fClosed) {
        fLock.wait();
    }
}

This ensures the condition is rechecked after any wakeup. The !fClosed guard also avoids waiting forever if the stream was closed while waiting.

Found via SpotBugs static analysis (WA_NOT_IN_LOOP).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions