plotly / dash

Data Apps & Dashboards for Python. No JavaScript Required.

Home Page:https://plotly.com/dash

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] dash_duo.server.stop() kills threads that it doesn't own - ones that were created after its inception

jake-bickle opened this issue · comments

Describe your context

dash                 2.14.1
dash-core-components 2.0.0
dash-html-components 2.0.0
dash-table           5.0.0
dash-testing-stub    0.0.2

Describe the bug

KillerThread kills all threads that were created since the server's inception. This appears to be intended, but it shouldn't be. Dash should only modify threads that it has created.

Execute pytest -sv on the following example:

import dash
from dash import html

import threading
import time

def some_function_on_a_different_thread():
    try:
        print("starting long work")
        time.sleep(5)
        print("ending long work")
    except SystemExit:
        print("Thread received SystemExit.")


def test_some_function(dash_duo):
    app = dash.Dash(__name__)
    app.layout = html.Div(id="nully-wrapper", children=0)

    dash_duo.start_server(app)
    time.sleep(1)

    # This function could interact with the webpage, or do some other important work
    # in the background.
    my_thread = threading.Thread(target=some_function_on_a_different_thread)
    my_thread.start()

    dash_duo.server.stop()

This is a rather contrived example, but I have pytest fixtures that spin up services that must last between tests, all of whom may or may not create their own threads throughout the session's lifetime.

Expected behavior
Test should hang for ~5 seconds, and then "ending long work" is printed.

Actual behavior
Test lasts ~1 second, and "Thread received SystemExit." is printed.