GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — fix(core): add dispose functions to prevent subscription memory leaks by hendem · Pull Request #7914 · anomalyco/opencode ConversationSummaryRe-implementation of #7032 rebased cleanly on current dev branch (original had merge conflicts).
ProblemBus subscriptions accumulate during extended use because return values from SolutionEach module now stores unsubscribe functions and provides
All 12 memory tests pass.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. - Add dispose() to Share, ShareNext, Plugin, and Format namespaces - Add cleanupSession() and dispose() to ACP Agent with AbortControllers - Add Bus._getSubscriptionCount() test helpers - Add memory tests to verify cleanup works correctly Supersedes anomalyco#7032 Fixes anomalyco#3013
hendem
force-pushed
the
fix/memory-leak-subscription-cleanup-v2
branch
from
a00b2cb to
8142552
Compare
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewThis pull request adds Changes:
Reviewed changesCopilot reviewed 10 out of 10 changed files in this pull request and generated 10 comments. Show a summary per file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. - Add multiple disposed checks in Share.sync() after async boundaries - Use splice(0) instead of length = 0 for clearer array clearing - Reduce test helper timeout from 10s to 100ms - Use Array.from() for iteration safety in Bus test helpers
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewCopilot reviewed 10 out of 10 changed files in this pull request and generated 13 comments. 💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. - Add try-catch around unsubscribe calls to ensure cleanup completes even if one fails - Use splice(0) pattern consistently before iteration for safe array clearing - Rename cleanupSession to cleanupSessionEventSubscription for clarity - Add try-finally to ACP test for generator cleanup on test failure - Fix Format.dispose() to use splice(0) for consistency with other modules
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewCopilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments. 💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
hendem
force-pushed
the
fix/memory-leak-subscription-cleanup-v2
branch
from
f2181ae to
48a68e6
Compare
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewCopilot reviewed 10 out of 10 changed files in this pull request and generated no new comments. 💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewCopilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments. 💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pull request overviewCopilot reviewed 10 out of 10 changed files in this pull request and generated 7 comments. Comments suppressed due to low confidence (1)packages/opencode/src/share/share-next.ts:188
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
Comment on lines
+253
to
+255
|
| // Use short timeout for tests - this is a no-op callback that won't cause issues if it fires | |
| const timeout = setTimeout(() => {}, 100) | |
| queue.set(sessionID, { timeout, data: dataMap }) | |
| // Use timeout handle for testing dispose cleanup, but clear it immediately so it never fires | |
| const timeout = setTimeout(() => {}, 100) | |
| queue.set(sessionID, { timeout, data: dataMap }) | |
| clearTimeout(timeout) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| export async function init() { | ||
| Bus.subscribe(Session.Event.Updated, async (evt) => { | ||
| await sync(evt.properties.info.id, [ | ||
| // Clean up any existing subscriptions before adding new ones | ||
| dispose() | ||
| disposed = false | ||
| // Increment generation so in-flight operations from previous cycle are invalidated | ||
| const gen = ++generation |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The async init() function calls dispose() synchronously at the start, but then continues with async operations. If multiple init() calls happen concurrently, they could all pass the dispose() gate and create duplicate subscriptions. The same issue exists in Share.ts but is more critical here since init() is async and takes longer to complete.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| stream: (async function* () { | ||
| // Use finite loop with abort check to prevent background runaway | ||
| for (let i = 0; i < 100 && !genController.signal.aborted; i++) { | ||
| await new Promise((r) => setTimeout(r, 100)) | ||
| if (genController.signal.aborted) break | ||
| yield { type: "test" } | ||
| } | ||
| activeGenerators.delete(genController) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The test creates async generators that run for up to 100 iterations with 100ms delays (potentially 10 seconds). Even with the abort check, this creates background async work that may not complete before the test ends. The 50ms wait at line 163 may not be sufficient for all generators to exit cleanly. Consider reducing the iteration count or delay time to ensure generators complete within a reasonable timeframe, or increase the cleanup wait time.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| export function dispose() { | ||
| disposed = true | ||
| const toUnsubscribe = unsubscribers.splice(0) |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The unsubscribers array is shared across multiple init() calls, but the splice(0) operation in dispose() may not be thread-safe if init() is called concurrently during dispose(). While JavaScript is single-threaded, async operations could interleave. Consider saving the reference before splicing to avoid potential race conditions where new subscribers are added during disposal.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // Clean up any existing subscriptions before adding new ones | ||
| dispose() | ||
| disposed = false | ||
| // Increment generation so in-flight operations from previous cycle are invalidated | ||
| const gen = ++generation |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The init() function calls dispose() at the start but then immediately sets disposed=false and increments generation before creating new subscriptions. However, there's no protection against concurrent init() calls. If init() is called twice simultaneously, both calls will increment generation and create duplicate subscriptions that won't all be tracked for cleanup. Consider adding a guard to prevent concurrent initialization.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| const toUnsubscribe = unsubscribers.splice(0) | ||
| for (const unsub of toUnsubscribe) { | ||
| try { | ||
| unsub() | ||
| } catch (error) { | ||
| log.error("failed to unsubscribe", { error }) | ||
| } | ||
| } | ||
| // Hardened: snapshot and clear atomically to avoid race during iteration | ||
| const pending = Array.from(queue.values()) | ||
| queue.clear() | ||
| for (const entry of pending) { | ||
| clearTimeout(entry.timeout) | ||
| } |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
The same concurrency issue exists here as in Share.ts. The unsubscribers.splice(0) operation could race with init() adding new subscribers. Additionally, the queue clearing logic snapshots the queue after clearing it, which means timeouts being added during this operation could be missed.
| const toUnsubscribe = unsubscribers.splice(0) | |
| for (const unsub of toUnsubscribe) { | |
| try { | |
| unsub() | |
| } catch (error) { | |
| log.error("failed to unsubscribe", { error }) | |
| } | |
| } | |
| // Hardened: snapshot and clear atomically to avoid race during iteration | |
| const pending = Array.from(queue.values()) | |
| queue.clear() | |
| for (const entry of pending) { | |
| clearTimeout(entry.timeout) | |
| } | |
| // Drain the live unsubscribers array so handlers added during disposal | |
| // are also cleaned up before we return. | |
| while (unsubscribers.length > 0) { | |
| const unsub = unsubscribers.pop() | |
| if (!unsub) { | |
| continue | |
| } | |
| try { | |
| unsub() | |
| } catch (error) { | |
| log.error("failed to unsubscribe", { error }) | |
| } | |
| } | |
| // Clear all pending timeouts from the live queue and then empty it. | |
| for (const entry of queue.values()) { | |
| clearTimeout(entry.timeout) | |
| } | |
| queue.clear() |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| data: evt.properties.info, | ||
| }, | ||
| ]) | ||
| if (gen !== generation) return |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There's an inconsistency in generation checks. Line 48 checks 'if (gen !== generation) return' after the first sync call, but line 32 checks both 'disposed' and 'gen !== generation' at the start of the subscription handler. For consistency and safety, all early returns should check both conditions or have a clear reason for checking only one.
| if (gen !== generation) return | |
| if (disposed || gen !== generation) return |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
|
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Uses a huge amount of memory
There was an error while loading. Please reload this page.