Search code examples
rubynetbeansnetbeans-plugins

Nonsequential Ruby Error Output in Netbeans Console


I'm trying to get started with using Netbeans 7.0 for rails development, and am using the latest ruby plugin. For the record I switched the default 1.8.7 interpreter to point to a local 1.9.2 ruby installation, but this issue seems to happen with both interpreters.

When ruby encounters an error in my code, the error output is shown at random points in my console output. I would have expected it to print the error as it is encountered, but it looks like the error stream and normal output stream are being updated on different threads. Just to give an example...with this code:

(0..10).each { |o| puts "Normal output" }
invalidSytax!

I am triggering a syntax error on the second line, but the error output can vary. For example:

Normal output
Normal output
Normal output
~/LearnRuby/lib/ch1_ex2.rb:41:in `<main>': undefined method `invalidSytax!' for main:Object (NoMethodError)
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output

and...

~/LearnRuby/lib/ch1_ex2.rb:41:in `<main>': undefined method `invalidSytax!' for main:Object (NoMethodError)
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output
Normal output

Any idea what could be causing this? I could see it being a big problem in a bigger project, when I might think my code has no issues but there's really an error message being outputted at the beginning of my console stream.


Solution

  • That's because of NetBeans' internal handling of STDOUT and STDERR, the output is not deterministic, I suppose they're both handled in two separate threads as you already suspected. So you are most likely witness of a race condition here.

    But this happens independently of using Ruby or not - e.g. when I run JUnit tests for my Java projects, the same happens, STDERR and STDOUT output gets mixed indeterministically.

    Why exactly do you consider this to be a problem? As long as the output does not get mixed to gibberish (this would happen if both threads would try to write to NetBeans' console at the same time and no proper locking mechanism was in place) I don't see why this would matter.

    The same principle can be observed with log files of web applications. If a lot of client connections try to log their information to a file at the same time, you will not be able to predict the order in which the messages will appear - in my opinion that behaviour is inherent to multi-threaded applications.