Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Guard the logger's internal buffer to prevent major breakage on genui…

…nely threaded environments
  • Loading branch information...
commit a24398b64757df8c5939b07238c740bddfdab03e 1 parent 7aaf168
@NZKoz NZKoz authored
Showing with 14 additions and 6 deletions.
  1. +14 −6 activesupport/lib/active_support/buffered_logger.rb
View
20 activesupport/lib/active_support/buffered_logger.rb
@@ -40,6 +40,7 @@ def initialize(log, level = DEBUG)
@buffer = []
@auto_flushing = 1
@no_block = false
+ @guard = Mutex.new
if log.respond_to?(:write)
@log = log
@@ -66,7 +67,9 @@ def add(severity, message = nil, progname = nil, &block)
# If a newline is necessary then create a new message ending with a newline.
# Ensures that the original message is not mutated.
message = "#{message}\n" unless message[-1] == ?\n
- buffer << message
+ @guard.synchronize do
+ buffer << message
+ end
auto_flush
message
end
@@ -98,11 +101,16 @@ def auto_flushing=(period)
end
def flush
- unless buffer.empty?
- if @no_block
- @log.write_nonblock(buffer.slice!(0..-1).join)
- else
- @log.write(buffer.slice!(0..-1).join)
+ @guard.synchronize do
+ unless buffer.empty?
+ old_buffer = @buffer
+ @buffer = []
+ text_to_write = old_buffer.join
+ if @no_block
+ @log.write_nonblock(text_to_write)
+ else
+ @log.write(text_to_write)
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.