Skip to content

Commit 6d6fb1a

Browse files
committed
Wait for input to finish even when streaming, if necessary.
1 parent 87a2a72 commit 6d6fb1a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/async/http/protocol/http1/finishable.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ def close(error = nil)
4040
super
4141
end
4242

43-
def wait
43+
def wait(persistent = true)
4444
if @reading
4545
@closed.wait
46-
else
46+
elsif persistent
47+
# If the connection can be reused, let's gracefully discard the body:
4748
self.discard
49+
else
50+
# Else, we don't care about the body, so we can close it immediately:
51+
self.close
4852
end
4953
end
5054

lib/async/http/protocol/http1/server.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ def each(task: Task.current)
9696
request = nil
9797
response = nil
9898

99-
# We must return here as no further request processing can be done:
100-
return body.call(stream)
99+
# In the case of streaming, `finishable` should wrap a `Remainder` body, which we can safely discard later on.
100+
body.call(stream)
101101
elsif response.status == 101
102102
# This code path is to support legacy behavior where the response status is set to 101, but the protocol is not upgraded. This may not be a valid use case, but it is supported for compatibility. We expect the response headers to contain the `upgrade` header.
103103
write_response(@version, response.status, response.headers)
@@ -108,8 +108,7 @@ def each(task: Task.current)
108108
request = nil
109109
response = nil
110110

111-
# We must return here as no further request processing can be done:
112-
return body&.call(stream)
111+
body&.call(stream)
113112
else
114113
write_response(@version, response.status, response.headers)
115114

@@ -143,8 +142,12 @@ def each(task: Task.current)
143142
request&.finish
144143
end
145144

146-
# Discard or wait for the input body to be consumed:
147-
finishable&.wait
145+
if finishable
146+
finishable.wait(@persistent)
147+
else
148+
# Do not remove this line or you will unleash the gods of concurrency hell.
149+
task.yield
150+
end
148151
rescue => error
149152
raise
150153
ensure

0 commit comments

Comments
 (0)