AUTOMATIC1111 / stable-diffusion-webui

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

[Bug]: /sdapi/v1/txt2img endpoint not working #9046

Closed Darklobi closed 1 year ago

Darklobi commented 1 year ago

Is there an existing issue for this?

What happened?

Today when i tried to use my discord bot to make some requests to the endpoint /sdapi/v1/txt2img, i kept getting the error: "There is no current event loop in thread 'AnyIO worker thread'." Last successful generations were around 48h ago.

Steps to reproduce the problem

  1. Start fast-stable-diffusion colab version by TheLastBen colab
  2. . Make a post request using to the endpoint /sdapi/v1/txt2img (or using any other tool like Postman) 1234 12345

What should have happened?

Generate image and return success response json body with image path

Commit where the problem happens

https://github.com/AUTOMATIC1111/stable-diffusion-webui/commit/955df7751eef11bb7697e2d77f6b8a6226b21e13

What platforms do you use to access the UI ?

Other/Cloud

What browsers do you use to access the UI ?

Google Chrome

Command Line Arguments

no

List of extensions

no

Console logs

API error: POST: https://41209b1e8d4c8c9c05.gradio.live/sdapi/v1/txt2img {'error': 'RuntimeError', 'detail': '', 'body': '', 'errors': "There is no current event loop in thread 'AnyIO worker thread'."}
╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /usr/local/lib/python3.9/dist-packages/anyio/streams/memory.py:94 in receive │
│                                                                              │
│ /usr/local/lib/python3.9/dist-packages/anyio/streams/memory.py:89 in         │
│ receive_nowait                                                               │
╰──────────────────────────────────────────────────────────────────────────────╯
WouldBlock

During handling of the above exception, another exception occurred:

╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /usr/local/lib/python3.9/dist-packages/starlette/middleware/base.py:43 in    │
│ call_next                                                                    │
│                                                                              │
│ /usr/local/lib/python3.9/dist-packages/anyio/streams/memory.py:114 in        │
│ receive                                                                      │
╰──────────────────────────────────────────────────────────────────────────────╯
EndOfStream

During handling of the above exception, another exception occurred:

