@miller-tech/uap 1.172.13 → 1.172.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/policies/enforcers/__pycache__/_common.cpython-312.pyc +0 -0
- package/templates/hooks/__pycache__/deliver_autoroute.cpython-312.pyc +0 -0
- package/tools/agents/scripts/__pycache__/toolcall_path_normalizer.cpython-312.pyc +0 -0
- package/tools/agents/scripts/anthropic_proxy.py +119 -20
- package/tools/agents/tests/test_client_disconnect.py +18 -19
- package/tools/agents/tests/test_disconnect_watcher.py +174 -0
- package/tools/agents/tests/test_upstream_chokepoint.py +4 -4
package/package.json
CHANGED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -2970,9 +2970,30 @@ _current_request_session: contextvars.ContextVar[str | None] = contextvars.Conte
|
|
|
2970
2970
|
#
|
|
2971
2971
|
# Same ContextVar trick as the session above: request-local without threading a
|
|
2972
2972
|
# Request through every signature. Holds Starlette's `request.is_disconnected`.
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2973
|
+
# Is the DOWNSTREAM client still there?
|
|
2974
|
+
#
|
|
2975
|
+
# Nothing used to ask. Once a turn was accepted, the guardrail loops kept
|
|
2976
|
+
# retrying and re-POSTing upstream on their own — so when the client died the
|
|
2977
|
+
# work carried on regardless. Observed live: two `uap deliver` runs were killed,
|
|
2978
|
+
# no socket remained on :4000, and the proxy still drove llama for minutes,
|
|
2979
|
+
# generating 32k tokens per orphaned turn that nobody would ever read.
|
|
2980
|
+
#
|
|
2981
|
+
# WHY NOT request.is_disconnected(). That was tried and MEASURED not to work
|
|
2982
|
+
# here: it returned False 16 times across 30s while the caller was demonstrably
|
|
2983
|
+
# gone. The cause is `@app.middleware("http")` — Starlette's BaseHTTPMiddleware
|
|
2984
|
+
# runs the endpoint in a separate anyio task behind its own receive channel, so
|
|
2985
|
+
# the endpoint polls a channel that never delivers http.disconnect. It works
|
|
2986
|
+
# fine in an app with no middleware, which is exactly why three rounds of
|
|
2987
|
+
# isolated testing said it was sound.
|
|
2988
|
+
#
|
|
2989
|
+
# So the signal is captured by a pure-ASGI middleware OUTSIDE all of that (see
|
|
2990
|
+
# DisconnectWatcherMiddleware) and published here as a mutable holder. A dict
|
|
2991
|
+
# rather than a bool because the middleware's poller task and the endpoint must
|
|
2992
|
+
# see the same object: the ContextVar is set before the app is invoked, so every
|
|
2993
|
+
# downstream task inherits the reference, and mutations are visible across them.
|
|
2994
|
+
_disconnect_holder: contextvars.ContextVar[dict | None] = contextvars.ContextVar(
|
|
2995
|
+
"uap_disconnect_holder", default=None
|
|
2996
|
+
)
|
|
2976
2997
|
|
|
2977
2998
|
|
|
2978
2999
|
class ClientGoneError(Exception):
|
|
@@ -2986,19 +3007,17 @@ class ClientGoneError(Exception):
|
|
|
2986
3007
|
|
|
2987
3008
|
|
|
2988
3009
|
async def _client_gone() -> bool:
|
|
2989
|
-
"""True when the caller has hung up.
|
|
3010
|
+
"""True when the caller has hung up.
|
|
2990
3011
|
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
3012
|
+
Now a flag read rather than a call into Starlette, so there is nothing to
|
|
3013
|
+
raise and nothing for a fail-safe except to swallow. The previous version
|
|
3014
|
+
wrapped request.is_disconnected() in `except Exception: return False`, which
|
|
3015
|
+
would have hidden a failing probe as "client still present" — it turned out
|
|
3016
|
+
the probe was not raising, but the shape was wrong regardless.
|
|
2994
3017
|
"""
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
try:
|
|
2999
|
-
return bool(await probe())
|
|
3000
|
-
except Exception: # noqa: BLE001
|
|
3001
|
-
return False
|
|
3018
|
+
holder = _disconnect_holder.get()
|
|
3019
|
+
return bool(holder and holder.get("gone"))
|
|
3020
|
+
|
|
3002
3021
|
|
|
3003
3022
|
# Session admission state. _admitted_sessions maps session_id -> last-seen
|
|
3004
3023
|
# monotonic ts; OrderedDict insertion order is the LRU (oldest = front).
|
|
@@ -3385,8 +3404,8 @@ class DisconnectAwareClient(httpx.AsyncClient):
|
|
|
3385
3404
|
"""
|
|
3386
3405
|
|
|
3387
3406
|
async def send(self, request, **kwargs): # type: ignore[override]
|
|
3388
|
-
|
|
3389
|
-
if
|
|
3407
|
+
holder = _disconnect_holder.get()
|
|
3408
|
+
if holder is None:
|
|
3390
3409
|
return await super().send(request, **kwargs)
|
|
3391
3410
|
|
|
3392
3411
|
task = asyncio.ensure_future(super().send(request, **kwargs))
|
|
@@ -3635,6 +3654,73 @@ async def lifespan(app: FastAPI):
|
|
|
3635
3654
|
logger.info("Proxy shut down")
|
|
3636
3655
|
|
|
3637
3656
|
|
|
3657
|
+
class DisconnectWatcherMiddleware:
|
|
3658
|
+
"""Pure-ASGI middleware that records when the caller hangs up.
|
|
3659
|
+
|
|
3660
|
+
Sits OUTERMOST, above the auth middleware, and is deliberately pure ASGI:
|
|
3661
|
+
Starlette's BaseHTTPMiddleware (`@app.middleware("http")`) re-tasks the
|
|
3662
|
+
endpoint behind its own receive channel, which is precisely why
|
|
3663
|
+
request.is_disconnected() reported False 16 times across 30s while the
|
|
3664
|
+
caller was gone. Being outside that, this sees the real channel.
|
|
3665
|
+
|
|
3666
|
+
THE HARD PART is that wrapping receive() is not enough on its own. After the
|
|
3667
|
+
request body is fully read nothing calls receive() again, so an
|
|
3668
|
+
http.disconnect just sits in the channel unobserved. So once the body is
|
|
3669
|
+
complete this starts polling the channel itself — safe precisely because the
|
|
3670
|
+
app has no more body to ask for, and the only remaining message type is the
|
|
3671
|
+
disconnect.
|
|
3672
|
+
|
|
3673
|
+
The flag lives in a mutable dict published on a ContextVar before the app is
|
|
3674
|
+
invoked: downstream tasks inherit the reference when their context is copied,
|
|
3675
|
+
so a mutation made by the poller is visible to the endpoint and to every
|
|
3676
|
+
upstream call it makes.
|
|
3677
|
+
"""
|
|
3678
|
+
|
|
3679
|
+
def __init__(self, app):
|
|
3680
|
+
self.app = app
|
|
3681
|
+
|
|
3682
|
+
async def __call__(self, scope, receive, send):
|
|
3683
|
+
if scope.get("type") != "http":
|
|
3684
|
+
return await self.app(scope, receive, send)
|
|
3685
|
+
|
|
3686
|
+
holder: dict = {"gone": False}
|
|
3687
|
+
_disconnect_holder.set(holder)
|
|
3688
|
+
poller: asyncio.Task | None = None
|
|
3689
|
+
|
|
3690
|
+
async def poll_for_disconnect() -> None:
|
|
3691
|
+
# Only reached once the body is complete, so this cannot steal a
|
|
3692
|
+
# message the app still needs.
|
|
3693
|
+
try:
|
|
3694
|
+
while not holder["gone"]:
|
|
3695
|
+
msg = await receive()
|
|
3696
|
+
if msg.get("type") == "http.disconnect":
|
|
3697
|
+
holder["gone"] = True
|
|
3698
|
+
return
|
|
3699
|
+
except asyncio.CancelledError:
|
|
3700
|
+
raise
|
|
3701
|
+
except Exception: # noqa: BLE001
|
|
3702
|
+
return # channel closed/erroring: stop watching, never crash
|
|
3703
|
+
|
|
3704
|
+
async def watched_receive():
|
|
3705
|
+
nonlocal poller
|
|
3706
|
+
msg = await receive()
|
|
3707
|
+
if msg.get("type") == "http.disconnect":
|
|
3708
|
+
holder["gone"] = True
|
|
3709
|
+
elif msg.get("type") == "http.request" and not msg.get("more_body", False):
|
|
3710
|
+
# Body complete — nobody will call receive() again, so take over.
|
|
3711
|
+
if poller is None:
|
|
3712
|
+
poller = asyncio.ensure_future(poll_for_disconnect())
|
|
3713
|
+
return msg
|
|
3714
|
+
|
|
3715
|
+
try:
|
|
3716
|
+
await self.app(scope, watched_receive, send)
|
|
3717
|
+
finally:
|
|
3718
|
+
if poller is not None and not poller.done():
|
|
3719
|
+
poller.cancel()
|
|
3720
|
+
|
|
3721
|
+
|
|
3722
|
+
|
|
3723
|
+
|
|
3638
3724
|
app = FastAPI(
|
|
3639
3725
|
title="UAP Anthropic Proxy",
|
|
3640
3726
|
description="Translates Anthropic Messages API to OpenAI Chat Completions API",
|
|
@@ -3642,6 +3728,7 @@ app = FastAPI(
|
|
|
3642
3728
|
lifespan=lifespan,
|
|
3643
3729
|
)
|
|
3644
3730
|
|
|
3731
|
+
|
|
3645
3732
|
@app.exception_handler(ClientGoneError)
|
|
3646
3733
|
async def _client_gone_handler(request: Request, exc: ClientGoneError):
|
|
3647
3734
|
"""The caller hung up mid-turn: stop, and say so at INFO rather than ERROR.
|
|
@@ -3696,6 +3783,18 @@ async def _pool_timeout_handler(request: Request, exc: httpx.PoolTimeout):
|
|
|
3696
3783
|
_PROXY_AUTH_OPEN_PATHS = frozenset({"/health", "/", "/v1/models"})
|
|
3697
3784
|
|
|
3698
3785
|
|
|
3786
|
+
# OUTERMOST — and it must be registered AFTER the auth middleware below to BE
|
|
3787
|
+
# outermost: Starlette's add_middleware inserts at position 0, so the last one
|
|
3788
|
+
# registered is the outermost wrapper. (Registered before auth first time round,
|
|
3789
|
+
# which would have put it INSIDE the very BaseHTTPMiddleware that hides
|
|
3790
|
+
# http.disconnect — the exact bug it exists to work around.)
|
|
3791
|
+
#
|
|
3792
|
+
# The registration therefore trails the auth definition on purpose. Auth itself
|
|
3793
|
+
# is deliberately untouched.
|
|
3794
|
+
def _install_disconnect_watcher() -> None:
|
|
3795
|
+
app.add_middleware(DisconnectWatcherMiddleware)
|
|
3796
|
+
|
|
3797
|
+
|
|
3699
3798
|
@app.middleware("http")
|
|
3700
3799
|
async def _shared_secret_auth(request: Request, call_next):
|
|
3701
3800
|
"""Gate every request behind PROXY_AUTH_TOKEN when it is set.
|
|
@@ -3754,6 +3853,10 @@ async def _shared_secret_auth(request: Request, call_next):
|
|
|
3754
3853
|
# ===========================================================================
|
|
3755
3854
|
|
|
3756
3855
|
|
|
3856
|
+
|
|
3857
|
+
# Auth is now defined, so this lands OUTSIDE it (add_middleware inserts at 0).
|
|
3858
|
+
_install_disconnect_watcher()
|
|
3859
|
+
|
|
3757
3860
|
def _image_block_to_openai(block: dict) -> dict | None:
|
|
3758
3861
|
"""Anthropic image block → OpenAI image_url part (data URI or URL)."""
|
|
3759
3862
|
src = block.get("source") or {}
|
|
@@ -10908,10 +11011,6 @@ async def messages(request: Request):
|
|
|
10908
11011
|
"""
|
|
10909
11012
|
global last_session_id
|
|
10910
11013
|
|
|
10911
|
-
# Publish this request's disconnect probe for the whole call tree. Set before
|
|
10912
|
-
# ANY upstream work so an already-dead client is caught on the first call.
|
|
10913
|
-
_current_client_gone.set(request.is_disconnected)
|
|
10914
|
-
|
|
10915
11014
|
body = await request.json()
|
|
10916
11015
|
is_stream = body.get("stream", False)
|
|
10917
11016
|
model = body.get("model", "default")
|
|
@@ -28,33 +28,32 @@ proxy = _load_proxy()
|
|
|
28
28
|
|
|
29
29
|
|
|
30
30
|
class ClientGoneProbeTest(unittest.TestCase):
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
"""The probe is now a FLAG READ, not a call into Starlette.
|
|
32
|
+
|
|
33
|
+
It used to call request.is_disconnected() wrapped in `except Exception:
|
|
34
|
+
return False`. That was measured not to work at all behind
|
|
35
|
+
BaseHTTPMiddleware (False 16x across 30s while the caller was gone), and the
|
|
36
|
+
swallow-everything shape would have hidden a failing probe as "client still
|
|
37
|
+
present". Both are gone: there is nothing left to raise.
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def test_no_holder_means_client_is_present(self):
|
|
41
|
+
"""Background tasks and health checks have no request context. They must
|
|
42
|
+
not read as disconnected."""
|
|
43
|
+
proxy._disconnect_holder.set(None)
|
|
35
44
|
self.assertFalse(asyncio.run(proxy._client_gone()))
|
|
36
45
|
|
|
37
46
|
def test_reports_disconnect(self):
|
|
38
|
-
|
|
39
|
-
return True
|
|
40
|
-
|
|
41
|
-
proxy._current_client_gone.set(gone)
|
|
47
|
+
proxy._disconnect_holder.set({"gone": True})
|
|
42
48
|
self.assertTrue(asyncio.run(proxy._client_gone()))
|
|
43
49
|
|
|
44
50
|
def test_reports_connected(self):
|
|
45
|
-
|
|
46
|
-
return False
|
|
47
|
-
|
|
48
|
-
proxy._current_client_gone.set(here)
|
|
51
|
+
proxy._disconnect_holder.set({"gone": False})
|
|
49
52
|
self.assertFalse(asyncio.run(proxy._client_gone()))
|
|
50
53
|
|
|
51
|
-
def
|
|
52
|
-
"""
|
|
53
|
-
|
|
54
|
-
async def broken() -> bool:
|
|
55
|
-
raise RuntimeError("receive channel exploded")
|
|
56
|
-
|
|
57
|
-
proxy._current_client_gone.set(broken)
|
|
54
|
+
def test_a_malformed_holder_reads_as_present(self):
|
|
55
|
+
"""Defensive: an empty dict must not be mistaken for a disconnect."""
|
|
56
|
+
proxy._disconnect_holder.set({})
|
|
58
57
|
self.assertFalse(asyncio.run(proxy._client_gone()))
|
|
59
58
|
|
|
60
59
|
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Pure-ASGI disconnect watcher — the thing that finally made the abort work.
|
|
3
|
+
|
|
4
|
+
Four attempts failed before this. The measured cause: `@app.middleware("http")`
|
|
5
|
+
is Starlette's BaseHTTPMiddleware, which runs the endpoint in a separate anyio
|
|
6
|
+
task behind its own receive channel, so request.is_disconnected() in the endpoint
|
|
7
|
+
polls a channel that never delivers http.disconnect. Instrumented in production
|
|
8
|
+
it returned False 16 times across 30s while the caller was demonstrably gone —
|
|
9
|
+
and it worked perfectly in an app with no middleware, which is exactly why three
|
|
10
|
+
rounds of isolated testing said the approach was sound.
|
|
11
|
+
|
|
12
|
+
Two properties are load-bearing and each has a test that fails if it breaks:
|
|
13
|
+
|
|
14
|
+
1. ORDER. The watcher must be OUTERMOST. Starlette's add_middleware inserts at
|
|
15
|
+
position 0, so it must be registered LAST. The first implementation
|
|
16
|
+
registered it before auth, putting it inside the very middleware whose
|
|
17
|
+
behaviour it exists to work around.
|
|
18
|
+
2. POLLING. Wrapping receive() is not enough. Once the body is fully read
|
|
19
|
+
nothing calls receive() again, so an http.disconnect sits in the channel
|
|
20
|
+
unobserved. The watcher must take over polling after the final body message.
|
|
21
|
+
|
|
22
|
+
Verified end to end after these were in place: the llama slot freed within 5s of
|
|
23
|
+
the client hanging up, against 30s+ of continued generation before.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import ast
|
|
27
|
+
import asyncio
|
|
28
|
+
import importlib.util
|
|
29
|
+
import unittest
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
PROXY_PATH = Path(__file__).resolve().parents[1] / "scripts" / "anthropic_proxy.py"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _load_proxy():
|
|
36
|
+
spec = importlib.util.spec_from_file_location("anthropic_proxy_watcher", PROXY_PATH)
|
|
37
|
+
assert spec is not None and spec.loader is not None
|
|
38
|
+
m = importlib.util.module_from_spec(spec)
|
|
39
|
+
spec.loader.exec_module(m)
|
|
40
|
+
return m
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
proxy = _load_proxy()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class MiddlewareOrderTest(unittest.TestCase):
|
|
47
|
+
def test_the_watcher_is_outermost(self):
|
|
48
|
+
"""If it ends up inside BaseHTTPMiddleware it cannot see http.disconnect,
|
|
49
|
+
which is the whole failure this replaced."""
|
|
50
|
+
names = [getattr(mw.cls, "__name__", str(mw.cls)) for mw in proxy.app.user_middleware]
|
|
51
|
+
self.assertTrue(names, "no middleware registered at all")
|
|
52
|
+
self.assertEqual(
|
|
53
|
+
names[0],
|
|
54
|
+
"DisconnectWatcherMiddleware",
|
|
55
|
+
f"watcher is not outermost; stack is {names}",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
def test_auth_middleware_is_still_installed(self):
|
|
59
|
+
"""The brief was to leave auth alone. Losing it would silently open the
|
|
60
|
+
LAN token gate — a far worse outcome than the bug being fixed."""
|
|
61
|
+
names = [getattr(mw.cls, "__name__", str(mw.cls)) for mw in proxy.app.user_middleware]
|
|
62
|
+
self.assertIn("BaseHTTPMiddleware", names)
|
|
63
|
+
|
|
64
|
+
def test_probe_is_a_flag_read_not_a_starlette_call(self):
|
|
65
|
+
"""_client_gone must not call back into the request object: that is the
|
|
66
|
+
path that measured False-forever behind BaseHTTPMiddleware.
|
|
67
|
+
|
|
68
|
+
Checked with ast rather than substring matching — the first version of
|
|
69
|
+
this test failed on the word appearing in the function's own docstring,
|
|
70
|
+
which is a false positive, not a finding.
|
|
71
|
+
"""
|
|
72
|
+
tree = ast.parse(PROXY_PATH.read_text())
|
|
73
|
+
fn = next(
|
|
74
|
+
n for n in ast.walk(tree)
|
|
75
|
+
if isinstance(n, ast.AsyncFunctionDef) and n.name == "_client_gone"
|
|
76
|
+
)
|
|
77
|
+
attrs = {
|
|
78
|
+
n.attr for n in ast.walk(fn) if isinstance(n, ast.Attribute)
|
|
79
|
+
}
|
|
80
|
+
self.assertNotIn("is_disconnected", attrs)
|
|
81
|
+
# And it must read the holder, so the test fails if the body is gutted.
|
|
82
|
+
self.assertIn("get", attrs)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class WatcherBehaviourTest(unittest.TestCase):
|
|
86
|
+
"""Drive the middleware directly with an ASGI receive/send pair."""
|
|
87
|
+
|
|
88
|
+
def _run(self, messages, app_body):
|
|
89
|
+
"""messages: what receive() yields, in order. app_body: coroutine fn(scope, receive, send)."""
|
|
90
|
+
sent = []
|
|
91
|
+
queue = list(messages)
|
|
92
|
+
|
|
93
|
+
async def receive():
|
|
94
|
+
if queue:
|
|
95
|
+
return queue.pop(0)
|
|
96
|
+
await asyncio.sleep(3600) # channel idle, like a live connection
|
|
97
|
+
|
|
98
|
+
async def send(msg):
|
|
99
|
+
sent.append(msg)
|
|
100
|
+
|
|
101
|
+
mw = proxy.DisconnectWatcherMiddleware(app_body)
|
|
102
|
+
asyncio.run(mw({"type": "http"}, receive, send))
|
|
103
|
+
return sent
|
|
104
|
+
|
|
105
|
+
def test_records_a_disconnect_the_app_itself_reads(self):
|
|
106
|
+
seen = {}
|
|
107
|
+
|
|
108
|
+
async def app(scope, receive, send):
|
|
109
|
+
await receive() # body
|
|
110
|
+
msg = await receive() # disconnect
|
|
111
|
+
seen["type"] = msg["type"]
|
|
112
|
+
seen["gone"] = await proxy._client_gone()
|
|
113
|
+
|
|
114
|
+
self._run(
|
|
115
|
+
[{"type": "http.request", "body": b"{}", "more_body": False},
|
|
116
|
+
{"type": "http.disconnect"}],
|
|
117
|
+
app,
|
|
118
|
+
)
|
|
119
|
+
self.assertEqual(seen["type"], "http.disconnect")
|
|
120
|
+
self.assertTrue(seen["gone"], "watcher did not record the disconnect")
|
|
121
|
+
|
|
122
|
+
def test_records_a_disconnect_the_app_NEVER_reads(self):
|
|
123
|
+
"""The real case. After the body is consumed the app never calls receive()
|
|
124
|
+
again, so the disconnect is only ever seen by the watcher's own poller.
|
|
125
|
+
Wrapping receive() alone would leave `gone` False here forever."""
|
|
126
|
+
seen = {}
|
|
127
|
+
|
|
128
|
+
async def app(scope, receive, send):
|
|
129
|
+
await receive() # body only — then just "work", like a generation
|
|
130
|
+
for _ in range(40):
|
|
131
|
+
await asyncio.sleep(0.02)
|
|
132
|
+
if await proxy._client_gone():
|
|
133
|
+
seen["gone"] = True
|
|
134
|
+
return
|
|
135
|
+
seen["gone"] = False
|
|
136
|
+
|
|
137
|
+
self._run(
|
|
138
|
+
[{"type": "http.request", "body": b"{}", "more_body": False},
|
|
139
|
+
{"type": "http.disconnect"}],
|
|
140
|
+
app,
|
|
141
|
+
)
|
|
142
|
+
self.assertTrue(seen.get("gone"), "poller did not observe the unread disconnect")
|
|
143
|
+
|
|
144
|
+
def test_a_live_request_is_never_marked_gone(self):
|
|
145
|
+
"""A false positive would abort healthy turns — worse than the bug."""
|
|
146
|
+
seen = {}
|
|
147
|
+
|
|
148
|
+
async def app(scope, receive, send):
|
|
149
|
+
await receive()
|
|
150
|
+
for _ in range(10):
|
|
151
|
+
await asyncio.sleep(0.02)
|
|
152
|
+
seen["gone"] = await proxy._client_gone()
|
|
153
|
+
|
|
154
|
+
self._run([{"type": "http.request", "body": b"{}", "more_body": False}], app)
|
|
155
|
+
self.assertFalse(seen["gone"])
|
|
156
|
+
|
|
157
|
+
def test_non_http_scopes_pass_straight_through(self):
|
|
158
|
+
"""Lifespan and websocket scopes have no disconnect semantics here and
|
|
159
|
+
must not be wrapped."""
|
|
160
|
+
called = {}
|
|
161
|
+
|
|
162
|
+
async def app(scope, receive, send):
|
|
163
|
+
called["type"] = scope["type"]
|
|
164
|
+
|
|
165
|
+
async def go():
|
|
166
|
+
mw = proxy.DisconnectWatcherMiddleware(app)
|
|
167
|
+
await mw({"type": "lifespan"}, None, None)
|
|
168
|
+
|
|
169
|
+
asyncio.run(go())
|
|
170
|
+
self.assertEqual(called["type"], "lifespan")
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
if __name__ == "__main__":
|
|
174
|
+
unittest.main()
|
|
@@ -115,9 +115,9 @@ class ChokePointBehaviourTest(unittest.TestCase):
|
|
|
115
115
|
return client, state, (lambda: setattr(httpx.AsyncClient, "send", original))
|
|
116
116
|
|
|
117
117
|
def _set_probe(self, gone: bool):
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
proxy.
|
|
118
|
+
# The guard now reads a shared mutable holder published by the ASGI
|
|
119
|
+
# watcher, rather than calling back into the request object.
|
|
120
|
+
proxy._disconnect_holder.set({"gone": gone})
|
|
121
121
|
|
|
122
122
|
def test_cancels_upstream_when_the_caller_leaves(self):
|
|
123
123
|
client, state, restore = self._client_with_send("never")
|
|
@@ -147,7 +147,7 @@ class ChokePointBehaviourTest(unittest.TestCase):
|
|
|
147
147
|
keep working rather than acquiring surprise cancellation."""
|
|
148
148
|
client, state, restore = self._client_with_send("fast")
|
|
149
149
|
try:
|
|
150
|
-
proxy.
|
|
150
|
+
proxy._disconnect_holder.set(None)
|
|
151
151
|
self.assertEqual(asyncio.run(client.send(self._Req())), "RESPONSE")
|
|
152
152
|
finally:
|
|
153
153
|
restore()
|