dosubot[bot] commented on issue #36892: URL: https://github.com/apache/superset/issues/36892#issuecomment-3707229903
<!-- Greeting --> Hi @bouk! I'm [Dosu](https://go.dosu.dev/dosubot) and Iām helping the superset team. <!-- Answer --> This error happens because the Celery worker's teardown handler calls `db.session.remove()` outside of a Flask application context. Flask-SQLAlchemy requires an active app context to manage sessions, and without it, you get the "Working outside of application context" RuntimeError. The teardown handler in `superset/tasks/celery_app.py` is registered on the `task_postrun` signal but does not wrap its logic in `flask_app.app_context()`, which is necessary for Flask context-dependent operations. Other handlers in the same file (like `reset_db_connection_pool`) do use the app context correctly, so this looks like an oversight or bug in the teardown handler's implementation. To resolve the error, you can modify the teardown handler to wrap the session removal in a Flask app context, like this: ```python with flask_app.app_context(): db.session.remove() ``` This change should prevent the RuntimeError from appearing in your logs. There is no indication that this error breaks alerts/reports functionality, but it does mean session cleanup may not always run as intended, which could eventually cause resource leaks or other subtle issues if left unaddressed. You can see the relevant code and context here: [superset/tasks/celery_app.py teardown handler](https://github.com/apache/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/tasks/celery_app.py#L28-L72). <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=hallucination) | [Report š](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/ad9a1610-cddb-43dc-a5ad-6dd1d31d66f8?feedback_type=other)</sup> [](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github) [](https://cursor.com/link/prompt?text=This%20error%20happens%20because%20the%20Celery%20worker%27s%20teardown%20handler%20calls%20%60db.session.remove%28%29%60%20outside%20of%20a%20Flask%20application%20context.%20Flask-SQLAlchemy%20requires%20an%20active%20app%20context%20to%20manage%20sessions%2C%20and%20without%20it%2C%20you%20get%20the%20%22Working%20outside%20of%20application%20context%22%20RuntimeError.%20The%20teardown%20handler%20in%20%60superset/tasks/celery_app.py%60%20is%20registered%20on%20the%20%60task_postrun%60%20signal%20but%20does%20not%20wrap%20its%20logic%20in%20%60flask_app.app_context%28%29%60%2C%20which%20is%20necessary%20for%20Flask%20context-dependent%20operations.%20Other%20handlers%20in%20the%20same%20file%20%28like%20%60reset_db_connection_pool%60%29%20do%20u se%20the%20app%20context%20correctly%2C%20so%20this%20looks%20like%20an%20oversight%20or%20bug%20in%20the%20teardown%20handler%27s%20implementation.%20%0A%0ATo%20resolve%20the%20error%2C%20you%20can%20modify%20the%20teardown%20handler%20to%20wrap%20the%20session%20removal%20in%20a%20Flask%20app%20context%2C%20like%20this%3A%0A%0A%60%60%60python%0Awith%20flask_app.app_context%28%29%3A%0A%20%20%20%20db.session.remove%28%29%0A%60%60%60%0A%0AThis%20change%20should%20prevent%20the%20RuntimeError%20from%20appearing%20in%20your%20logs.%20There%20is%20no%20indication%20that%20this%20error%20breaks%20alerts/reports%20functionality%2C%20but%20it%20does%20mean%20session%20cleanup%20may%20not%20always%20run%20as%20intended%2C%20which%20could%20eventually%20cause%20resource%20leaks%20or%20other%20subtle%20issues%20if%20left%20unaddressed.%20%20%0AYou%20can%20see%20the%20relevant%20code%20and%20context%20here%3A%20%5Bsuperset/tasks/celery_app.py%20teardown%20handler%5D%28https%3A//github.com/apac he/superset/blob/829e4d92d91ceae4b43b1ed3b063ffe45377799c/superset/tasks/celery_app.py%23L28-L72%29.) [](https://go.dosu.dev/discord-bot) [](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/issues/36892) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
