Skip to content
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

fix(gatsby): leak less in chokidar listeners #27685

Merged
merged 2 commits into from
Oct 27, 2020
Merged

Conversation

vladar
Copy link
Contributor

@vladar vladar commented Oct 27, 2020

Description

This PR addresses one of our memory leaks in develop with refresh endpoint (in this case in internal plugin internal-data-bridge).

Refresh endpoint always re-runs sourceNodes and we keep re-creating chokidar listeners that retain current redux state at the moment of the sourceNodes call. So we basically retain as many state "snapshots" as there were sourceNode API calls (i.e. refreshes).

To confirm I've run the https://github.com/reactjs/reactjs.org website with a 200MB node memory limit and kept hitting the __refresh endpoint. Before this PR it was OOMing on the 14-16th refresh, after this PR somewhere after 30th (so other memory leaks in play)

To clarify: this is not the only leak 😢 We have others. It is possible that chokidar listeners in gatsby-source-filesystem are also leaking. But this warrants a separate investigation.

Also, originally we were hoping that sourceNodesStatefully (#19663) would be a proper fix for those kinds of leaks but it has it's own issues and breaking changes, so need to fix those individually.

Related Issues

Partially addresses #25868

@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Oct 27, 2020
@vladar vladar marked this pull request as ready for review October 27, 2020 17:30
@vladar vladar requested a review from a team as a code owner October 27, 2020 17:30
@vladar vladar added status: needs core review Currently awaiting review from Core team member topic: data Relates to source-nodes, internal-data-bridge, and node creation and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Oct 27, 2020
@@ -87,8 +85,8 @@ exports.sourceNodes = ({ createContentDigest, actions, store }) => {
siteMetadata: {
...configCopy.siteMetadata,
},
port: state.program.proxyPort,
host: state.program.host,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we retain the state

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, we could call store.getState() here, but because program can't change this is fine (program does get retained in memory either way)

@pieh pieh self-assigned this Oct 27, 2020
Copy link
Contributor

@pieh pieh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let's ship it!

@pieh pieh merged commit e0a3645 into master Oct 27, 2020
@vladar
Copy link
Contributor Author

vladar commented Oct 27, 2020

Published in gatsby@2.24.89

@vladar vladar deleted the vladar/fix-memory-leak branch October 29, 2020 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs core review Currently awaiting review from Core team member topic: data Relates to source-nodes, internal-data-bridge, and node creation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants