Skip to content

fix: clear terminal drag-and-drop observer on detach (fixes #311164) - #332908

Closed
Suryaansh Prithvijit Singh (Ultron09) wants to merge 3 commits into
microsoft:mainfrom
Ultron09:fix/terminal-shift-drop-detached-311164
Closed

fix: clear terminal drag-and-drop observer on detach (fixes #311164)#332908
Suryaansh Prithvijit Singh (Ultron09) wants to merge 3 commits into
microsoft:mainfrom
Ultron09:fix/terminal-shift-drop-detached-311164

Conversation

@Ultron09

Copy link
Copy Markdown

Summary

Fixes #311164.
Supersedes #318756 with requested unit tests and race condition guards.

TerminalInstance._dndObserver (a MutableDisposable<IDisposable> holding the per-container DragAndDropObserver) was set by _initDragAndDrop but never cleared by detachFromElement. When a TerminalEditor pane reuses its container across tab switches (setInput calls detachFromElement on the old instance and attachToElement on the new one), newly-attached terminals stacked additional DragAndDropObservers on top of leaked ones.

When shift-dropping a file onto the active editor-area terminal, each previous instance's observer fired on the shared container and inserted the path.

Changes

  1. Dispose this._dndObserver.clear() in detachFromElement() so drops on the old container no longer route to the detached terminal instance.
  2. Guard the deferred setTimeout in attachToElement with if (this._store.isDisposed || this._container !== container) return; so a synchronous detach in the same tick does not re-attach a stale observer.
  3. Added unit tests in terminalInstance.test.ts verifying that detachFromElement properly clears the observer.

Co-authored-by: Vlad Gerasimov vlad@vlad.studio

…#311164)

Clears `TerminalInstance._dndObserver` when detaching from DOM container, and guards deferred `_initDragAndDrop` execution against detached/disposed container changes.

Co-authored-by: Vlad Gerasimov <vlad@vlad.studio>
Copilot AI balanced review requested due to automatic review settings August 27, 2026 09:50
@vs-code-engineering

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Anthony Kim (@anthonykim1)

Matched files:

  • src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
  • src/vs/workbench/contrib/terminal/test/browser/terminalInstance.test.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes leaked terminal drag-and-drop observers when editor containers are reused.

Changes:

  • Clears the observer when detaching.
  • Guards deferred initialization against stale containers.
  • Adds detach cleanup coverage.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
terminalInstance.ts Cleans up and guards drag-and-drop observers.
terminalInstance.test.ts Tests observer cleanup during detach.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/vs/workbench/contrib/terminal/browser/terminalInstance.ts
@Ultron09

Copy link
Copy Markdown
Author

Added a unit test in terminalInstance.test.ts verifying that attaching and immediately detaching in the same tick prevents deferred _initDragAndDrop execution once the timer resolves.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

I prepared stronger regression coverage that creates real TerminalInstance objects and dispatches an actual file drop against a reused container. It verifies both detaching after the observer is initialized and detaching before deferred initialization runs.

I couldn't push to the contributor fork because maintainer edits are unavailable. Please cherry-pick b1b63b784ac into this branch.

@dmitrivMS

Copy link
Copy Markdown
Collaborator

Replacing with #335945 which has better unit-tests.

@Ultron09

Copy link
Copy Markdown
Author

Thanks Dmitriy Vasyura (@dmitrivMS) for the review, approval, and for strengthening the regression coverage in #335945! Left a quick note on #335945 to please include Co-authored-by: Ultron09 <connect.singha@gmail.com> when merging. Much appreciated!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dragging a file into one terminal editor while holding Shift inserts the path into other terminal editors as well

4 participants