Skip to content

Commit

Permalink
Merge branch 'main' into expose_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
ahopkins committed Apr 24, 2022
2 parents 14a2094 + 3a6cc73 commit ac0c58c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions sanic/__init__.py
Expand Up @@ -4,6 +4,7 @@
from sanic.constants import HTTPMethod
from sanic.request import Request
from sanic.response import HTTPResponse, html, json, text
from sanic.server.websockets.impl import WebsocketImplProtocol as Websocket


__all__ = (
Expand All @@ -13,6 +14,7 @@
"HTTPMethod",
"HTTPResponse",
"Request",
"Websocket",
"html",
"json",
"text",
Expand Down
2 changes: 1 addition & 1 deletion sanic/__version__.py
@@ -1 +1 @@
__version__ = "22.3.0"
__version__ = "22.3.1"
8 changes: 7 additions & 1 deletion sanic/app.py
Expand Up @@ -252,7 +252,13 @@ def loop(self):
"Loop can only be retrieved after the app has started "
"running. Not supported with `create_server` function"
)
return get_running_loop()
try:
return get_running_loop()
except RuntimeError:
if sys.version_info > (3, 10):
return asyncio.get_event_loop_policy().get_event_loop()
else:
return asyncio.get_event_loop()

# -------------------------------------------------------------------- #
# Registration
Expand Down
2 changes: 1 addition & 1 deletion sanic/compat.py
Expand Up @@ -72,7 +72,7 @@ async def stay_active(app):
"""Asyncio wakeups to allow receiving SIGINT in Python"""
while not die:
# If someone else stopped the app, just exit
if app.is_stopping:
if app.state.is_stopping:
return
# Windows Python blocks signal handlers while the event loop is
# waiting for I/O. Frequent wakeups keep interrupts flowing.
Expand Down
12 changes: 7 additions & 5 deletions tests/test_signal_handlers.py
Expand Up @@ -3,6 +3,7 @@
import signal

from queue import Queue
from types import SimpleNamespace
from unittest.mock import MagicMock

import pytest
Expand Down Expand Up @@ -74,11 +75,12 @@ def test_windows_workaround():
# Windows...
class MockApp:
def __init__(self):
self.is_stopping = False
self.state = SimpleNamespace()
self.state.is_stopping = False

def stop(self):
assert not self.is_stopping
self.is_stopping = True
assert not self.state.is_stopping
self.state.is_stopping = True

def add_task(self, func):
loop = asyncio.get_event_loop()
Expand All @@ -91,11 +93,11 @@ async def atest(stop_first):
if stop_first:
app.stop()
await asyncio.sleep(0.2)
assert app.is_stopping == stop_first
assert app.state.is_stopping == stop_first
# First Ctrl+C: should call app.stop() within 0.1 seconds
os.kill(os.getpid(), signal.SIGINT)
await asyncio.sleep(0.2)
assert app.is_stopping
assert app.state.is_stopping
assert app.stay_active_task.result() is None
# Second Ctrl+C should raise
with pytest.raises(KeyboardInterrupt):
Expand Down

0 comments on commit ac0c58c

Please sign in to comment.