Replace `CustomThread` in `run_app` with a shared Dash/werkzeug app launcher

Open
#772 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 10 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Active
Tech stack
jupyter-notebook, python
Domain
backend, testing

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

enhancement

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:

  1. The kill isn't needed. The _kill closure in run_app already calls wsgi_server.shutdown(). That stops serve_forever cleanly, and werkzeug's serve_forever closes the socket in its finally. The trace hook only fires after the loop has already returned, and it raises a SystemExit that escapes the thread. pytest reports this as a PytestUnhandledThreadExceptionWarning (Exception in thread Thread-1 (serve_forever)), for example in tests/integration_tests/test_webapp_click_updates_local_explanation.py.
  2. It slows down every request. make_server defaults to threaded=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.
  3. It breaks tooling. sys.settrace replaces coverage's and debuggers' tracers on that thread. Callback code isn't measured by --cov, and breakpoints in callbacks don't hit.
  4. Requests are handled one at a time. Dash fires callbacks concurrently, but threaded=False serves them serially. Dash's own runner uses threaded=True.
  5. The thread isn't a daemon. A script that forgets .kill() hangs at interpreter exit.
  6. The structure is fragile. .kill is monkey-patched onto the instance with cast(Any, ...), even though an on_kill hook 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 with threaded=True and stops with shutdown(), which is what CustomThread should have done. The only private dependency is jupyter_dash._servers, and a unit test pins it.
  • Anywhere else, in the background: run make_server(host, port, app.server, threaded=True) on a daemon threading.Thread, with RunningApp.kill() = shutdown() + join(). This uses only public werkzeug API.
  • Blocking vs. background outside a notebook: run_app keeps 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 is run_app. Since shapash.utils.custom_thread is 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from MAIF/shapash

All issues in MAIF/shapash

Similar issues

More Backend & API Design issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.