-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Serialize Dash._setup_server under a lock and publish its flag last
#3980
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mokashang
wants to merge
1
commit into
plotly:dev
Choose a base branch
from
mokashang:fix/setup-server-race-3971
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−71
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| """Regression test for gh-3971: ``_setup_server`` TOCTOU race. | ||
|
|
||
| Before the fix, ``Dash._setup_server`` set its guard flag before doing the | ||
| work that flag protects (populating ``registered_paths``, ``callback_map``, | ||
| etc.). Under a multi-threaded WSGI worker such as ``gunicorn -k gthread``, | ||
| a second thread arriving mid-setup could observe the flag already set, skip | ||
| setup, and then read ``registered_paths`` while it was still empty, which | ||
| caused component-bundle requests to 500 with "Error loading dependency." | ||
|
|
||
| The test simulates a concurrent second request by slowing down one of the | ||
| inner setup steps and having several threads call ``_setup_server`` at the | ||
| same time. After every thread returns, ``registered_paths`` must be | ||
| populated, because a return from ``_setup_server`` is meant to guarantee | ||
| the setup work is done. | ||
| """ | ||
| import threading | ||
| import time | ||
|
|
||
| from dash import Dash, html | ||
|
|
||
|
|
||
| def test_setup_server_is_atomic_under_concurrent_requests(): | ||
| app = Dash() | ||
| app.layout = html.Div(id="root") | ||
|
|
||
| original_generate_scripts_html = app._generate_scripts_html | ||
|
|
||
| def slow_generate_scripts_html(): | ||
| # Widen the TOCTOU window so a stock CPython scheduler reliably lets | ||
| # other threads reach the guard while this one is still running. | ||
| time.sleep(0.1) | ||
| return original_generate_scripts_html() | ||
|
|
||
| app._generate_scripts_html = slow_generate_scripts_html | ||
|
|
||
| thread_count = 4 | ||
| barrier = threading.Barrier(thread_count) | ||
| paths_seen_after_setup = [] | ||
| lock = threading.Lock() | ||
|
|
||
| def worker(): | ||
| barrier.wait() | ||
| app._setup_server() | ||
| with lock: | ||
| paths_seen_after_setup.append(set(app.registered_paths)) | ||
|
|
||
| threads = [threading.Thread(target=worker) for _ in range(thread_count)] | ||
| for t in threads: | ||
| t.start() | ||
| for t in threads: | ||
| t.join() | ||
|
|
||
| assert len(paths_seen_after_setup) == thread_count | ||
| for paths in paths_seen_after_setup: | ||
| # Every thread that received control back from _setup_server must | ||
| # observe registered_paths already populated by the winning thread. | ||
| # Before the fix, threads that raced past the guard saw an empty set. | ||
| assert paths, ( | ||
| "A thread returned from _setup_server before the setup work " | ||
| "was done; registered_paths was still empty." | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neighbor entries lead with the PR link (
[#3944](...),[#3955](...)). Prefix this with[#3980](https://github.com/plotly/dash/pull/3980)to match. TheFixes #3971at the end is the issue, not the PR.