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: v0.91.0 of fastapi Cannot add middleware after an application ha… #7717

Merged
merged 1 commit into from
Feb 13, 2023

Conversation

zijiren233
Copy link
Contributor

@zijiren233 zijiren233 commented Feb 10, 2023

…s started

fix #7714

Traceback (most recent call last):
  File "launch.py", line 361, in <module>
    start()
  File "launch.py", line 356, in start
    webui.webui()
  File "/content/stable-diffusion-webui/webui.py", line 232, in webui
    app.add_middleware(GZipMiddleware, minimum_size=1000)
  File "/usr/local/lib/python3.8/dist-packages/starlette/applications.py", line 135, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started

v0.91.0 of fastapi Cannot add middleware after an application has started
https://github.com/tiangolo/fastapi/releases/tag/0.91.0

@ClashSAN ClashSAN added the upstream Issue or feature that must be resolved upstream label Feb 11, 2023
@wiwomu
Copy link

wiwomu commented Feb 11, 2023

Justed wanted to add, reverting fastapi to 'fastapi<0.91.0' works.

@usai-dns
Copy link

if youre in windows you'll get this error if you run webui.bat
run python3 launch.py and it will run no problem.

@chris-ryu
Copy link

if youre in windows you'll get this error if you run webui.bat run python3 launch.py and it will run no problem.

I did what you said and it still won't go through with the following error at the end.
Installing clip
Installing open_clip
Installing requirements for CodeFormer
Installing requirements for Web UI
Launching Web UI with arguments:
No module 'xformers'. Proceeding without it.
Loading weights [038ba203d8] from C:\Users\narzis.DESKTOP-AI02\source\repos\stable-diffusion-webui\models\Stable-diffusion\AbyssOrangeMix2_sfw.safetensors
Creating model from config: C:\Users\narzis.DESKTOP-AI02\source\repos\stable-diffusion-webui\configs\v1-inference.yaml
LatentDiffusion: Running in eps-prediction mode
DiffusionWrapper has 859.52 M params.
Applying cross attention optimization (Doggettx).
Textual inversion embeddings loaded(0):
Model loaded in 2.9s (create model: 0.4s, apply weights to model: 0.5s, apply half(): 0.5s, move model to device: 0.7s, load textual inversion embeddings: 0.7s).
Running on local URL: http://127.0.0.1:7860

To create a public link, set share=True in launch().
Traceback (most recent call last):
File "C:\Users\narzis.DESKTOP-AI02\source\repos\stable-diffusion-webui\launch.py", line 361, in
start()
File "C:\Users\narzis.DESKTOP-AI02\source\repos\stable-diffusion-webui\launch.py", line 356, in start
webui.webui()
File "C:\Users\narzis.DESKTOP-AI02\source\repos\stable-diffusion-webui\webui.py", line 232, in webui
app.add_middleware(GZipMiddleware, minimum_size=1000)
File "C:\Users\narzis.DESKTOP-AI02\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\starlette\applications.py", line 135, in add_middleware
raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started

@AUTOMATIC1111 AUTOMATIC1111 merged commit 3715ece into AUTOMATIC1111:master Feb 13, 2023
@mykeehu
Copy link
Contributor

mykeehu commented Feb 13, 2023

Colab is still having problems with this error, I just got this, at the very end of the startup:

Traceback (most recent call last):
  File "launch.py", line 361, in <module>
    start()
  File "launch.py", line 356, in start
    webui.webui()
  File "/content/stable-diffusion-webui/webui.py", line 232, in webui
    app.add_middleware(GZipMiddleware, minimum_size=1000)
  File "/usr/local/lib/python3.8/dist-packages/starlette/applications.py", line 135, in add_middleware
    raise RuntimeError("Cannot add middleware after an application has started")
RuntimeError: Cannot add middleware after an application has started

Update: it seems that modifying the txt is not enough, I had to run the following command on Colab. Wouldn't setting requirements_versions.txt work?
!pip install --upgrade fastapi==0.90.1

@usai-dns
Copy link

if youre in windows you'll get this error if you run webui.bat run python3 launch.py and it will run no problem.

I did what you said and it still won't go through with the following error at the end

raise RuntimeError("Cannot add middleware after an application has started")

RuntimeError: Cannot add middleware after an application has started

Make sure youre launching with python 3.10.6, if you arent, delete repo and clone again. revert fastapi to 0.90.

@tommyjohn81
Copy link

Dreambooth is still broken with fastapi=0.90.1

#######################################################################################################
Initializing Dreambooth
If submitting an issue on github, please provide the below text for debugging purposes:

Python revision: 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0]
Dreambooth revision: 43ae9d55531004f1dedaea7ac2443e9b16739913
SD-WebUI revision:

Checking Dreambooth requirements...
[+] bitsandbytes version 0.35.0 installed.
[+] diffusers version 0.10.2 installed.
[+] transformers version 4.25.1 installed.
[+] xformers version 0.0.16rc425 installed.
[+] torch version 1.13.1+cu117 installed.
[+] torchvision version 0.14.1+cu117 installed.

#######################################################################################################

