-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix username logging on 500 responses. (#754)
* Fix username logging on 500 responses. A bug causes the server to send no response, such that the client raised: ``` RemoteProtocolError: Server disconnected without sending a response. ``` instead of returning 500 response. This was a regression introduced in #750. Needs test. Closes #750 * Add comment * Refine comments * TST: Reproduce 'Server disconnected without sending a response' in test. * Update CHANGELOG * Clean up prints in test * Refactor so server can be shut down.
- Loading branch information
1 parent
9059fd0
commit 3c19b6d
Showing
4 changed files
with
87 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import contextlib | ||
import threading | ||
import time | ||
|
||
import uvicorn | ||
from fastapi import APIRouter | ||
from starlette.status import HTTP_500_INTERNAL_SERVER_ERROR | ||
|
||
from ..catalog import in_memory | ||
from ..client import from_uri | ||
from ..server.app import build_app | ||
from ..server.logging_config import LOGGING_CONFIG | ||
|
||
router = APIRouter() | ||
|
||
|
||
class Server(uvicorn.Server): | ||
# https://github.com/encode/uvicorn/discussions/1103#discussioncomment-941726 | ||
|
||
def install_signal_handlers(self): | ||
pass | ||
|
||
@contextlib.contextmanager | ||
def run_in_thread(self): | ||
thread = threading.Thread(target=self.run) | ||
thread.start() | ||
try: | ||
# Wait for server to start up, or raise TimeoutError. | ||
for _ in range(100): | ||
time.sleep(0.1) | ||
if self.started: | ||
break | ||
else: | ||
raise TimeoutError("Server did not start in 10 seconds.") | ||
host, port = self.servers[0].sockets[0].getsockname() | ||
yield f"http://{host}:{port}" | ||
finally: | ||
self.should_exit = True | ||
thread.join() | ||
|
||
|
||
@router.get("/error") | ||
def error(): | ||
1 / 0 # error! | ||
|
||
|
||
def test_500_response(): | ||
""" | ||
Test that unexpected server error returns 500 response. | ||
This test is meant to catch regressions in which server exceptions can | ||
result in the server sending no response at all, leading clients to raise | ||
like: | ||
httpx.RemoteProtocolError: Server disconnected without sending a response. | ||
This can happen when bugs are introduced in the middleware layer. | ||
""" | ||
API_KEY = "secret" | ||
catalog = in_memory() | ||
app = build_app(catalog, {"single_user_api_key": API_KEY}) | ||
app.include_router(router) | ||
config = uvicorn.Config(app, port=0, loop="asyncio", log_config=LOGGING_CONFIG) | ||
server = Server(config) | ||
|
||
with server.run_in_thread() as url: | ||
client = from_uri(url, api_key=API_KEY) | ||
response = client.context.http_client.get(f"{url}/error") | ||
assert response.status_code == HTTP_500_INTERNAL_SERVER_ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters