sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18.05k stars 1.55k forks source link

Sanic doesn't shutdown cleanly on Mac #2684

Closed tylerprete closed 1 year ago

tylerprete commented 1 year ago

Is there an existing issue for this?

Describe the bug

When running a simple server on mac os 13.1, after using ctrl-c to shutdown the app, a socket exception is thrown instead of a graceful shutdown

python3 helloworld.py
[2023-02-14 12:23:23 -0700] [6169] [DEBUG] Creating multiprocessing context using 'spawn'
[2023-02-14 12:23:23][DEBUG] Creating multiprocessing context using 'spawn'
[2023-02-14 12:23:23 -0700] [6169] [DEBUG] Starting a process: Sanic-Server-0-0
[2023-02-14 12:23:23][DEBUG] Starting a process: Sanic-Server-0-0
[2023-02-14 12:23:24 -0700] [6175] [DEBUG] Process ack: Sanic-Server-0-0 [6175]
[2023-02-14 12:23:24][DEBUG] Process ack: Sanic-Server-0-0 [6175]
[2023-02-14 12:23:24 -0700] [6175] [INFO] Starting worker [6175]
[2023-02-14 12:23:24][INFO] Starting worker [6175]
^C[2023-02-14 12:23:26 -0700] [6169] [INFO] Received signal SIGINT. Shutting down.
[2023-02-14 12:23:26][INFO] Received signal SIGINT. Shutting down.
[2023-02-14 12:23:26 -0700] [6169] [DEBUG] Terminating a process: Sanic-Server-0-0 [6175]
[2023-02-14 12:23:26][DEBUG] Terminating a process: Sanic-Server-0-0 [6175]
[2023-02-14 12:23:26 -0700] [6169] [INFO] Server Stopped
[2023-02-14 12:23:26][INFO] Server Stopped
Traceback (most recent call last):
  File "/Users/tylerprete/sandbox/asana/asana2/asana/server/kube_app/apps/helloworld/helloworld.py", line 22, in <module>
    app.run(host="127.0.0.1", port=8086, debug=True)
  File "/usr/local/lib/python3.9/site-packages/sanic/mixins/startup.py", line 209, in run
    serve(primary=self)  # type: ignore
  File "/usr/local/lib/python3.9/site-packages/sanic/mixins/startup.py", line 880, in serve
    sock.shutdown(SHUT_RDWR)
OSError: [Errno 57] Socket is not connected
[2023-02-14 12:23:26 -0700] [6175] [INFO] Stopping worker [6175]
[2023-02-14 12:23:26][INFO] Stopping worker [6175]

Code snippet

from sanic import Sanic
from sanic.response import html, text

app = Sanic("helloworld")

@app.get("/")
def hello_world(request):
    print("Serving /")
    return html("<p>Hello, World!</p>")

if __name__ == "__main__":
    app.run(host="127.0.0.1", port=8086, debug=True)

Expected Behavior

On linux I run this and get the following (removing the sanic banners for brevity):

python3 helloworld.py
[2023-02-14 19:17:43 +0000] [23570] [DEBUG] Creating multiprocessing context using 'spawn'
[2023-02-14 19:17:43][DEBUG] Creating multiprocessing context using 'spawn'
[2023-02-14 19:17:43 +0000] [23570] [DEBUG] Starting a process: Sanic-Server-0-0
[2023-02-14 19:17:43][DEBUG] Starting a process: Sanic-Server-0-0
[2023-02-14 19:17:43 +0000] [23579] [DEBUG] Process ack: Sanic-Server-0-0 [23579]
[2023-02-14 19:17:43][DEBUG] Process ack: Sanic-Server-0-0 [23579]
[2023-02-14 19:17:43 +0000] [23579] [INFO] Starting worker [23579]
[2023-02-14 19:17:43][INFO] Starting worker [23579]
^C[2023-02-14 19:17:45 +0000] [23570] [INFO] Received signal SIGINT. Shutting down.
[2023-02-14 19:17:45][INFO] Received signal SIGINT. Shutting down.
[2023-02-14 19:17:45 +0000] [23570] [DEBUG] Terminating a process: Sanic-Server-0-0 [23579]
[2023-02-14 19:17:45][DEBUG] Terminating a process: Sanic-Server-0-0 [23579]
[2023-02-14 19:17:45 +0000] [23570] [INFO] Server Stopped
[2023-02-14 19:17:45][INFO] Server Stopped
[2023-02-14 19:17:45 +0000] [23579] [INFO] Stopping worker [23579]
[2023-02-14 19:17:45][INFO] Stopping worker [23579]

How do you run Sanic?

As a script (app.run or Sanic.serve)

Operating System

macOS Ventura 13.1

Sanic Version

22.12.0

Additional context

No response

ahopkins commented 1 year ago

Covered by this: https://github.com/sanic-org/sanic/pull/2680