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 Ctrl+C and tests on Windows. #1808

Merged
merged 29 commits into from
Mar 26, 2020
Merged

Conversation

Tronic
Copy link
Member

@Tronic Tronic commented Mar 18, 2020

Alternative signal handling logic for Windows (fix #1753) and allow green CI pipelines again.

  • Adds app.is_stopping for determining whether app.stop has been called, which is different from app not running.
  • Most tests use random ports because on Windows CI they would otherwise fail to bind.
  • Fixed deprecation warnings in tests (Sanic missing name argument).

@Tronic
Copy link
Member Author

Tronic commented Mar 18, 2020

The code adds a background task that wakes up 10 times a second so that Python can receive signals (otherwise the signal handler is only called once Sanic receives network traffic). This is of course not needed on other operating systems.

The signal handler cannot call app.stop directly because it is in signal handling context (on other OSes loop.add_signal_handler is used but this of course is not available on Windows). So, the handler sets a flag, which will be seen by the background task, causing graceful app exit. Additionally, in case of more than one Ctrl+C being received, it falls back to good old KeyboardInterrupt termination.

It took too many hours to figure all that crap out, because it is not in the docs.

Windows still cannot support multiple workers, so that side has not been considered in this fix.

@codecov
Copy link

codecov bot commented Mar 18, 2020

Codecov Report

Merging #1808 into master will increase coverage by 0.11%.
The diff coverage is 96.15%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1808      +/-   ##
==========================================
+ Coverage   91.94%   92.05%   +0.11%     
==========================================
  Files          23       23              
  Lines        2309     2342      +33     
  Branches      428      435       +7     
==========================================
+ Hits         2123     2156      +33     
  Misses        143      143              
  Partials       43       43              
Impacted Files Coverage Δ
sanic/app.py 91.90% <80.00%> (+0.06%) ⬆️
sanic/compat.py 86.20% <100.00%> (+16.97%) ⬆️
sanic/response.py 98.70% <100.00%> (+<0.01%) ⬆️
sanic/testing.py 97.97% <100.00%> (+0.02%) ⬆️
sanic/request.py 99.63% <0.00%> (+0.01%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 60b4efa...b3a38b7. Read the comment docs.

@Tronic Tronic changed the title Fix Ctrl+C on Windows. Fix Ctrl+C and tests on Windows. Mar 21, 2020
@Tronic
Copy link
Member Author

Tronic commented Mar 23, 2020

Okay, I think I'm finished with this. Care to review?

Copy link
Member

@yunstanford yunstanford left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!

@@ -203,7 +207,7 @@ def __init__(

self.app = app

dispatch = SanicASGIDispatch(app=app, client=(ASGI_HOST, PORT))
dispatch = SanicASGIDispatch(app=app, client=(ASGI_HOST, PORT or 0))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 0 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing says it needs to be int, so None causes a failure. Either the type hint should be changed to Optional[int] or 0 be used instead but I did not look at ASGI code to see how this is actually handled. In any case, 0 is the standard way to request for a free random port.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually all of Sanic should probably be changed to accept 0 port. Currently entering zero gives port 8000. But that is a potentially breaking change, and for another PR even if implemented.

sanic/server.py Outdated
"Sanic tried to use loop.add_signal_handler "
"but it is not implemented on this platform."
)
if os.name == "nt":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's define a constant and use that instead of string literal directly

Copy link
Member Author

@Tronic Tronic Mar 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a constant in server.py but did not change this in skipif of OS-dependent tests.

Do note that os.name is the more coarse platform definition where all Windows are "nt", where sys.platform uses many names for Windows (and quite confusingly, 64 bit Python on 64 bit Windows is still called "win32").

@@ -1177,6 +1178,7 @@ def run(

try:
self.is_running = True
self.is_stopping = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the diff between is_running and is_stopping ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the app has not yet started, both are False. After calling app.stop the app may keep running for a while, so in that period both are True. Asyncio implementation uses loop._stopping in a similar fashion but uvloop doesn't have that and depending on private parts of the implementation is ugly in any case.

@@ -114,6 +115,9 @@ def _collect_request(request):
url = "{scheme}://{host}:{port}{uri}".format(
scheme=scheme, host=host, port=port, uri=uri
)
# Tests construct URLs using PORT = None, which means random port not
# known until this function is called, so fix that here
url = url.replace(":None/", f":{port}/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a little bit concern on this, probably should avoid this kinda of hacky pattern.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoiding this would require major restructuring of testing.py which I am not quite up to, or rewriting/disabling all tests that depend on this.

@yunstanford yunstanford merged commit 120f026 into sanic-org:master Mar 26, 2020
@Tronic Tronic deleted the ctrlc-windows branch March 27, 2020 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Any plans to support Ctrl+C on Windows?
2 participants