This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 242979 - Output window contents garbled when cleared by other thread
Summary: Output window contents garbled when cleared by other thread
Status: RESOLVED FIXED
Alias: None
Product: platform
Classification: Unclassified
Component: Output Window (show other bugs)
Version: 7.4
Hardware: All All
: P3 normal with 1 vote (vote)
Assignee: Jaroslav Havlin
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-03-17 16:59 UTC by ebakke
Modified: 2014-06-06 09:12 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
Screenshot showing the correct vs. garbled contents of the output pane (45.12 KB, image/png)
2014-03-17 16:59 UTC, ebakke
Details

Note You need to log in before you can comment on or make changes to this bug.
Description ebakke 2014-03-17 16:59:24 UTC
Created attachment 146079 [details]
Screenshot showing the correct vs. garbled contents of the output pane

My platform application has several worker threads that use the same output window to show status messages. Whenever a thread does a new task, it will update the output message like this:

static final InputOutput inputOutput = 
  IOProvider.getDefault().getIO("SQL Queries", false);

void setOutputMessage(String message) {
  PrintWriter pw = inputOutput.getOut();
  synchronized (pw) {
    pw.reset();
    pw.println(message);
  }
}

However, when two threads do this at the same thread, the output is often garbled. See the attached screenshot. It's garbled in an interesting way--each character is replaced by a substitution character, but newlines are preserved. This seems like an Output pane bug, no?
Comment 1 ebakke 2014-03-17 17:01:52 UTC
Sorry, that should be "when two threads do this at the same _time_".
Comment 2 Jaroslav Havlin 2014-03-21 11:06:32 UTC
> It's garbled in an interesting way--each character is replaced by
> a substitution character, but newlines are preserved.
Probably the content (stored in a memory-mapped file) had been cleared, but metadata (containing line lengths and offsets) were untouched.

> This seems like an Output pane bug, no?
It may be a problem in synchronization of the "reset" operation.

Thank you.
Comment 3 Jaroslav Havlin 2014-04-18 12:07:04 UTC
The problem occurs when operations happen in this order:

In Thread 1:
 - reset() - use new OutWriter o1
   - schedules reset of output pane in EDT
 - println()

In Thread 2:
 - reset() - use new OutWriter o2
   - schedules reset of output pane in EDT
 - println()

In EDT (after operations in both Thread 1 and Thread 2 finished)
 (first scheduled invocation)
 - reset pane
   - set new document with OutWriter o2 (currently set in InputOutput object.)
     - dispose old document which use OutWriter o0
 (second scheduled invocation)
 - reset pane
   - set new document with OutWriter o2 (still currently set in InputOutput object.)
     - dispose old document, which unfortunately also use o2

So the problem is that the reset operation in EDT is called twice for the same  OutWriter.


Possible workaround is calling reset() in EDT:

void setOutputMessage(String message) {
  OutputWriter pw = inputOutput.getOut();
  synchronized (someCustomLock) { // cannot synchronize on pw, deadlock would occur
    EventQueue.invokeAndWait(new Runnable() {
       @Override
       public void run() {
          pw.reset(); // handle exceptions
       }
    });
    pw.println(message);
  }
}


Fixed in trunk as http://hg.netbeans.org/core-main/rev/5be1a645928b.
Please verify the fix in the development build if possible.
Thank you very much for reporting and for providing the sample test case.
Comment 4 Quality Engineering 2014-04-19 04:08:15 UTC
Integrated into 'main-silver', will be available in build *201404190001* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/5be1a645928b
User: Jaroslav Havlin <jhavlin@netbeans.org>
Log: #242979: Output window contents garbled when cleared by other thread
Comment 5 ebakke 2014-04-22 05:13:36 UTC
Thanks for fixing! I'd try to verify, but I'm not sure how to build my Maven-based platform application against nightly. Is there a maven repository for nightly that I can point my pom.xml at?
Comment 6 Jaroslav Havlin 2014-04-22 07:40:02 UTC
> Is there a maven repository for nightly that I can point my pom.xml at?
Please try this:
http://bits.netbeans.org/netbeans/trunk/maven-snapshot/

Thank you for help.
Comment 7 ebakke 2014-04-22 14:58:16 UTC
I just ran my application on RELEASE80, observed the bug several times, and then ran on today's dev-SNAPSHOT, and could not reproduce the bug there anymore. It seems the bug is fixed--thank you!

One observation: the garbled contents is still visible for a split-second every time the output window is cleared, even if only a single thread is accessing the output window at a time. Might be a separate issue, although only a cosmetic one.

(Note: after Googling around, it seems repository http://bits.netbeans.org/netbeans/trunk/new-maven-snapshot with version "dev-SNAPSHOT" is the right way to build against nightly, see bug 228747.)
Comment 8 Jaroslav Havlin 2014-06-06 09:12:07 UTC
(In reply to ebakke from comment #7)
> I just ran my application on RELEASE80, observed the bug several times, and
> then ran on today's dev-SNAPSHOT, and could not reproduce the bug there
> anymore. It seems the bug is fixed--thank you!
Thank you very much for checking it.

> One observation: the garbled contents is still visible for a split-second
> every time the output window is cleared, even if only a single thread is
> accessing the output window at a time. Might be a separate issue, although
> only a cosmetic one.
I didn't notice this, but I guess it can be caused by the fact that the writer is reset and the new document is set some time later later in EDT. It can be noticeable if there are more events in the event queue.

> (Note: after Googling around, it seems repository
> http://bits.netbeans.org/netbeans/trunk/new-maven-snapshot with version
> "dev-SNAPSHOT" is the right way to build against nightly, see bug 228747.)
Thank you.