-
-
Notifications
You must be signed in to change notification settings - Fork 638
Fix descriptor closed error caused by workers restart while request is in progress
#1970
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3b1c777
always use retries httpx plugin
AbanoubGhadban a66fb93
don't skip flaky tests
AbanoubGhadban b217eb3
Revert "always use retries httpx plugin"
AbanoubGhadban 8b32c4c
don't skip tests
AbanoubGhadban 9a0588a
update the assert statement that looks for the client bundle at the page
AbanoubGhadban 1cab0ac
revert this: increase the workers restart interval
AbanoubGhadban dcd8538
revert this: don't use `connection_without_retries`
AbanoubGhadban d3c02bf
Revert "revert this: increase the workers restart interval"
AbanoubGhadban 4d8e807
implement graceful shutdown at node renderer workers
AbanoubGhadban 31fa75c
add gracefulWorkerRestartTimeout node renderer config
AbanoubGhadban 4b0f581
tiny fixes and linting
AbanoubGhadban 0e434f5
update CHANGELOG.md
AbanoubGhadban 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 |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| # Node Renderer Troubleshooting | ||
|
|
||
| - If you enabled restarts (having `allWorkersRestartInterval` and `delayBetweenIndividualWorkerRestarts`), you should set it with a high number to avoid the app from crashing because all Node renderer workers are stopped/killed. | ||
|
|
||
| - If your app contains streamed pages that take too much time to be streamed to the client, ensure to not set the `gracefulWorkerRestartTimeout` parameter or set to a high number, so the worker is not killed while it's still serving an active request. |
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
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
49 changes: 49 additions & 0 deletions
49
react_on_rails_pro/packages/node-renderer/src/worker/handleGracefulShutdown.ts
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,49 @@ | ||
| import cluster from 'cluster'; | ||
| import { FastifyInstance } from './types'; | ||
| import { SHUTDOWN_WORKER_MESSAGE } from '../shared/utils'; | ||
| import log from '../shared/log'; | ||
|
|
||
| const handleGracefulShutdown = (app: FastifyInstance) => { | ||
| const { worker } = cluster; | ||
| if (!worker) { | ||
| log.error('handleGracefulShutdown is called on master, expected to call it on worker only'); | ||
| return; | ||
| } | ||
|
|
||
| let activeRequestsCount = 0; | ||
| let isShuttingDown = false; | ||
|
|
||
| process.on('message', (msg) => { | ||
| if (msg === SHUTDOWN_WORKER_MESSAGE) { | ||
| log.debug('Worker #%d received graceful shutdown message', worker.id); | ||
| isShuttingDown = true; | ||
| if (activeRequestsCount === 0) { | ||
| log.debug('Worker #%d has no active requests, killing the worker', worker.id); | ||
| worker.destroy(); | ||
| } else { | ||
| log.debug( | ||
| 'Worker #%d has "%d" active requests, disconnecting the worker', | ||
| worker.id, | ||
| activeRequestsCount, | ||
| ); | ||
| worker.disconnect(); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| app.addHook('onRequest', (_req, _reply, done) => { | ||
| activeRequestsCount += 1; | ||
| done(); | ||
| }); | ||
|
|
||
| app.addHook('onResponse', (_req, _reply, done) => { | ||
| activeRequestsCount -= 1; | ||
| if (isShuttingDown && activeRequestsCount === 0) { | ||
| log.debug('Worker #%d served all active requests and going to be killed', worker.id); | ||
| worker.destroy(); | ||
| } | ||
| done(); | ||
| }); | ||
| }; | ||
|
|
||
| export default handleGracefulShutdown; |
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 |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
| find("input").set new_text | ||
| within("h3") do | ||
| if expect_no_change | ||
| expect(subject).to have_no_content new_text | ||
| expect(subject).not_to have_content new_text | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All of these changes at this file just reverts the changes made at the PR that skips flaky tests (I think many of these changes unnecessary at that PR) |
||
| else | ||
| expect(subject).to have_content new_text | ||
| end | ||
|
|
@@ -110,7 +110,7 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
| it "changes name in message according to input" do | ||
| visit "/client_side_hello_world" | ||
| change_text_expect_dom_selector("#HelloWorld-react-component-0") | ||
| click_link "Hello World Component Server Rendered, with extra options" # rubocop:disable Capybara/ClickLinkOrButtonStyle | ||
| click_link "Hello World Component Server Rendered, with extra options" | ||
| change_text_expect_dom_selector("#my-hello-world-id") | ||
| end | ||
| end | ||
|
|
@@ -174,19 +174,19 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
|
|
||
| before do | ||
| visit "/" | ||
| click_link "React Router" # rubocop:disable Capybara/ClickLinkOrButtonStyle | ||
| click_link "React Router" | ||
| end | ||
|
|
||
| context "when rendering /react_router" do | ||
| it { is_expected.to have_text("Woohoo, we can use react-router here!") } | ||
|
|
||
| it "clicking links correctly renders other pages" do | ||
| click_link "Router First Page" # rubocop:disable Capybara/ClickLinkOrButtonStyle | ||
| click_link "Router First Page" | ||
| expect(page).to have_current_path("/react_router/first_page") | ||
| first_page_header_text = page.find(:css, "h2#first-page").text | ||
| expect(first_page_header_text).to eq("React Router First Page") | ||
|
|
||
| click_link "Router Second Page" # rubocop:disable Capybara/ClickLinkOrButtonStyle | ||
| click_link "Router Second Page" | ||
| expect(page).to have_current_path("/react_router/second_page") | ||
| second_page_header_text = page.find(:css, "h2#second-page").text | ||
| expect(second_page_header_text).to eq("React Router Second Page") | ||
|
|
@@ -239,12 +239,12 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
| end | ||
| end | ||
|
|
||
| describe "Manual client hydration", :js do | ||
| describe "Manual client hydration", :js, type: :system do | ||
| before { visit "/xhr_refresh" } | ||
|
|
||
| it "HelloWorldRehydratable onChange should trigger" do | ||
| within("form") do | ||
| click_button "refresh" # rubocop:disable Capybara/ClickLinkOrButtonStyle | ||
| click_button "refresh" | ||
| end | ||
| within("#HelloWorldRehydratable-react-component-1") do | ||
| find("input").set "Should update" | ||
|
|
@@ -396,7 +396,7 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
|
|
||
| it "hydrates the component" do | ||
| visit path | ||
| expect(page.html).to include("client-bundle.js") | ||
| expect(page.html).to match(/client-bundle[^\"]*.js/) | ||
| change_text_expect_dom_selector(selector) | ||
| end | ||
|
|
||
|
|
@@ -407,28 +407,27 @@ def change_text_expect_dom_selector(dom_selector, expect_no_change: false) | |
| # Ensure that the component state is not updated | ||
| change_text_expect_dom_selector(selector, expect_no_change: true) | ||
|
|
||
| expect(page).to have_no_text "Loading branch1" | ||
| expect(page).to have_no_text "Loading branch2" | ||
| expect(page).to have_no_text(/Loading branch1 at level \d+/) | ||
| expect(page).not_to have_text "Loading branch1" | ||
| expect(page).not_to have_text "Loading branch2" | ||
| expect(page).not_to have_text(/Loading branch1 at level \d+/) | ||
| expect(page).to have_text(/branch1 \(level \d+\)/, count: 5) | ||
| end | ||
|
|
||
| it "doesn't hydrate status component if packs are not loaded" do | ||
| # visit waits for the page to load, so we ensure that the page is loaded before checking the hydration status | ||
| visit "#{path}?skip_js_packs=true" | ||
| expect(page).to have_text "HydrationStatus: Streaming server render" | ||
| expect(page).to have_no_text "HydrationStatus: Hydrated" | ||
| expect(page).to have_no_text "HydrationStatus: Page loaded" | ||
| expect(page).not_to have_text "HydrationStatus: Hydrated" | ||
| expect(page).not_to have_text "HydrationStatus: Page loaded" | ||
| end | ||
| end | ||
|
|
||
| describe "Pages/stream_async_components_for_testing", :js, | ||
| skip: "Flaky test replaced by Playwright E2E tests in e2e-tests/streaming.spec.ts" do | ||
| describe "Pages/stream_async_components_for_testing", :js do | ||
| it_behaves_like "streamed component tests", "/stream_async_components_for_testing", | ||
| "#AsyncComponentsTreeForTesting-react-component-0" | ||
| end | ||
|
|
||
| describe "React Router Sixth Page", :js, skip: "Work in progress in another branch: justin808/fix-body-dup-retry" do | ||
| describe "React Router Sixth Page", :js do | ||
| it_behaves_like "streamed component tests", "/server_router/streaming-server-component", | ||
| "#ServerComponentRouter-react-component-0" | ||
| end | ||
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.
Uh oh!
There was an error while loading. Please reload this page.