From cf288fe9a743b6c9685b9f478828e52ac6bff702 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Wed, 27 Mar 2024 21:16:51 +1300 Subject: [PATCH 1/2] Add support for defer_stop. --- lib/async/http/protocol/http1/server.rb | 2 +- lib/async/http/protocol/http2/connection.rb | 2 +- lib/async/http/protocol/http2/server.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/async/http/protocol/http1/server.rb b/lib/async/http/protocol/http1/server.rb index 23e1160b..901d25c3 100644 --- a/lib/async/http/protocol/http1/server.rb +++ b/lib/async/http/protocol/http1/server.rb @@ -55,7 +55,7 @@ def each(task: Task.current) return end - begin + task.defer_stop do # If a response was generated, send it: if response trailer = response.headers.trailer! diff --git a/lib/async/http/protocol/http2/connection.rb b/lib/async/http/protocol/http2/connection.rb index 9b26c9f2..c9dd7a3a 100644 --- a/lib/async/http/protocol/http2/connection.rb +++ b/lib/async/http/protocol/http2/connection.rb @@ -89,7 +89,7 @@ def read_in_background(parent: Task.current) task.annotate("#{version} reading data for #{self.class}.") - begin + task.defer_stop do while !self.closed? self.consume_window self.read_frame diff --git a/lib/async/http/protocol/http2/server.rb b/lib/async/http/protocol/http2/server.rb index 73d7f833..907698ed 100644 --- a/lib/async/http/protocol/http2/server.rb +++ b/lib/async/http/protocol/http2/server.rb @@ -53,7 +53,7 @@ def each(task: Task.current) @count += 1 - begin + task.defer_stop do response = yield(request) rescue # We need to close the stream if the user code blows up while generating a response: From 1d3f1215b1f6b1465afed263eafefdf692f351c8 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 28 Mar 2024 11:47:50 +1300 Subject: [PATCH 2/2] We don't need to defer stop in the HTTP/2 background reader. --- lib/async/http/protocol/http2/connection.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/async/http/protocol/http2/connection.rb b/lib/async/http/protocol/http2/connection.rb index c9dd7a3a..0068ac23 100644 --- a/lib/async/http/protocol/http2/connection.rb +++ b/lib/async/http/protocol/http2/connection.rb @@ -89,7 +89,8 @@ def read_in_background(parent: Task.current) task.annotate("#{version} reading data for #{self.class}.") - task.defer_stop do + # We don't need to defer stop here as this is already a transient task (ignores stop): + begin while !self.closed? self.consume_window self.read_frame