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 API thread safe issues of txt2img and img2img #14421

Merged
merged 5 commits into from
Dec 30, 2023
Merged

fix API thread safe issues of txt2img and img2img #14421

merged 5 commits into from
Dec 30, 2023

Conversation

lanyeeee
Copy link
Contributor

@lanyeeee lanyeeee commented Dec 25, 2023

Description

TL;DR:

Function ui.create_ui and self.init_default_script_args are not thread-safe.
It causes thread-safety issues of API txt2img and img2img.
To ensure the API is thread-safe, I move the code to init

Steps to reproduce the thread-safety issues

1. Run webui with the following command

.\webui.bat --api --disable-all-extensions --nowebui

2. Run the following python script

import aiohttp
import asyncio


async def main():
    async with aiohttp.ClientSession() as session:
        task1 = session.request('POST', url='http://127.0.0.1:7861/sdapi/v1/txt2img', json={})
        task2 = session.request('POST', url='http://127.0.0.1:7861/sdapi/v1/txt2img', json={})
        task3 = session.request('POST', url='http://127.0.0.1:7861/sdapi/v1/txt2img', json={})
        await asyncio.gather(task1, task2, task3, return_exceptions=True)


asyncio.run(main())

In most cases you will see the error on the terminal which running webui.
If not encounter the error, ctrl+c cancel the webui then repeat the above steps again until the error appears

Fix issue #10884

Checklist:

@AUTOMATIC1111
Copy link
Owner

I think there might be some issues if you use same scripts.scripts_img2img for both txt2img and img2img. Also, does this impact startup time?

@lanyeeee
Copy link
Contributor Author

Also, does this impact startup time?

I don't think it will cause a noticeable impact on startup time.

But to make sure it doesn't impact startup time.
I give up move the code to __init__, instead use locks

@AUTOMATIC1111
Copy link
Owner

Actually, I'm sorry to say it now, but I liked the original version without locks a lot more. If there's no noticeable impact, I'd rather have that, just with two script_runners instead of one.

@AUTOMATIC1111 AUTOMATIC1111 merged commit 79c9151 into AUTOMATIC1111:dev Dec 30, 2023
3 checks passed
@w-e-w w-e-w mentioned this pull request Feb 17, 2024
@pawel665j pawel665j mentioned this pull request Apr 16, 2024
CastielMa added a commit to CastielMa/stable-diffusion-webui that referenced this pull request May 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants