AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
136.18k stars 25.96k forks source link

[Bug]: API error: change() and other events can only be called within a Blocks context. #10884

Open SombreCode opened 1 year ago

SombreCode commented 1 year ago

Is there an existing issue for this?

What happened?

when CALL txt2img api, return 500 Internal Server Error : {'error': 'AttributeError', 'detail': '', 'body': '', 'errors': 'click() and other events can only be called within a Blocks context.'}. restart program can temporarily resolve this problem.

Steps to reproduce the problem

possible occurrence when start program

What should have happened?

API return 200 and works fine

Commit where the problem happens

https://github.com/AUTOMATIC1111/stable-diffusion-webui/commit/89f9faa63388756314e8a1d96cf86bf5e0663045

What Python version are you running on ?

Python 3.10.x

What platforms do you use to access the UI ?

Linux

What device are you running WebUI on?

No response

What browsers do you use to access the UI ?

No response

Command Line Arguments

--no-download-sd-model --autolaunch --nowebui --api

List of extensions

No

Console logs

API error: POST: /sdapi/v1/txt2img {'error': 'AttributeError', 'detail': '', 'body': '', 'errors': 'click() and other events can only be called within a Blocks context.'}
INFO:     None:0 - "POST /sdapi/v1/txt2img HTTP/1.1" 500 Internal Server Error
Traceback (most recent call last):
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 94, in receive
    return self.receive_nowait()
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 89, in receive_nowait
    raise WouldBlock
anyio.WouldBlock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 78, in call_next
    message = await recv_stream.receive()
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/streams/memory.py", line 114, in receive
    raise EndOfStream
