Skip to content

Fix: Prevent memory leak in transaction management - #122

Merged
KyleAMathews merged 2 commits into
TanStack:mainfrom
jiji-hoon96:fix/transaction-memory-leak
Jun 5, 2025
Merged

KyleAMathews merged 2 commits into
TanStack:mainfrom
jiji-hoon96:fix/transaction-memory-leak

Conversation

@jiji-hoon96

@jiji-hoon96 jiji-hoon96 commented May 30, 2025 •

Copy link
Copy Markdown
Contributor

Problem

The global transactions array in src/transactions.ts was accumulating all created transactions indefinitely, including completed and failed ones. This caused

  • Memory leaks in long-running applications
  • Test isolation issues due to shared state between tests
  • Potential OOM crashes in high-transaction-volume scenarios

Reproduction Case

Before fix: All transactions stay in memory forever
for(let i = 0; i < 10000; i++) {
  const tx = createTransaction({...})
  await tx.commit() // Stays in global array
}
console.log(transactions.length) // 10000 - Memory leak!

Change

setState(newState: TransactionState) {
  this.state = newState
  
  // Remove completed transactions from global tracking
  if (newState === `completed` || newState === `failed`) {
    removeFromPendingList(this)
  }
}
  • for Each => for of

…ions

- Remove completed/failed transactions from global array to prevent memory accumulation
- Add removeFromPendingList function to clean up transactions on state change
- Maintain existing rollback functionality for pending transaction conflict detection
- Resolves potential OOM issues in long-running applications
@changeset-bot

changeset-bot Bot commented May 30, 2025

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 44b86dd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

@KyleAMathews KyleAMathews moved this to In Progress in 0.1.0 release Jun 2, 2025
@KyleAMathews

Copy link
Copy Markdown
Collaborator

We can actually just remove it — the idea was/is in the future we'll add Devtools (#42) but that'll be an opt-in thing when it happens.

I'll just push some changes.

@pkg-pr-new

pkg-pr-new Bot commented Jun 5, 2025

Copy link
Copy Markdown

@tanstack/db-example-react-todo

npm i https://pkg.pr.new/@tanstack/db@122
npm i https://pkg.pr.new/@tanstack/db-collections@122
npm i https://pkg.pr.new/@tanstack/react-db@122
npm i https://pkg.pr.new/@tanstack/vue-db@122

commit: 44b86dd

@KyleAMathews

Copy link
Copy Markdown
Collaborator

Oh actually we do use the transactions array — so nvm, this is good 👍

@KyleAMathews KyleAMathews left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants