Skip to content

Instantly share code, notes, and snippets.

@gregclermont
Last active August 29, 2015 13:57
Show Gist options
  • Save gregclermont/9793651 to your computer and use it in GitHub Desktop.
Save gregclermont/9793651 to your computer and use it in GitHub Desktop.
Fix for `app error: deadlock; recursive locking (ThreadError)` with Rack::Deflater in rack 1.4.5 (rails 3.2)

This file combines 7bda8d4 & d860397 from rack 1.5.2 to fix a race condition in Rack::Deflater. It can be used to patch rack 1.4.5 for apps that can't upgrade to rack 1.5.2 (e.g. as an initializer in a rails 3.2 app)

edit: added 6c186f2, too

module Rack
class Deflater
class GzipStream
def initialize(body, mtime)
@body = body
@mtime = mtime
@closed = false
end
def each(&block)
@writer = block
gzip =::Zlib::GzipWriter.new(self)
gzip.mtime = @mtime
@body.each { |part|
gzip.write(part)
gzip.flush
}
ensure
gzip.close
@writer = nil
end
def close
return if @closed
@closed = true
@body.close if @body.respond_to?(:close)
end
end
class DeflateStream
def initialize(body)
@body = body
@closed = false
end
def each
deflator = ::Zlib::Deflate.new(*DEFLATE_ARGS)
@body.each { |part| yield deflator.deflate(part, Zlib::SYNC_FLUSH) }
yield deflator.finish
nil
ensure
deflator.close
end
def close
return if @closed
@closed = true
@body.close if @body.respond_to?(:close)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment