Add new hook dev-reload to prevent memory leaks

Open
#5,132 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
node.js, typescript
Domain
backend, devtools

Research direction

The request is a new plugin lifecycle hook in Nitro, so start in the runtime plugin and hook handling code, and check whether the shutdown hook mentioned in the docs is defined there. Look at how the gracefulShutdown listeners get registered during dev reloads to see where a dev-reload hook would fire. Done means a hook runs before the previous plugin instance is torn down, with a test that reloads twice without a MaxListeners warning.

Written by the indexing model from the issue text.

Description

pending triage
Describe the feature

When starting a long-running resource in a Nitro plugin (e.g. a job queue worker like Graphite Worker), every dev reload runs the plugin again and starts another one, but there is no runtime hook to stop the previous one first, so they pile up:

(node:9212) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 gracefulShutdown listeners added to [EventEmitter]. MaxListeners is 10. Use emitter.setMaxListeners() to increase limit

Example:

import { definePlugin } from 'nitro';
import { useJobRunner } from '~/utils/use-job-runner';

export default definePlugin((nitroApp) => {
  // Starts the job runner, otherwise waiting jobs would only run after the first job is added
  useJobRunner();
});

Btw in the docs there is a hook named 'shutdown' but it seems like it doesnt exist in latest rc.

Additional information
  • Would you be willing to help implement this feature?
Dominant language
TypeScript
Stars
11.5k
Forks
968
Avg merge
4d 9h
Merged PRs (30d)
64

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

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 nitrojs/nitro

All issues in nitrojs/nitro

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.