Skip to content

Output to STDOUT can't be redirected #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fraterrisus opened this issue Jun 18, 2015 · 2 comments
Closed

Output to STDOUT can't be redirected #109

fraterrisus opened this issue Jun 18, 2015 · 2 comments

Comments

@fraterrisus
Copy link

This is a pretty weird one, and I might be filing it in the wrong place, but hopefully someone smarter than me can figure this out.

The symptom is this: If I create an XML document and use puts to write it to STDOUT, it does so. However, if I try to pipe or redirect the output of the script, it writes nothing.

Using:

  • Ubuntu 14.10
  • linux kernel 3.16.0-39 x86_64
  • libxml2 2.9.1+dfsg1-4ubuntu (system library package)
  • rvm 1.26.11
  • ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]
  • minimal installed gems:
% gem list

*** LOCAL GEMS ***

bigdecimal (1.2.6)
bundler (1.9.3)
bundler-unload (1.0.2)
executable-hooks (1.3.2)
gem-wrappers (1.2.7)
io-console (0.4.3)
json (1.8.1)
libxml-ruby (2.8.0)
minitest (5.4.3)
power_assert (0.2.2)
psych (2.0.8)
rake (10.4.2)
rdoc (4.2.0)
rubygems-bundler (1.4.4)
rvm (1.11.3.9)
test-unit (3.0.8)

And the following script:

#!/usr/bin/env ruby

require 'libxml'

d = LibXML::XML::Document::new
d.root = LibXML::XML::Node.new('configs')

puts d.to_s

If run from the command line, it works as expected:

% ./test.rb
<?xml version="1.0" encoding="UTF-8"?>
<configs/>
% ./test.rb > foo
% cat foo
<?xml version="1.0" encoding="UTF-8"?>
<configs/>
% ls -l foo
-rw-r--r-- 1 becordes staff 50 Jun 18 16:12 foo

But put it inside bundler, and this happens:

% cat Gemfile
source 'https://rubygems.org'
gem 'libxml-ruby'
% bundle install
Fetching gem metadata from https://rubygems.org/..
Fetching version metadata from https://rubygems.org/.
Resolving dependencies...
Installing libxml-ruby 2.8.0
Using bundler 1.9.3
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Bundled gems are installed into ./vendor/bundle.
% bundle exec ./test.rb
<?xml version="1.0" encoding="UTF-8"?>
<configs/>
% bundle exec ./test.rb > foo
% cat foo
% ls -l foo
-rw-r--r-- 1 becordes staff 0 Jun 18 16:10 foo

I've been able to duplicate this in ruby 2.2.0, but not in ruby 2.1.5.

@tgarnett
Copy link

I've been having a very similar issue in ruby 2.2.4

iff using libxml to read through a document, stdout buffering does not get flushed on process exit

perhaps some kind of crash or failure in the libxml finalizers?

The reason it works for the original filer on the command line and not the redirected script is that stdout is line buffered by default for ruby when attached to a terminal (so the puts flushes immediately) but when redirecting to a file it's just generally buffered.

@cfis
Copy link
Member

cfis commented Jun 11, 2016

So I was able to duplicate this once, and then not again. You can likely work around it by:

STDOUT.sync=true

Charlie

@cfis cfis closed this as completed Jun 11, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants