mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 15:49:58 +00:00
TUN-8621: Prevent QUIC connection from closing before grace period after unregistering
Whenever cloudflared receives a SIGTERM or SIGINT it goes into graceful shutdown mode, which unregisters the connection and closes the control stream. Unregistering makes it so we no longer receive any new requests and makes the edge close the connection, allowing in-flight requests to finish (within a 3 minute period). This was working fine for http2 connections, but the quic proxy was cancelling the context as soon as the controls stream ended, forcing the process to stop immediately. This commit changes the behavior so that we wait the full grace period before cancelling the request
This commit is contained in:

committed by
chungthuang

parent
05249c7b51
commit
e251a21810
@@ -45,9 +45,10 @@ class TestTermination:
|
||||
with connected:
|
||||
connected.wait(self.timeout)
|
||||
# Send signal after the SSE connection is established
|
||||
self.terminate_by_signal(cloudflared, signal)
|
||||
self.wait_eyeball_thread(
|
||||
in_flight_req, self.grace_period + self.timeout)
|
||||
with self.within_grace_period():
|
||||
self.terminate_by_signal(cloudflared, signal)
|
||||
self.wait_eyeball_thread(
|
||||
in_flight_req, self.grace_period + self.timeout)
|
||||
|
||||
# test cloudflared terminates before grace period expires when all eyeball
|
||||
# connections are drained
|
||||
@@ -66,7 +67,7 @@ class TestTermination:
|
||||
|
||||
with connected:
|
||||
connected.wait(self.timeout)
|
||||
with self.within_grace_period():
|
||||
with self.within_grace_period(has_connection=False):
|
||||
# Send signal after the SSE connection is established
|
||||
self.terminate_by_signal(cloudflared, signal)
|
||||
self.wait_eyeball_thread(in_flight_req, self.grace_period)
|
||||
@@ -78,7 +79,7 @@ class TestTermination:
|
||||
with start_cloudflared(
|
||||
tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True, capture_output=False) as cloudflared:
|
||||
wait_tunnel_ready(tunnel_url=config.get_url())
|
||||
with self.within_grace_period():
|
||||
with self.within_grace_period(has_connection=False):
|
||||
self.terminate_by_signal(cloudflared, signal)
|
||||
|
||||
def terminate_by_signal(self, cloudflared, sig):
|
||||
@@ -92,13 +93,21 @@ class TestTermination:
|
||||
|
||||
# Using this context asserts logic within the context is executed within grace period
|
||||
@contextmanager
|
||||
def within_grace_period(self):
|
||||
def within_grace_period(self, has_connection=True):
|
||||
try:
|
||||
start = time.time()
|
||||
yield
|
||||
finally:
|
||||
|
||||
# If the request takes longer than the grace period then we need to wait at most the grace period.
|
||||
# If the request fell within the grace period cloudflared can close earlier, but to ensure that it doesn't
|
||||
# close immediately we add a minimum boundary. If cloudflared shutdown in less than 1s it's likely that
|
||||
# it shutdown as soon as it received SIGINT. The only way cloudflared can close immediately is if it has no
|
||||
# in-flight requests
|
||||
minimum = 1 if has_connection else 0
|
||||
duration = time.time() - start
|
||||
assert duration < self.grace_period
|
||||
# Here we truncate to ensure that we don't fail on minute differences like 10.1 instead of 10
|
||||
assert minimum <= int(duration) <= self.grace_period
|
||||
|
||||
def stream_request(self, config, connected, early_terminate):
|
||||
expected_terminate_message = "502 Bad Gateway"
|
||||
|
Reference in New Issue
Block a user