╭───────────────────── Traceback (most recent call last) ──────────────────────╮
│ /content/gdrive/MyDrive/sd/stable-diffusion-webui/modules/api/api.py:145 in  │
│ exception_handling                                                           │
│                                                                              │
│   144 │   │   try:                                                           │
│ ❱ 145 │   │   │   return await call_next(request)                            │
│   146 │   │   except Exception as e:                                         │
│                                                                              │
│ ╭───────────────────────────────── locals ─────────────────────────────────╮ │
│ │        call_next = <function                                             │ │
│ │                    BaseHTTPMiddleware.__call__.<locals>.call_next at     │ │
│ │                    0x7f45ac0faf70>                                       │ │
│ │                e = RuntimeError("There is no current event loop in       │ │
│ │                    thread 'AnyIO worker thread'.")                       │ │
│ │ handle_exception = <function api_middleware.<locals>.handle_exception at │ │
│ │                    0x7f45ac638dc0>                                       │ │
│ │          request = <starlette.requests.Request object at 0x7f45ac0b2790> │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
│                                                                              │
│ /usr/local/lib/python3.9/dist-packages/starlette/middleware/base.py:46 in    │
│ call_next                                                                    │
│                                                                              │
│                           ... 25 frames hidden ...                           │
│                                                                              │
│ /usr/lib/python3.9/asyncio/locks.py:81 in __init__                           │
│                                                                              │
│    80 │   │   if loop is None:                                               │
│ ❱  81 │   │   │   self._loop = events.get_event_loop()                       │
│    82 │   │   else:                                                          │
│                                                                              │
│ ╭──────────────────────────── locals ─────────────────────────────╮          │
│ │ loop = None                                                     │          │
│ │ self = <asyncio.locks.Lock object at 0x7f45ac06b0a0 [unlocked]> │          │
│ ╰─────────────────────────────────────────────────────────────────╯          │
│                                                                              │
│ /usr/lib/python3.9/asyncio/events.py:642 in get_event_loop                   │
│                                                                              │
│   641 │   │   if self._local._loop is None:                                  │
│ ❱ 642 │   │   │   raise RuntimeError('There is no current event loop in thre │
│   643 │   │   │   │   │   │   │      % threading.current_thread().name)      │
│                                                                              │
│ ╭───────────────────────────────── locals ─────────────────────────────────╮ │
│ │ self = <asyncio.unix_events._UnixDefaultEventLoopPolicy object at        │ │
│ │        0x7f45adf50820>                                                   │ │
│ ╰──────────────────────────────────────────────────────────────────────────╯ │
╰──────────────────────────────────────────────────────────────────────────────╯
RuntimeError: There is no current event loop in thread 'AnyIO worker thread'.

Additional information

No response

cbg342 commented 1 year ago

same here, must be a new thing

MeemeeLab commented 1 year ago

Can confirm, just updated my webui installation and it broke the API I guess I have to downgrade then...

ousinkou commented 1 year ago

Same

5hal1n commented 1 year ago

same

prp-e commented 1 year ago

I also encountered the same problem.

vaalberith commented 1 year ago

Same error on Colab. On local Windows PC - OK (just done fresh install).

tooothl3ss commented 1 year ago

yep. same issue in colab

prp-e commented 1 year ago

If this problem is on colab, it's a Linux problem I believe since I try to make the API work on my server. Isn't there any solution?

prp-e commented 1 year ago

Could anyone solve this?

dinilvalsan commented 1 year ago

any solution for colab?

Darklobi commented 1 year ago

i didnt find anything yet

Darklobi commented 1 year ago

sry wrong button

LuizHenriqueKS commented 1 year ago

Same here. I'm using 'git checkout 9377092a892687a8ae43ba5f7df44f0929ab2997' as a temporary solution

SuperKsa commented 1 year ago

I have the same problem

MeemeeLab commented 1 year ago

Another workaround while keeping the latest commit is to edit the modules/api/api.py. I don't think this is a good solution though, so I don't want to make a PR.

diff --git a/modules/api/api.py b/modules/api/api.py
index 518b2a61..a9cef928 100644
--- a/modules/api/api.py
+++ b/modules/api/api.py
@@ -29,6 +29,7 @@ from modules import devices
 from typing import List
 import piexif
 import piexif.helper
+import asyncio

 def upscaler_to_index(name: str):
     try:
@@ -276,6 +277,15 @@ class Api:
         return script_args

     def text2imgapi(self, txt2imgreq: StableDiffusionTxt2ImgProcessingAPI):
+        try:
+            loop = asyncio.get_event_loop()
+        except RuntimeError as e:
+            if str(e).startswith('There is no current event loop in thread'):
+                loop = asyncio.new_event_loop()
+                asyncio.set_event_loop(loop)
+            else:
+                raise
+
         script_runner = scripts.scripts_txt2img
         if not script_runner.scripts:
             script_runner.initialize_scripts(False)
briva commented 1 year ago

same

dinilvalsan commented 1 year ago

@MeemeeLab solution worked... thanks for this.

Darklobi commented 1 year ago

until some1 fix this issue, @MeemeeLab solution is working fine, thanks for the help.

WALL-E commented 1 year ago

The issue is in the source code.

def get_event_loop(self):
    """Get the event loop.
    This may be None or an instance of EventLoop.
    """
    if (self._local._loop is None and
            not self._local._set_called and
            isinstance(threading.current_thread(), threading._MainThread)):
        self.set_event_loop(self.new_event_loop())

    if self._local._loop is None:
        raise RuntimeError('There is no current event loop in thread %r.'
                            % threading.current_thread().name)

    return self._local._loop
MeemeeLab commented 1 year ago

The issue is in the source code.

def get_event_loop(self):
    """Get the event loop.
    This may be None or an instance of EventLoop.
    """
    if (self._local._loop is None and
            not self._local._set_called and
            isinstance(threading.current_thread(), threading._MainThread)):
        self.set_event_loop(self.new_event_loop())

    if self._local._loop is None:
        raise RuntimeError('There is no current event loop in thread %r.'
                            % threading.current_thread().name)

    return self._local._loop

I belive it's internal code of asyncio, there's should be a mistake in the code, not in the library.

Vuhiep190297 commented 1 year ago

Is there any solution to fix the error on colab?

lzghades commented 1 year ago

Is there any solution to fix the error?

bennettwbrown commented 1 year ago

Experiencing this issue on img2img generation as well txt2img. @MeemeeLab worked but needed to add that code snippet into both functions.

Doublefire-Chen commented 1 year ago

Same

somya-15 commented 1 year ago

It's been quite long since this problem has been persisting in the /sdapi/v1/txt2img

Pan-dz commented 1 year ago

May I ask you to solve this problem yet? I'm also having this troublesome problem now

Darklobi commented 1 year ago

Its back to normal thanks.