Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 323816

Summary: [console] Race condition between writing to console and clearing console?
Product: [Eclipse Project] Platform Reporter: Dennis Hendriks <dh_tue>
Component: DebugAssignee: Platform-Debug-Inbox <platform-debug-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: dh_tue, john.ruud, paul-eclipse, pawel.1.piech, remy.suen
Version: 3.6   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Dennis Hendriks CLA 2010-08-27 08:58:37 EDT
Build Identifier: Build id: 20100617-1415 (Helios)

I have a plug-in that contributes an action. Executing the action clears the console, and prints some
output. This repeated multiple times. After the last clear, output is also written. However, the console is empty after executing the action. Invoking the action a second or third time does give me output on the console after the action. It feels like a race condition between writing and clearing, kind of like described in this old and long time ago fixed bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=202564

Reproducible: Always

Steps to Reproduce:
1. Create a Hello World Command plugin project. Call the handler "TestHandler"
and set the package to "testplugin.handlers".
2. Replace the handler code with the code below.
3. Run the plugin.
4. Select the "Sample Command" from the "Sample Menu" (or use ctrl+6 shortcut).
5. Observe that output is written and the console is emptied (you have to be careful here, as it is done very fast!). The most important thing is that the console is empty after the command finishes execution.
6. Run the command again. There should be output this time.

NOTE: if it is a race condition, I can't guarantee this is reproducible on other systems. However, on my system, it happens every time I restart the Eclipse Application and execute the action for the first time...

Code:

package testplugin.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

public class TestHandler extends AbstractHandler {

    private static final String PLUGIN_CONSOLE_NAME = "Test";

    public Object execute(ExecutionEvent event) throws ExecutionException {
        MessageConsole console = getConsole();
        MessageConsoleStream out = console.newMessageStream();
        for (int i = 0;i<20;i++){
            console.clearConsole();
            for (int j = 0; j<20;j++)
            {
                out.println("Plugin command output...");
            }
        }
        return null;
    }

    public static MessageConsole findConsole(String name) {
        ConsolePlugin plugin = ConsolePlugin.getDefault();
        IConsoleManager conMan = plugin.getConsoleManager();
        IConsole[] existing = conMan.getConsoles();
        for (int i = 0; i < existing.length; i++)
            if (name.equals(existing[i].getName()))
                return (MessageConsole) existing[i];
        // no console found, so create a new one
        MessageConsole myConsole = new MessageConsole(name, null);
        conMan.addConsoles(new IConsole[] { myConsole });
        return myConsole;
    }

    public static MessageConsole getConsole() {
        MessageConsole myConsole = findConsole(PLUGIN_CONSOLE_NAME);
        myConsole.activate();
        return myConsole;
    }
}
Comment 1 Remy Suen CLA 2010-08-27 15:36:18 EDT
Issues with the 'Console' view goes to Debug.
Comment 2 John Ruud CLA 2014-06-27 16:21:36 EDT
I ran into a similar/possibly identical race condition with Eclipse 4.3.2 when doing: MessageConsole.clearConsole() + MessageConsoleStream.print().

The workaround was to clear the console by running code similar to what's in one of superclasses: TextConsole.clearConsole() (but that might need to run in the UI thread):

    public void clearConsole() {
        IDocument document = getDocument();
        if (document != null) {
            document.set(""); //$NON-NLS-1$
        }
    }
Comment 3 Lars Vogel CLA 2019-11-27 07:17:03 EST
This bug hasn't had any activity in quite some time. Maybe the problem got
resolved, was a duplicate of something else, or became less pressing for some
reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it.
The information can be, for example, that the problem still occurs, that you
still want the feature, that more information is needed, or that the bug is
(for whatever reason) no longer relevant.

If the bug is still relevant, please remove the stalebug whiteboard tag.
Comment 4 Paul Pazderski CLA 2020-01-09 16:06:28 EST
*** Bug 323814 has been marked as a duplicate of this bug. ***
Comment 5 Paul Pazderski CLA 2020-01-09 16:10:09 EST
Comment 2 is a duplicate of bug 365770. For comment 0, Console had some major changes at least last year. I assume it is fixed.

*** This bug has been marked as a duplicate of bug 365770 ***