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

Cleanup doesn't run when exception occurs in startup. #4799

Closed
Dreamsorcerer opened this issue Jun 6, 2020 · 5 comments · Fixed by #5263
Closed

Cleanup doesn't run when exception occurs in startup. #4799

Dreamsorcerer opened this issue Jun 6, 2020 · 5 comments · Fixed by #5263

Comments

@Dreamsorcerer
Copy link
Member

When an exception occurs in startup, the cleanup is never run.

Expected behaviour: Any cleanup steps added should be run, especially when using the cleanup-context.

e.g.
Task A = Setup DB as cleanup context.
Task B = Setup a remote connection as cleanup context. This throws an exception.

Actual behaviour:
Task A is successfully setup.
Task B creates an exception.
No cleanup happens.

Expected behaviour:
Task A is successfully setup.
Task B creates an exception.
Task A cleanup happens.

@asvetlov
Copy link
Member

Please use cleanup_ctx instead

@Dreamsorcerer
Copy link
Member Author

@asvetlov As I mentioned in that PR, and in the description of this issue, I am using a cleanup context.

If an exception occurs in another part of the startup, the cleanup context does not get cleaned up, despite having been successfully initiated.

@asvetlov
Copy link
Member

Thanks for the clarification.
Please let me keep the issue open.
Would you provide a reproducible example to save my time?

@asvetlov asvetlov reopened this Oct 25, 2020
@Dreamsorcerer
Copy link
Member Author

This is a minimal example of what I mean. Expected behaviour is that "CLEANUP" is printed after the exception occurs.

from aiohttp import web

async def cleanup_ctx(app):
    print("SETUP")
    yield
    print("CLEANUP")

async def an_error(app):
    raise Exception()
    yield

def init_func(argv):
    app = web.Application()
    app.cleanup_ctx.append(cleanup_ctx)
    app.cleanup_ctx.append(an_error)
    return app

The reason this is such a problem in my use case, is that the cleanup_ctx is actually launching a process which listens on a specific port. The cleanup part will shut down that process. If the cleanup is not run, then devtools restarts the server and spawns a new process which then fails because the old process is still bound to the port, meaning that the application breaks and I have to shutdown everything and restart devtools, which is a massive pain when trying to develop another part of the application with its own startup steps.

@Dreamsorcerer
Copy link
Member Author

Dreamsorcerer commented Oct 25, 2020

This would also need a small change to devtools in order to ensure the cleanup is run, which I've suggested here: https://github.com/aio-libs/aiohttp-devtools/pull/266/files

But, it won't do anything unless aiohttp ensures that the cleanup call actually does something at this stage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants