microsoft / microsoft/tsyringe
Disposing container doesn't dispose instances created/registered with `useFactory` and `useValue` providers
@Xapphire13 is already working on this.
Since Nov 4, 2025.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.