microsoft / microsoft/tsyringe

Disposing container doesn't dispose instances created/registered with `useFactory` and `useValue` providers

Open
#274 4 comments 1 reaction 1 assignee View on GitHub

@Xapphire13 is already working on this.

Since Nov 4, 2025.

awaiting triage
Dominant language
TypeScript
Stars
6k
Forks
184
Avg merge
3m
Merged PRs (30d)
1

Description

Describe the bug

Disposing container doesn't dispose instances created/registered with useFactory and useValue providers.

To Reproduce

import "reflect-metadata";
import { container } from 'tsyringe';

const APP_INIT = Symbol('APP_INIT')

container.register(APP_INIT, {
  useFactory: () => ({
     dispose: () => console.log('disposed from useFactory')
  })
});

container.register(APP_INIT, {
  useValue: {
     dispose: () => console.log('disposed from useValue')
  }
});

container.register(APP_INIT, {
  useClass: class {
    dispose() {
      console.log('disposed from useClass')
    }
  }
});

console.log(container.resolveAll(APP_INIT))
Promise.resolve(container.dispose()).then(() => console.log('container disposed'))

the output is:

[
  { dispose: [Function: dispose] },
  { dispose: [Function: dispose] },
  useClass {}
]
disposed from useClass
container disposed

Expected behavior

Documentation says

All instances created by the container that implement the Disposable interface will automatically be disposed of when the container is disposed.

So, I expect that all services that have .dispose method are disposed. At the bare minimum there should be a possibility to dispose factories and values. We use factories to provide APP_INITIALIZERs that can connect to db or remote socket and need to close this connections on shutdown

Version: 4.10.0

Contributor guide

No contributing guide indexed for this repository

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.