You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a situation where I'm using FastApi with starlette and have few endpoints with a following timeout check (these endpoints don't use async internally):
timeout=1.0 threading.get_native_id()=443875
sleeping for duration=2.0
threading.get_native_id()=443814
and traceback:
venv/lib/python3.11/site-packages/starlette/testclient.py:591: in post
return super().post(
venv/lib/python3.11/site-packages/httpx/_client.py:1132: in post
return self.request(
venv/lib/python3.11/site-packages/starlette/testclient.py:466: in request
return super().request(
venv/lib/python3.11/site-packages/httpx/_client.py:814: in request
return self.send(request, auth=auth, follow_redirects=follow_redirects)
venv/lib/python3.11/site-packages/httpx/_client.py:901: in send
response = self._send_handling_auth(
venv/lib/python3.11/site-packages/httpx/_client.py:929: in _send_handling_auth
response = self._send_handling_redirects(
venv/lib/python3.11/site-packages/httpx/_client.py:966: in _send_handling_redirects
response = self._send_single_request(request)
venv/lib/python3.11/site-packages/httpx/_client.py:1002: in _send_single_request
response = transport.handle_request(request)
venv/lib/python3.11/site-packages/starlette/testclient.py:343: in handle_request
raise exc
venv/lib/python3.11/site-packages/starlette/testclient.py:340: in handle_request
portal.call(self.app, scope, receive, send)
venv/lib/python3.11/site-packages/anyio/from_thread.py:277: in call
return cast(T_Retval, self.start_task_soon(func, *args).result())
/usr/lib/python3.11/concurrent/futures/_base.py:451: in result
self._condition.wait(timeout)
/usr/lib/python3.11/threading.py:327: in wait
waiter.acquire()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
signum = 14
frame = <frame at 0x7f1b00e23680, file '/usr/lib/python3.11/threading.py', line 339, code wait>
def timeout_handler(signum: int, frame: Any) -> None:
print(f"{threading.get_native_id()=}")
> raise TimeoutException(f"end of time {frame}")
E TimeoutException: end of time <frame at 0x7f1b00e23680, file '/usr/lib/python3.11/threading.py', line 327, code wait>
app.py:33: TimeoutException
Based on my search it's because signals are sent to the main thread and testclient is riunning the server in a different thread. Sadly I'm not aware of any other way to ensure that a task is terminated after given amount of time (it's a CPU bound task).
Is there anything that can be done so that I can include this test? (Other than move it to integration level and test against uvicorn)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello,
I have a situation where I'm using FastApi with starlette and have few endpoints with a following timeout check (these endpoints don't use async internally):
and set up using
Example endpoint:
When triggering the timeout check using uvicorn, everything looks OK, but in a test the signal gets sent to a different thread id.
with output for example:
and traceback:
Based on my search it's because signals are sent to the main thread and testclient is riunning the server in a different thread. Sadly I'm not aware of any other way to ensure that a task is terminated after given amount of time (it's a CPU bound task).
Is there anything that can be done so that I can include this test? (Other than move it to integration level and test against uvicorn)
Beta Was this translation helpful? Give feedback.
All reactions