Skip to content

fix(locking,locking-redis): close the Redis client on shutdown - #17460

Merged
kodiakhq[bot] merged 2 commits into
developfrom
fix/locking-redis-close-client-on-shutdown
Oct 23, 2026
Merged

kodiakhq[bot] merged 2 commits into
developfrom
fix/locking-redis-close-client-on-shutdown

Conversation

@medusa-os-bot

@medusa-os-bot medusa-os-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

What — The locking-redis provider's Redis client is now closed when the application shuts down.

Why — No code ever closed the provider's Redis client. LockingModuleService had no __hooks, and RedisLockingProvider had no shutdown method. So anything that boots Medusa in-process more than once leaked ref'd TCP sockets, with medusaIntegrationTestRunner as the main case. Other Redis-backed modules (cache-redis, event-bus-redis, workflow-engine-redis) already close their connections on shutdown.

How

  • packages/modules/providers/locking-redis/src/services/redis-lock.ts: added shutdown(), which calls redisClient.disconnect(). This is what cache-redis does.
  • packages/modules/locking/src/services/locking-module.ts: added __hooks.onApplicationShutdown, which delegates to the provider service. This follows the analytics module's pattern.
  • packages/modules/locking/src/services/locking-provider.ts: added shutdown(). It calls the optional shutdown() on each registered lp_* provider. Providers without the method, such as in-memory, are skipped.

Testing

  • locking-redis/src/loaders/__tests__/index.spec.ts runs the real loader against an in-process TCP stub. "should close the Redis connection on shutdown" failed before the fix because the provider had no shutdown method. It passes now.
  • locking/integration-tests/__tests__/shutdown.spec.ts uses a fixture provider. Before the fix the module's shutdown hook made 0 calls to the provider's shutdown(); now it makes 1.

Checklist

  • I have added a changeset for this PR
  • The changes are covered by relevant tests
  • I have verified the code works as intended locally
  • I have linked the related issue(s) if applicable

Additional Context

  • The loader still connects to Redis at load time. Provider loaders also run on migration-only loads (MedusaAppMigrateUp, MedusaAppGetLinksExecutionPlanner), which create no module instance with hooks, so those loads can still leave a connection open.
  • I didn't run the locking-redis integration spec, because it needs a live Redis.
  • The new locking spec loads the module from src (resolve: <cwd>/src), so it doesn't depend on a prior build.
  • Related, not fixed here: medusajs/caching-redis seems to follow the same pattern. It creates and pings its client at load time, and nothing disconnects it on shutdown.

Fixes #17220

Opened automatically by the Fix Bug workflow. Review carefully before merging.

ðŸĪ– Generated with Claude Code

@medusa-os-bot
medusa-os-bot Bot requested a review from a team as a code owner October 22, 2026 16:50
@changeset-bot

changeset-bot Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

ðŸĶ‹ Changeset detected

Latest commit: cf56333

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 83 packages
Name Type
@medusajs/locking Patch
@medusajs/locking-redis Patch
@medusajs/medusa Patch
@medusajs/test-utils Patch
@medusajs/loyalty-plugin Patch
@medusajs/medusa-oas-cli Patch
integration-tests-http Patch
@medusajs/analytics Patch
@medusajs/api-key Patch
@medusajs/auth Patch
@medusajs/caching Patch
@medusajs/cart Patch
@medusajs/currency Patch
@medusajs/customer Patch
@medusajs/file Patch
@medusajs/fulfillment Patch
@medusajs/index Patch
@medusajs/inventory Patch
@medusajs/link-modules Patch
@medusajs/notification Patch
@medusajs/order Patch
@medusajs/payment Patch
@medusajs/pricing Patch
@medusajs/product Patch
@medusajs/promotion Patch
@medusajs/rbac Patch
@medusajs/region Patch
@medusajs/sales-channel Patch
@medusajs/search Patch
@medusajs/settings Patch
@medusajs/stock-location Patch
@medusajs/store Patch
@medusajs/tax Patch
@medusajs/translation Patch
@medusajs/user Patch
@medusajs/workflow-engine-inmemory Patch
@medusajs/workflow-engine-redis Patch
@medusajs/search-postgres Patch
@medusajs/draft-order Patch
@medusajs/oas-github-ci Patch
@medusajs/cache-inmemory Patch
@medusajs/cache-redis Patch
@medusajs/event-bus-local Patch
@medusajs/event-bus-redis Patch
@medusajs/analytics-local Patch
@medusajs/analytics-posthog Patch
@medusajs/auth-emailpass Patch
@medusajs/auth-github Patch
@medusajs/auth-google Patch
@medusajs/auth-oidc Patch
@medusajs/caching-redis Patch
@medusajs/file-local Patch
@medusajs/file-s3 Patch
@medusajs/fulfillment-manual Patch
@medusajs/locking-postgres Patch
@medusajs/notification-local Patch
@medusajs/notification-sendgrid Patch
@medusajs/payment-stripe Patch
@medusajs/core-flows Patch
@medusajs/framework Patch
@medusajs/instantsearch-adapter Patch
@medusajs/js-sdk Patch
@medusajs/modules-sdk Patch
@medusajs/orchestration Patch
@medusajs/query Patch
@medusajs/types Patch
@medusajs/utils Patch
@medusajs/workflows-sdk Patch
create-medusa-app Patch
@medusajs/http-types-generator Patch
@medusajs/cli Patch
@medusajs/deps Patch
@medusajs/eslint-plugin Patch
@medusajs/telemetry Patch
@medusajs/admin-bundler Patch
@medusajs/admin-sdk Patch
@medusajs/admin-shared Patch
@medusajs/admin-vite-plugin Patch
@medusajs/dashboard Patch
@medusajs/icons Patch
@medusajs/toolbox Patch
@medusajs/ui-preset Patch
@medusajs/ui Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@medusa-os-bot

medusa-os-bot Bot commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the contribution! Initial automated review looks good.

Verdict: Looks good for a final review by the team.

Removes the eager Redis connect() call in the locking-redis loader and adds a shutdown() chain so the client is closed when the application stops. Fixes leaked sockets on migration-only loads and the three open connections medusaIntegrationTestRunner left per test file.

Checked: security, performance, correctness, tests, conventions. No API routes or auth changes. Object.keys(this.__container__) pattern is established in the codebase; new Set() correctly deduplicates aliased provider instances. Changeset covers both @medusajs/locking and @medusajs/locking-redis; both AGENTS files are updated.

Triggered by: new PR opened

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@shahednasser shahednasser changed the title fix(locking,locking-redis): close the Redis client and connect lazily fix(locking,locking-redis): close the Redis client on shutdown Oct 23, 2026
@kodiakhq
kodiakhq Bot merged commit afa4b15 into develop Oct 23, 2026
46 checks passed
@kodiakhq
kodiakhq Bot deleted the fix/locking-redis-close-client-on-shutdown branch October 23, 2026 09:27
@github-actions github-actions Bot mentioned this pull request Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant