diff --git a/docker/gunicorn.conf.py b/docker/gunicorn.conf.py index f86de35..456ed02 100644 --- a/docker/gunicorn.conf.py +++ b/docker/gunicorn.conf.py @@ -34,3 +34,19 @@ def post_worker_init(worker): logging.getLogger("library.apps").warning( "Startup probe crashed: %s", exc, exc_info=True ) + + +def worker_exit(server, worker): + # Neomodel lazily creates a neo4j.Driver on first cypher_query and + # holds it for the process lifetime. Newer neo4j drivers warn (and + # will eventually fail to clean up) if the driver is destroyed + # without an explicit close. Close it here so each gunicorn worker + # shuts down cleanly. + try: + from neomodel import db + + db.close_connection() + except Exception as exc: + logging.getLogger("neomodel").warning( + "Failed to close neomodel driver on worker exit: %s", exc + )