From e1f631649fc32bfe87d57501b54f6163ba7b5dd3 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Fri, 1 Mar 2024 12:04:39 -0800 Subject: [PATCH] Shutdown the executor after compilation is done Avoid keeping the executor threads around while the process is running. --- reflex/app_module_for_backend.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/reflex/app_module_for_backend.py b/reflex/app_module_for_backend.py index da5f51671..392c2540e 100644 --- a/reflex/app_module_for_backend.py +++ b/reflex/app_module_for_backend.py @@ -12,11 +12,22 @@ if "app" != constants.CompileVars.APP: app_module = get_app(reload=False) app = getattr(app_module, constants.CompileVars.APP) -compile_future = ThreadPoolExecutor(max_workers=1).submit(app.compile_) -compile_future.add_done_callback( - # Force background compile errors to print eagerly - lambda f: f.result() -) +_executor = ThreadPoolExecutor(max_workers=1) + + +def _done(executor: ThreadPoolExecutor): + def _cb(f): + # Do not leak file handles from the executor itself + executor.shutdown(wait=False) + # Force background compile errors to print eagerly + print("compile done", f.result(), executor) + + return _cb + + +compile_future = _executor.submit(app.compile_) +compile_future.add_done_callback(_done(_executor)) + # Wait for the compile to finish in prod mode to ensure all optional endpoints are mounted. if is_prod_mode(): compile_future.result() @@ -24,6 +35,8 @@ if is_prod_mode(): # ensure only "app" is exposed. del app_module del compile_future +del _done +del _executor del get_app del get_compiled_app del is_prod_mode