Skip to content

fix(client): Avoids infinite promise-chaining when socket's creation fails - #2295

Merged
leibale merged 4 commits into
redis:masterfrom
JonasFaure:issue-2134
Oct 26, 2022
Merged

fix(client): Avoids infinite promise-chaining when socket's creation fails#2295
leibale merged 4 commits into
redis:masterfrom
JonasFaure:issue-2134

Conversation

@JonasFaure

Copy link
Copy Markdown
Contributor

Description

This change prevents a memory leak when a Redis client tries to connect to an unavailable host. This is achieve by switching from a recursive to an iterative approach.

When a Redis instance fails, our clients slowly build up memory until they get killed.

#2134

First, I had to slightly change the tests' implementation to work around timeout issues when running the tests, ie : specifying a timeout using the connectTimeout argument & giving up mocking time by specifying a very small retry delay. This commit should preferably be reverted if someone who manages to run tests locally can validate my implementation with the previous implementation for those tests 🤷

Second, I swapped the recursive call to connect for a do{}while loop and leverage the #this.isReady property as the exit condition.


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

@JonasFaure
JonasFaure marked this pull request as ready for review October 17, 2022 08:58
@leibale leibale linked an issue Oct 21, 2022 that may be closed by this pull request
@leibale

leibale commented Oct 21, 2022

Copy link
Copy Markdown
Contributor

@JonasFaure nice one! sorry for the long delay, I was on vacation... I'll release a version with this fix next week :)

@codecov-commenter

codecov-commenter commented Oct 21, 2022

Copy link
Copy Markdown

Codecov Report

Base: 95.85% // Head: 95.85% // Increases project coverage by +0.00% 🎉

Coverage data is based on head (7d7cff7) compared to base (e1c0580).
Patch coverage: 84.00% of modified lines in pull request are covered.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2295   +/-   ##
=======================================
  Coverage   95.85%   95.85%           
=======================================
  Files         433      433           
  Lines        4001     4004    +3     
  Branches      451      451           
=======================================
+ Hits         3835     3838    +3     
  Misses        102      102           
  Partials       64       64           
Impacted Files Coverage Δ
packages/client/lib/client/socket.ts 80.35% <84.00%> (+0.35%) ⬆️
index.ts 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@leibale
leibale merged commit 252c219 into redis:master Oct 26, 2022
florian-schunk pushed a commit to florian-schunk/node-redis that referenced this pull request Jun 18, 2025
…fails (redis#2295)

* fix(client): timeout issues during tests

* fix(client): avoiding infinite Promise chaining while socket creation fails

* fix(client): Added missing semicolons

* clean test

Co-authored-by: leibale <leibale1998@gmail.com>
trentm added a commit to trentm/opentelemetry-js-contrib that referenced this pull request Jun 26, 2025
In redis@4.4.0 and earlier versions of redis v4, client.disconnect()
will throw if the connect failed. That broke the "with empty string for
client URL, ..." test case, at least on macOS:

    npm run test:docker:run
    RUN_REDIS_TESTS=1 npm t

I cannot explain why this is not failing in CI. Perhaps something
platform specific? This is related to socket handling in the redis
client. I believe the relevant change in node-redis was:
redis/node-redis#2295
which was part of `@redis/client@1.3.1` which was included in
`redis@4.4.0`.
trentm added a commit to open-telemetry/opentelemetry-js-contrib that referenced this pull request Jun 27, 2025
…#2914)

In redis@4.4.0 and earlier versions of redis v4, client.disconnect()
will throw if the connect failed. That broke the "with empty string for
client URL, ..." test case, at least on macOS:

    npm run test:docker:run
    RUN_REDIS_TESTS=1 npm t

I cannot explain why this is not failing in CI. Perhaps something
platform specific? This is related to socket handling in the redis
client. I believe the relevant change in node-redis was:
redis/node-redis#2295
which was part of `@redis/client@1.3.1` which was included in
`redis@4.4.0`.
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.

Memory leak on repeated connection failures

3 participants