ValueError: File cannot be fetched: /content/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser/javascript/images_history.js. All files must contained within the Gradio python app working directory, or be a temp file created by the Gradio python app.
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/opt/conda/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/opt/conda/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in call
return await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/fastapi/applications.py", line 270, in call
await super().call(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/applications.py", line 124, in call
await self.middleware_stack(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in call
raise exc
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in call
await self.app(scope, receive, _send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in call
await responder(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 43, in call
await self.app(scope, receive, self.send_with_gzip)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in call
raise exc
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call
raise e
File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 706, in call
await route.handle(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 235, in app
raw_response = await run_endpoint_function(
File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 161, in run_endpoint_function
return await dependant.call(**values)
File "/opt/conda/lib/python3.10/site-packages/gradio/routes.py", line 286, in file
raise ValueError(

DanganHat added a commit to DanganHat/stable-diffusion-webui that referenced this pull request Feb 17, 2023
@mykeehu
Copy link
Contributor

mykeehu commented Feb 20, 2023

On Colab, this extra code solved the startup problems, which must be started separately before running:

!pip install --upgrade fastapi==0.90.1
!apt-get install -y libnvinfer-dev=8.4.3-1+cuda11.6 libnvinfer-plugin-dev=8.4.3-1+cuda11.6 libnvparsers-dev=8.4.3-1+cuda11.6 libnvonnxparsers-dev=8.4.3-1+cuda11.6 python3-libnvinfer-dev=8.4.3-1+cuda11.6 libnvinfer8=8.4.3-1+cuda11.6 libnvinfer-plugin8=8.4.3-1+cuda11.6 libnvparsers8=8.4.3-1+cuda11.6 libnvonnxparsers8=8.4.3-1+cuda11.6 python3-libnvinfer=8.4.3-1+cuda11.6
!pip install protobuf
!sudo apt-get install -y graphsurgeon-tf=8.4.3-1+cuda11.6 uff-converter-tf=8.4.3-1+cuda11.6
!ln -s /usr/lib/x86_64-linux-gnu/libnvinfer.so.8.4.3 /usr/lib64-nvidia/libnvinfer.so.7
!ln -s /usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so.8.4.3 /usr/lib64-nvidia/libnvinfer_plugin.so.7

@robertalanbevan
Copy link

robertalanbevan commented Feb 24, 2023

I resolved this by "simply" cutting out these lines from webui.py:

app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']

setup_cors(app)

app.add_middleware(GZipMiddleware, minimum_size=1000)

Not that I have any idea what I'm doing, but it's the solution I would have wanted, so use at your own risk. I've had no further problems so far.

@AlonAshken
Copy link

AlonAshken commented Feb 24, 2023

@robertalanbevan the note right above what you removed says exactly what it's doing, so probably it should stay there if you can.

I just resolved this with the suggestion above to install fastapi==0.90.1

I'd try that, just in case what you did will open the door to some attacker via some addon that you've added to the system.

The note above what you deleted mentioned:
# gradio uses a very open CORS policy via app.user_middleware, which makes it possible for # an attacker to trick the user into opening a malicious HTML page, which makes a request to the # running web ui and do whatever the attacker wants, including installing an extension and # running its code. We disable this here. Suggested by RyotaK.

oh and @AUTOMATIC1111 since you already put there a requirements_versions.txt file but it doesn't have the fastapi, I suggest maybe you add the fastapi==0.90.1 to it and it'll help a lot of people.

Thanks for all of your awesome work :)

@tommyjohn81
Copy link

This still seems to be an ongoing issue, just did a fresh install, fastapi is set to 0.90.1 and still get the following errors:

ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/opt/conda/lib/python3.10/site-packages/uvicorn/protocols/http/h11_impl.py", line 407, in run_asgi
result = await app( # type: ignore[func-returns-value]
File "/opt/conda/lib/python3.10/site-packages/uvicorn/middleware/proxy_headers.py", line 78, in call
return await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/fastapi/applications.py", line 271, in call
await super().call(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/applications.py", line 125, in call
await self.middleware_stack(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 184, in call
raise exc
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/errors.py", line 162, in call
await self.app(scope, receive, _send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in call
await responder(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in call
await self.app(scope, receive, self.send_with_gzip)
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in call
raise exc
File "/opt/conda/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in call
await self.app(scope, receive, sender)
File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in call
raise e
File "/opt/conda/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in call
await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 706, in call
await route.handle(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
await self.app(scope, receive, send)
File "/opt/conda/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
response = await func(request)
File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
raw_response = await run_endpoint_function(
File "/opt/conda/lib/python3.10/site-packages/fastapi/routing.py", line 163, in run_endpoint_function
return await dependant.call(**values)
File "/opt/conda/lib/python3.10/site-packages/gradio/routes.py", line 286, in file
raise ValueError(
ValueError: File cannot be fetched: /content/stable-diffusion-webui/extensions/stable-diffusion-webui-images-browser/javascript/images_history.js. All files must contained within the Gradio python app working directory, or be a temp file created by the Gradio python app.

@emotionaldreams
Copy link

I resolved this by "simply" cutting out these lines from webui.py:

app.user_middleware = [x for x in app.user_middleware if x.cls.__name__ != 'CORSMiddleware']

setup_cors(app)

app.add_middleware(GZipMiddleware, minimum_size=1000)

Not that I have any idea what I'm doing, but it's the solution I would have wanted, so use at your own risk. I've had no further problems so far.

thsi worked for me..thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
upstream Issue or feature that must be resolved upstream
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: fastapi 0.91 app.add_middleware