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(server/objects): solves database connection resource leak when client connections are prematurely closed #3889

Merged
merged 8 commits into from
Jan 28, 2025

Conversation

iainsproat
Copy link
Contributor

@iainsproat iainsproat commented Jan 24, 2025

Description & motivation

This PR fixes a long existing bug with prematurely closed streaming connections. Prior to this PR, for the /api/getobjects/* REST endpoint, if the client prematurely closed a connection to the server it was not propogated to the database connection. This resulted in the database connection never being closed, and inevitably saturation for the server and the inability to serve further traffic (reffectively a Denial of Service until the server is restarted).

The bug is due to an issue with Node.js stream.pipeline implementation, described in the documentation as so:

stream.pipeline() leaves dangling event listeners on the streams after the callback has been invoked. In the case of reuse of streams after failure, this can cause event listener leaks and swallowed errors.

Changes:

  • introduces a feature flag around the fix
  • introduces stream-chain library to resolve a known issue with Node.js built-in stream.pipeline
  • includes a test to prevent regressions

To-do before merge:

  • Fix this issue.
  • Amend the test expectations to prevent regression.

Screenshots:

Before (recreating the issue):

Screenshot 2025-01-24 at 15 33 59 Screenshot 2025-01-24 at 15 33 23 Screenshot 2025-01-24 at 15 33 11

After (the fix has been applied):

  • no errors, the logs are nice and quiet. The premature close of the stream does not seem to be considered an error case.

Validation of changes:

  • the included test fails if the Feature flag is disabled, and passes if the feature flag is enabled

Run the test with yarn test --run 'modules/core/tests/integration/objectsStream.rest.spec.ts' -f 'should close database connections if client connection is prematurely closed'

Checklist:

  • My pull request follows the guidelines in the Contributing guide?
  • My pull request does not duplicate any other open Pull Requests for the same update/change?
  • My commits are related to the pull request and do not amend unrelated code or documentation.
  • My code follows a similar style to existing code.
  • I have added appropriate tests.
  • I have updated or added relevant documentation.

References

@iainsproat iainsproat changed the title test(server/objects): prematurely close streaming connections test(server/objects): test for prematurely closed streaming connections Jan 28, 2025
@iainsproat iainsproat marked this pull request as ready for review January 28, 2025 10:14
@iainsproat iainsproat changed the title test(server/objects): test for prematurely closed streaming connections fix(server/objects): solves database connection resource leak when client connections are prematurely closed Jan 28, 2025
Copy link
Contributor

📸 Preview service has generated an image.

@iainsproat iainsproat merged commit 32c310f into main Jan 28, 2025
28 of 30 checks passed
@iainsproat iainsproat deleted the iain/zombie-streaming-db-connections branch January 28, 2025 14:56
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.

3 participants