Replace `CustomThread` in `run_app` with a shared Dash/werkzeug app launcher
Maintainers usually reply within 10 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
Start by reading shapash/webapp/utils/launch.py's existing NLP launcher and the current SmartExplainer.run_app() implementation that uses CustomThread; the issue does not name the latter's file. Check tests/integration_tests/test_webapp_click_updates_local_explanation.py and the other app integration tests to see how they start and stop the server. Done means both notebook and background launch paths use the shared launcher, shutdown is clean, and tests no longer report the thread-exception warning.
Written by the indexing model from the issue text.
Description
What's wrong with CustomThread
SmartExplainer.run_app() serves the webapp on a shapash.utils.custom_thread.CustomThread. That class "kills" a thread by installing sys.settrace on it and raising SystemExit on the next executed line once killed is set. This causes several problems:
- The kill isn't needed. The
_killclosure inrun_appalready callswsgi_server.shutdown(). That stopsserve_forevercleanly, and werkzeug'sserve_forevercloses the socket in itsfinally. The trace hook only fires after the loop has already returned, and it raises aSystemExitthat escapes the thread. pytest reports this as aPytestUnhandledThreadExceptionWarning(Exception in thread Thread-1 (serve_forever)), for example intests/integration_tests/test_webapp_click_updates_local_explanation.py. - It slows down every request.
make_serverdefaults tothreaded=False, so every Dash callback (pandas work, plotly figure building) runs on the traced thread. A Python-level trace call on every line costs a lot. - It breaks tooling.
sys.settracereplaces coverage's and debuggers' tracers on that thread. Callback code isn't measured by--cov, and breakpoints in callbacks don't hit. - Requests are handled one at a time. Dash fires callbacks concurrently, but
threaded=Falseserves them serially. Dash's own runner usesthreaded=True. - The thread isn't a daemon. A script that forgets
.kill()hangs at interpreter exit. - The structure is fragile.
.killis monkey-patched onto the instance withcast(Any, ...), even though anon_killhook exists for that purpose.
Suggested fix
Replace CustomThread with a single launcher in shapash/webapp/utils/launch.py. SmartApp will use it, and it returns one handle type, RunningApp. This approach is already implemented for the NLP webapp on branch feature/nlp-refacto.
- In a notebook: call
Dash.run(...)and wrap the server Dash registers. Dash's Jupyter integration already handles inline, JupyterLab and external display, the JupyterHub proxy prefix, Colab and port reuse. Its runner uses a daemon thread withthreaded=Trueand stops withshutdown(), which is whatCustomThreadshould have done. The only private dependency isjupyter_dash._servers, and a unit test pins it. - Anywhere else, in the background: run
make_server(host, port, app.server, threaded=True)on a daemonthreading.Thread, withRunningApp.kill()=shutdown()+join(). This uses only public werkzeug API. - Blocking vs. background outside a notebook:
run_appkeeps its current non-blocking behaviour, so its documented contract ("returns immediately, call.kill()") doesn't change. The NLP app can keep blocking by default, with Ctrl+C to stop. - Remove
CustomThread. Its only user isrun_app. Sinceshapash.utils.custom_threadis importable, it can stay one release as a deprecated alias before removal. - Tests: the integration tests start the app through the same launcher. That removes the settrace and the thread-exception warning, and the tests exercise the real code path.
Compatibility note: run_app would return a RunningApp instead of a threading.Thread subclass. RunningApp already has kill(), is_alive() and context-manager support. Adding join() covers the remaining Thread methods callers are likely to use.
- Dominant language
- Jupyter Notebook
- Stars
- 3.3k
- Forks
- 390
- Avg merge
- 7d 15h
- Merged PRs (30d)
- 9
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from MAIF/shapash
-
Distribution plot crashes when a numeric feature is constant in the train or test setPossibly taken @adembilge claimed this today. Openbug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
MAIF/shapash#847 · 1 comment ·
Maintainers usually reply within 10 days
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 10 days
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 35/100
Maintainers usually reply within 10 days
-
Extend end-to-end model support for scikit-learn, Keras, TensorFlow and PyTorchPossibly taken @guillaume-vignal claimed this 34 days ago. Open
Difficulty 5/5 Over a week Newbie friendliness 25/100
Maintainers usually reply within 10 days
-
Add native TabICL backend with automatic selection in SmartExplainerPossibly taken @FlorisRimb claimed this 31 days ago. Openenhancement
Difficulty 4/5 3-5 days Newbie friendliness 55/100
MAIF/shapash#811 · 1 assignee ·
Maintainers usually reply within 10 days
Similar issues
-
area: models enhancement needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rexmhall09/TUFF#69 ·
-
BUG: Backend Docker build fails due to Go version mismatchPossibly taken @Aakriti0207 claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 93/100
gittuf/visualizer#203 ·
Maintainers usually reply within 2 days
-
area: api area: auth area: backend area: security bug cookies difficulty: easy dogfood error-handling good first issue needs-triage priority: P1 reliability security session tech: cloudflare-workers tech: javascript
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
bug P2
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
BaaRai/ARTEX#981 · 2 comments · 1 reaction ·
Maintainers usually reply within 1 day
-
Inbox: permission prompt (perm:) sessions appear as duplicate agents in the recipient picker and participantsPossibly taken @Djiit claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
backnotprop/plannotator#4517 ·
Maintainers usually reply within 1 day