anyio.EndOfStream

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/fish/stable-diffusion-webui/modules/api/api.py", line 144, in exception_handling
    return await call_next(request)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 108, in __call__
    response = await self.dispatch_func(request, call_next)
  File "/root/fish/stable-diffusion-webui/modules/api/api.py", line 109, in log_and_time
    res: Response = await call_next(req)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 84, in call_next
    raise app_exc
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/base.py", line 70, in coro
    await self.app(scope, receive_or_disconnect, send_no_error)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 24, in __call__
    await responder(scope, receive, send)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/gzip.py", line 44, in __call__
    await self.app(scope, receive, self.send_with_gzip)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 79, in __call__
    raise exc
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/middleware/exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 21, in __call__
    raise e
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/middleware/asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 718, in __call__
    await route.handle(scope, receive, send)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/routing.py", line 66, in app
    response = await func(request)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/routing.py", line 237, in app
    raw_response = await run_endpoint_function(
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/fastapi/routing.py", line 165, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/starlette/concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "/root/fish/stable-diffusion-webui/modules/api/api.py", line 283, in text2imgapi
    ui.create_ui()
  File "/root/fish/stable-diffusion-webui/modules/ui.py", line 1051, in create_ui
    ui_postprocessing.create_ui()
  File "/root/fish/stable-diffusion-webui/modules/ui_postprocessing.py", line 25, in create_ui
    script_inputs = scripts.scripts_postproc.setup_ui()
  File "/root/fish/stable-diffusion-webui/modules/scripts_postprocessing.py", line 113, in setup_ui
    self.create_script_ui(script, inputs)
  File "/root/fish/stable-diffusion-webui/modules/scripts_postprocessing.py", line 84, in create_script_ui
    for control in script.controls.values():
AttributeError: 'NoneType' object has no attribute 'values'

Additional information

other error print

calling /root/fish/stable-diffusion-webui/scripts/postprocessing_upscale.py/ui: AttributeError
Traceback (most recent call last):
  File "/root/fish/stable-diffusion-webui/modules/scripts_postprocessing.py", line 53, in wrap_call
    res = func(*args, **kwargs)
  File "/root/fish/stable-diffusion-webui/scripts/postprocessing_upscale.py", line 42, in ui
    upscaling_res_switch_btn.click(lambda w, h: (h, w), inputs=[upscaling_resize_w, upscaling_resize_h], outputs=[upscaling_resize_w, upscaling_resize_h], show_progress=False)
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/events.py", line 132, in __call__
    dep, dep_index = self.trigger.set_event_trigger(
  File "/root/fish/stable-diffusion-webui/venv/lib/python3.10/site-packages/gradio/blocks.py", line 221, in set_event_trigger
    raise AttributeError(
AttributeError: click() and other events can only be called within a Blocks context.
webcoderz commented 1 year ago

running into this too any luck?

AtlantiseD commented 1 year ago

I have the same issue, and after many many trying, I found that : When the stable-diffusion-web start with --nowebui, the first request must completely finished then you can use multi thread to request。demo code:

`

@Test
public void badRequestSample() throws ExecutionException, InterruptedException {
    CompletableFuture<?> future1 = CompletableFuture.runAsync(() ->text2ImgTest("black cat"));
    CompletableFuture<?> future2 = CompletableFuture.runAsync(() ->text2ImgTest("white horse"));
    CompletableFuture<?> future3 = CompletableFuture.runAsync(() ->text2ImgTest("high tree"));
    CompletableFuture.allOf(future1, future2, future3).get();
}

@Test
public void goodRequestSample() throws ExecutionException, InterruptedException {
    CompletableFuture<?> future1 = CompletableFuture.runAsync(() ->text2ImgTest("black cat"));
    future1.get();

    CompletableFuture<?> future2 = CompletableFuture.runAsync(() ->text2ImgTest("white horse"));
    CompletableFuture<?> future3 = CompletableFuture.runAsync(() ->text2ImgTest("high tree"));
    CompletableFuture.allOf(future2, future3).get();
}

`

I don't konw why but I guess the error happens in gradio:3.2.0 , I not sure as you can see, I am a java programmer

webcoderz commented 1 year ago

yea i found it was an extension blocking context, so when i uninstalled extension it worked, so i reached out to the affected extension with an issue

bigahega commented 1 year ago

I have the same issue, and after many many trying, I found that : When the stable-diffusion-web start with --nowebui, the first request must completely finished then you can use multi thread to request。demo code:

`

@Test
public void badRequestSample() throws ExecutionException, InterruptedException {
  CompletableFuture<?> future1 = CompletableFuture.runAsync(() ->text2ImgTest("black cat"));
  CompletableFuture<?> future2 = CompletableFuture.runAsync(() ->text2ImgTest("white horse"));
  CompletableFuture<?> future3 = CompletableFuture.runAsync(() ->text2ImgTest("high tree"));
  CompletableFuture.allOf(future1, future2, future3).get();
}

@Test
public void goodRequestSample() throws ExecutionException, InterruptedException {
  CompletableFuture<?> future1 = CompletableFuture.runAsync(() ->text2ImgTest("black cat"));
  future1.get();

  CompletableFuture<?> future2 = CompletableFuture.runAsync(() ->text2ImgTest("white horse"));
  CompletableFuture<?> future3 = CompletableFuture.runAsync(() ->text2ImgTest("high tree"));
  CompletableFuture.allOf(future2, future3).get();
}

`

I don't konw why but I guess the error happens in gradio:3.2.0 , I not sure as you can see, I am a java programmer

this issue is indeed easily reproducible following this approach. default args of scripts are initialized in the first txt2img and img2img api calls. if you send multiple requests to the txt2img / img2img api endpoints at the same time, they break. fastapi uses an internal threadpool to execute non-async endpoints so apparently when two threads try to initialize default script args, bad things happen.

swayam1998 commented 10 months ago

Running into the same issue. Did anyone find a solution for this?

shelltea commented 9 months ago

yea i found it was an extension blocking context, so when i uninstalled extension it worked, so i reached out to the affected extension with an issue是的,我发现是一个扩展程序阻止了上下文,所以当我卸载了扩展程序后,问题就解决了,所以我联系了受影响的扩展程序以解决问题

@webcoderz Hello, can you tell me which extension caused this issue?

webcoderz commented 9 months ago

@shelltea https://github.com/Interpause/auto-sd-paint-ext

henbucuoshanghai commented 9 months ago

how to use the txt2img API?can u give a demo ?tks

lanyeeee commented 1 month ago

@henbucuoshanghai https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/API