Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Server set_wakeup_fd fix #1

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/py/extra/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .utils.codec import BytesTransform
from .utils.limits import LimitType, unlimit
from .model import Application, Service, mount
import threading
from .http.model import (
HTTPRequest,
HTTPResponse,
Expand Down Expand Up @@ -52,6 +53,7 @@ class ServerOptions(NamedTuple):
# SEE: https://repost.aws/questions/QU-_rSWDtwSmOD5wBO5tsrwg/load-balancer-502-bad-gateway
keepalive: float = 3_600
logRequests: bool = True
stopSignals: bool = True
condition: Callable[[], bool] | None = None


Expand Down Expand Up @@ -371,9 +373,15 @@ async def Serve(

# Manage server state
state = ServerState()
# Registers handlers for signals and exception (so that we log them)
loop.add_signal_handler(SIGINT, lambda: state.stop())
loop.add_signal_handler(SIGTERM, lambda: state.stop())
# Registers handlers for signals and exception (so that we log them). Note
# that we'll get a `set_wakeup_fd only works in main thread of the main interpreter`
# when this is not run out of the main thread.
if (
options.stopSignals
and threading.current_thread() is threading.main_thread()
):
loop.add_signal_handler(SIGINT, lambda: state.stop())
loop.add_signal_handler(SIGTERM, lambda: state.stop())
loop.set_exception_handler(state.onException)

info(
Expand Down
Loading