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

Feature Request: reload after error #425

Closed
valgarf opened this issue Sep 16, 2019 · 12 comments
Closed

Feature Request: reload after error #425

valgarf opened this issue Sep 16, 2019 · 12 comments

Comments

@valgarf
Copy link

valgarf commented Sep 16, 2019

Even after the subprocess crashes, uvicorn should still reload, i.e. restart the process after the next file change. Maybe with an additional command line flag (--reload-after-error).

Not having to restart the server after every syntax error would help during development.

@tomchristie
Copy link
Member

tomchristie commented Oct 7, 2019

Slightly depends on what you mean.
We recommend using Gunicorn (or another process monitor) in production, as that'll handle hard process restarts.

Incidentally, how is the subprocess crashing? Python exceptions from the child application should just end up logging as errors. Is it something more serious than that?

@valgarf
Copy link
Author

valgarf commented Oct 8, 2019

It reliably crashes from syntax errors in my code. This would be a pure development feature, as the whole 'reload after file change' logic should have nothing to do with production code in any way.

When the subprocess crashes, the reloading after file changes is stopped, you have an explicit check process.is_alive() in your loop.

In case anyone else finds this discussion, I monkeypatched it for my use case:

from uvicorn.supervisors.statreload import HANDLED_SIGNALS, StatReload

def statreload_run(self, target, *args, **kwargs):
    pid = os.getpid()
    logger = self.config.logger_instance

    logger.info("Started reloader process [{}]".format(pid))

    for sig in HANDLED_SIGNALS:
        signal.signal(sig, self.handle_exit)

    def get_subprocess():
        spawn = multiprocessing.get_context("spawn")
        try:
            fileno = sys.stdin.fileno()
        except OSError:
            fileno = None

        return spawn.Process(
            target=self.handle_fds, args=(target, fileno), kwargs=kwargs
        )

    process = get_subprocess()
    process.start()

    # Changes are in this loop. No check if the process is alive in the loop
    while not self.should_exit:
        time.sleep(0.3)
        if self.should_restart():
            self.clear()
            if process.is_alive():
                os.kill(process.pid, signal.SIGTERM)
                process.join()

            process = get_subprocess()
            process.start()
            self.reload_count += 1

    logger.info("Stopping reloader process [{}]".format(pid))

StatReload.run = statreload_run

@tomchristie
Copy link
Member

Ah right, I was misunderstanding slightly - I though you were talking about runtime errors in a deployed version, rather than syntax errors during development (which yes, will just quit the server).

@maxfurman
Copy link

Can this issue be reopened? I am using uvicorn --reload for development and it is a hassle to restart the server every time I accidentally save a syntax error.

@tomchristie
Copy link
Member

Sure thing, let's put it on the list.

@gnat
Copy link

gnat commented Dec 10, 2019

+1 for this.

@anand870
Copy link

Any update on this?

@tomchristie
Copy link
Member

Folks, if there's an update on a ticket, there'll be an update on the ticket. Please don't do issue-spam.

@rafalp rafalp reopened this Apr 10, 2020
@rafalp
Copy link
Member

rafalp commented Apr 23, 2020

It appears that this issue no longer exists as of Uvicorn 0.11.

@rafalp rafalp closed this as completed Apr 23, 2020
@kungfoome
Copy link

This is still an issue for me Running uvicorn 0.11.8 with CPython 3.8.5 on Windows

@nareto
Copy link

nareto commented Jan 10, 2022

Still an issue for me in 0.16.0. This makes it unusable for me because I like to have autosave enabled in my code editor, so as I write code it restarts and crashes.

@Kludex
Copy link
Member

Kludex commented Jan 10, 2022

Still an issue for me in 0.16.0. This makes it unusable for me because I like to have autosave enabled in my code editor, so as I write code it restarts and crashes.

It's not the same issue, but a fix is already provided on #1313

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

No branches or pull requests

9 participants