Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion async-http.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "async", ">= 2.35.1"
spec.add_dependency "async-pool", "~> 0.11"
spec.add_dependency "io-endpoint", "~> 0.14"
spec.add_dependency "io-stream", "~> 0.6"
spec.add_dependency "io-stream", "~> 0.14"
spec.add_dependency "protocol-http", "~> 0.64"
spec.add_dependency "protocol-http1", "~> 0.39"
spec.add_dependency "protocol-http2", "~> 0.26"
Expand Down
1 change: 1 addition & 0 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

gem "sus-fixtures-async"
gem "sus-fixtures-async-http", "~> 0.8"
gem "sus-fixtures-console"
gem "sus-fixtures-openssl"
gem "sus-fixtures-benchmark"

Expand Down
21 changes: 19 additions & 2 deletions lib/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2025, by Samuel Williams.
# Copyright, 2018-2026, by Samuel Williams.

require_relative "request"
require_relative "response"
Expand Down Expand Up @@ -67,7 +67,24 @@ def concurrency

# Can we use this connection to make requests?
def viable?
self.idle? && @stream&.readable?
unless self.idle?
return false
end

unless @stream
return false
end

# `nil` indicates that the connection has no data, but is still alive. An empty string means the connection is closed, while a non-empty string indicates application data on a idle HTTP/1 connection, both are failure cases.
if @stream.peek_partial(1)
return false
end

return @stream.readable?
rescue => error
Console.debug(self, "Connection viability probe failed!", exception: error)

return false
end

# @returns [Boolean] Whether the connection can be reused for another request.
Expand Down
4 changes: 4 additions & 0 deletions releases.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Releases

## Unreleased

- Probe idle HTTP/1 connections before reuse, avoiding requests on connections already closed by the peer.

## v0.98.0

- Rewind request bodies before retrying requests.
Expand Down
118 changes: 118 additions & 0 deletions test/async/http/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2026, by Samuel Williams.

require "async/http/protocol/http1/connection"

require "io/stream"

require "sus/fixtures/async/reactor_context"
require "sus/fixtures/console"
require "sus/fixtures/openssl/verified_certificate_context"
require "sus/fixtures/openssl/valid_certificate_context"

describe Async::HTTP::Protocol::HTTP1::Connection do
include Sus::Fixtures::Async::ReactorContext
include Sus::Fixtures::Console::CapturedLogger
include Sus::Fixtures::OpenSSL::VerifiedCertificateContext
include Sus::Fixtures::OpenSSL::ValidCertificateContext

before do
listener = TCPServer.new("localhost", 0)
port = listener.local_address.ip_port

@sockets = [
TCPSocket.new("localhost", port),
listener.accept,
]
listener.close

client_socket = OpenSSL::SSL::SSLSocket.new(@sockets[0], client_context)
server_socket = OpenSSL::SSL::SSLSocket.new(@sockets[1], server_context)

client_socket.sync_close = true
server_socket.sync_close = true

accept = reactor.async do
server_socket.accept
end

connect = reactor.async do
client_socket.connect
end

[accept, connect].each(&:wait)

@client = IO::Stream::Buffered.wrap(client_socket)
@server = IO::Stream::Buffered.wrap(server_socket)
@connection = subject.new(@client, "HTTP/1.1")
end

after do
@client&.close
@server&.close

@sockets.each do |socket|
unless socket.closed?
socket.close
end
end
end

attr :client
attr :server
attr :connection

it "remains viable when an idle TLS connection would block" do
expect(connection).to be(:viable?)
end

it "is not viable while active" do
connection.state = :open

expect(connection).not.to be(:viable?)
end

it "is not viable without a stream" do
connection = subject.new(nil, "HTTP/1.1")

expect(connection).not.to be(:viable?)
end

it "is not viable after a TLS close notification" do
closing = reactor.async do
server.close
end

@sockets.first.wait_readable(1)

expect(connection).not.to be(:viable?)
closing.wait
end

it "is not viable after an abrupt TLS disconnect" do
@sockets.last.close
@server = nil

@sockets.first.wait_readable(1)

expect(connection).not.to be(:viable?)
expect_console.to have_logged(
severity: be == :debug,
subject: be_equal(connection),
message: be == "Connection viability probe failed!",
event: have_keys(type: be == :failure)
)
end

it "is not viable when application data is pending while idle" do
server.write("H")
server.flush

@sockets.first.wait_readable(1)

expect(connection).not.to be(:viable?)
expect(client.read(1)).to be == "H"
end
end
43 changes: 41 additions & 2 deletions test/async/http/ssl.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2025, by Samuel Williams.
# Copyright, 2018-2026, by Samuel Williams.

require "async/http/server"
require "async/http/client"
require "async/http/endpoint"

require "sus/fixtures/async"
require "sus/fixtures/openssl"
require "sus/fixtures/async/http"
require "sus/fixtures/console"
require "sus/fixtures/openssl"

describe Async::HTTP::Server do
include Sus::Fixtures::Async::HTTP::ServerContext
Expand Down Expand Up @@ -52,5 +53,43 @@ def make_client_endpoint(bound_endpoint)
expect(response).to be(:success?)
expect(response.read).to be == "Hello World!"
end

with "an idle connection closed by the server" do
include Sus::Fixtures::Console::NullLogger

let(:connections) {[]}

let(:app) do
connections = self.connections

Protocol::HTTP::Middleware.for do |request|
connections << request.connection

Protocol::HTTP::Response[200, {}, ["Hello World!"]]
end
end

it "uses a fresh connection for a non-idempotent request" do
first_response = client.get("/")
first_response.read
first_connection = first_response.connection

closing = reactor.async do
connections.first.close
end

first_connection.stream.io.to_io.wait_readable(1)

second_response = client.post("/")

expect(second_response).to be(:success?)
expect(second_response.connection).not.to be == first_connection
expect(connections.size).to be == 2
closing.wait
ensure
first_response&.close
second_response&.close
end
end
end
end
Loading