Skip to content

Opional connection closing on shutdown hook #1889

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
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

n-nik
Copy link

@n-nik n-nik commented Jul 24, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Currently, the SequelizeModule unconditionally closes the database connection during the onApplicationShutdown lifecycle hook. In applications that run long-running background tasks (e.g., RabbitMQ consumers), this creates a race condition. An active task may attempt a database query after the connection has been closed, leading to a ConnectionManager was closed error and preventing a truly graceful shutdown.

Issue Number: N/A

What is the new behavior?

This PR introduces a new boolean option, autoCloseConnection, to the module configuration options.

  • It defaults to true to ensure full backward compatibility.
  • When autoCloseConnection is set to false, the module will skip closing the connection during the shutdown lifecycle.

This gives developers control over the connection's lifecycle, allowing them to implement an orchestrated shutdown. For example, they can wait for all background tasks to complete before manually closing the Sequelize connection in their own onApplicationShutdown hook.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This change enables more robust graceful shutdown patterns for applications with background workers. This PR also includes minor code formatting and dependency updates from npm audit fix to align with the project's standards.

Example Usage:

// In a module
SequelizeModule.forRoot({
  ...options,
  autoCloseConnection: false,
})

// In a custom shutdown service
@Injectable()
class CustomShutdownService implements OnApplicationShutdown {
  constructor(
    private readonly sequelize: Sequelize
  ) {}

  async onApplicationShutdown() {
    // Manually close the connection
    await this.sequelize.close();
  }
}

nik added 3 commits July 24, 2025 23:57
The default behavior of closing the connection in the `onApplicationShutdown` hook can cause a race condition in applications with long-running background tasks, such as queue consumers. This leads to errors when an active task attempts to query the database after the connection has been closed.
This change introduces an `autoCloseConnection: boolean` option to the module configuration, defaulting to `true` for backward compatibility.
Setting `autoCloseConnection: false` prevents the automatic closing, allowing developers to manage the connection lifecycle manually at a later point in their own `onApplicationShutdown` logic for a more robust graceful shutdown.
Running `npm audit fix` to update dependencies and resolve known security issues.
This addresses the following advisories:
- GHSA-xffm-g5w8-qvg7: ReDoS in @eslint/plugin-kit
- GHSA-v6h2-p8h4-qcjw: ReDoS in brace-expansion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant