sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.

Home Page:https://sanic.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

INSPECTOR worker may handle HTTP request unexpectly when `Sanic.start_method="fork"` and `INSPECTOR=True`

LCBHSStudent opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When I set Sanic's start_method to fork and enable INSPECTOR, the HTTP requests I send may unexpectedly be processed by the INSPECTOR worker process.

Could this be because the webserver socket inherited from the parent process is not closed when forking the child process?

If there are any suggestions, or if this behavior is expected, please let me know. Thank you to all the excellent library developers.

Code snippet

test server code:

from sanic import Sanic, Request, HTTPResponse, json
import os

Sanic.start_method = "fork"

app = Sanic("test-fork-with-inspector", inspector=True)

@app.get("/test")
async def test_handler(_: Request) -> HTTPResponse:
    return json(body={"pid": os.getpid()})

running server and sending HTTP request:

python -m sanic test_fork_with_inspector:app --worker=1 --host=127.0.0.1 --port=8080

curl -X GET http://localhost:8080/test
{"pid":1199437}  # inspector worker

curl -X GET http://localhost:8080/test
{"pid":1199435}  # nomal worker

Socket fd of inspector worker:

lsof -p 1199437 | grep LISTEN
python  1199437 user   12u     IPv4           24318245      0t0      TCP localhost:http-alt (LISTEN)
python  1199437 user   15u     IPv4           24311762      0t0      TCP localhost:6457 (LISTEN)

Socket fd of normal worker:

lsof -p 1199435 | grep LISTEN
python  1199435 tusen   12u     IPv4           24318245      0t0      TCP localhost:http-alt (LISTEN)

Expected Behavior

inspector worker should not handle regular HTTP requests

How do you run Sanic?

Sanic CLI Python3.8.10

Operating System

Ubuntu 20.04

Sanic Version

Sanic 23.12.0; Routing 23.12.0

Additional context

No response