Skip to content

Instantly share code, notes, and snippets.

@wjordan
Created May 29, 2020 18:23
Show Gist options
  • Save wjordan/0e31fefd0e98c0fdbf9896282d7bd649 to your computer and use it in GitHub Desktop.
Save wjordan/0e31fefd0e98c0fdbf9896282d7bd649 to your computer and use it in GitHub Desktop.
# Repro case of https://bugs.ruby-lang.org/issues/13882.
# A thread in the middle of reading GZip-encoded content with Net::HTTP
# will stop a Ruby process from exiting.
require 'net/http'
PORT = 5678
SLEEP = 1
# Server
Thread.new do
server = TCPServer.new(PORT).accept
server.print "HTTP/1.1 200\r\n"
server.print "Content-Type: text/plain\r\n"
server.print "Content-Encoding: gzip\r\n"
s = StringIO.new
z = Zlib::GzipWriter.new(s)
z.write 'Hello World!'
z.close
server.print "Content-Length: #{s.length}\r\n\r\n"
# Sleep half-way through writing encoded content.
half = s.length / 2
p1 = s.string.byteslice(0, half)
p2 = s.string.byteslice(half, half)
server.write p1
sleep SLEEP
server.write p2
end
# Client
Thread.new do
begin
puts Net::HTTP.get(URI('http://localhost:5678'))
rescue Zlib::BufError => e
puts "Caught error: #{e.inspect}"
retry
end
end
at_exit {puts 'Exiting'}
# Exit half-way through writing encoded content.
sleep SLEEP / 2.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment