Skip to content

Commit

Permalink
Restore sync=true on global stdout/stderr streams (#2557)
Browse files Browse the repository at this point in the history
* Restore sync=true on STDOUT/STDERR streams

* Move mutation of STDOUT and STDERR streams to `redirect_io`

This isn't technically related to redirecting the STDOUT and STDERR
streams, but moving it here keeps all of the STDOUT/STDERR logic
together. It seems like a more natural place to put it.

* Add a test to ensure that STDOUT is flushed by default
  • Loading branch information
cjlarose committed Feb 22, 2021
1 parent 1555ca2 commit 7a2cdf6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Bugfixes
* Add `#flush` and `#sync` methods to `Puma::NullIO` ([#2553])
* Restore `sync=true` on `STDOUT` and `STDERR` streams #2557

## 5.2.1 / 2021-02-05

Expand Down
3 changes: 2 additions & 1 deletion lib/puma/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ def puma_default_options
:first_data_timeout => Const::FIRST_DATA_TIMEOUT,
:raise_exception_on_sigterm => true,
:max_fast_inline => Const::MAX_FAST_INLINE,
:io_selector_backend => :auto
:io_selector_backend => :auto,
:mutate_stdout_and_stderr_to_sync_on_write => true,
}
end

Expand Down
4 changes: 4 additions & 0 deletions lib/puma/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -888,5 +888,9 @@ def max_fast_inline(num_of_requests)
def io_selector_backend(backend)
@options[:io_selector_backend] = backend.to_sym
end

def mutate_stdout_and_stderr_to_sync_on_write(enabled=true)
@options[:mutate_stdout_and_stderr_to_sync_on_write] = enabled
end
end
end
5 changes: 5 additions & 0 deletions lib/puma/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def redirect_io
STDERR.puts "=== puma startup: #{Time.now} ==="
STDERR.flush unless STDERR.sync
end

if @options[:mutate_stdout_and_stderr_to_sync_on_write]
STDOUT.sync = true
STDERR.sync = true
end
end

def load_and_bind
Expand Down
6 changes: 6 additions & 0 deletions test/rackup/write_to_stdout.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
app = lambda do |env|
$stdout.write "hello\n"
[200, {"Content-Type" => "text/plain"}, ["Hello World"]]
end

run app
7 changes: 7 additions & 0 deletions test/test_integration_single.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,11 @@ def test_puma_started_log_writing
assert(!File.file?("t2-pid"))
assert_equal("Puma is started\n", out)
end

def test_application_logs_are_flushed_on_write
cli_server 'test/rackup/write_to_stdout.ru'
read_body connect
log_line = @server.gets
assert_equal "hello\n", log_line
end
end

0 comments on commit 7a2cdf6

Please sign in to comment.