Skip to content

Windows: DropTarget hover events freeze the drag source process (explorer.exe) completely — event pipeline verified non-blocking; suspicion narrows to ungated IDropTargetHelper + top-level-window registration #57

Done

Description

@TATboy210

Summary

With nativeapi 0.6.2 DropTarget on Windows, hovering a dragged file over the app window (mouse not released, Drop never fires) completely freezes the drag-source process (explorer.exe) until the drag is force-aborted. The app-side Dart callback is never observed to run.

We verified the native→Dart event pipeline is non-blocking at every hop, so the freeze is not in the Dart dispatch chain. After a line-by-line comparison against two mature Flutter drag-drop implementations (desktop_drop 0.9.2, super_drag_and_drop / super_native_extensions), suspicion narrows to two things nativeapi does differently from both: ungated IDropTargetHelper calls on every DragOver and registering on the top-level window HWND instead of the FlutterView child window.

Environment

  • nativeapi 0.6.2 + cnativeapi 0.6.2 (MSVC native build), Windows 11 build 10.0.26300 x64
  • Flutter 3.50.1 stable, Windows desktop (custom frameless window with DWM attribute surgery: DWMWA_NCRENDERING_POLICY, DWMWA_BORDER_COLOR, DWMWA_WINDOW_CORNER_PREFERENCE, fullscreen ncrendering suppression)

Minimal repro

final target = na.DropTarget.create(na.WindowManager.instance.getCurrent());
target!.addListener((event) { /* any lightweight Dart callback */ });
// first listener triggers lazy RegisterDragDrop
  1. Drag an .mp4 from Explorer, hover over the app window without releasing the mouse.
  2. Observed: explorer.exe (the drag source) freezes immediately until the drag is force-aborted; no Dart callback observed.
  3. Control: same app, same window, with desktop_drop 0.9.2 as the backend — all six drag checks pass.

What we ruled out

  • RegisterDragDrop conflict — reproduced with desktop_drop fully removed (single-variable isolation).
  • OleInitialize failure / GetNativeObject null — registration demonstrably succeeds (hover events fire); those failures can only yield "no drag response", never a frozen source.
  • Dart dispatch blocking — the pipeline is post-only end to end: DropTargetCom::DragEnter/DragOver → Impl::Entered/Moved (same-thread Emit) → event_emitter.h Emit (dispatch outside the listener lock) → C trampoline → Dart NativeCallable.listener (post semantics, never blocks native).
  • Source-data reads too heavy — nativeapi reads are lighter than desktop_drop's, which works fine.

Remaining synchronous work inside the callbacks (the suspects)

DragEnter/DragOver call IDropTargetHelper unconditionally whenever helper_ exists — no activation gate (cnativeapi-0.6.2/cxx_impl/src/platform/windows/drop_target_windows.cpp:111-152):

HRESULT STDMETHODCALLTYPE DragOver(DWORD /*key_state*/, POINTL point, DWORD* effect) override {
  const DWORD allowed = *effect;
  *effect = handler_ ? handler_->Moved(point, allowed) : DROPEFFECT_NONE;
  if (helper_) {                      // ← ungated: called on EVERY move
    POINT pt = {point.x, point.y};
    helper_->DragOver(&pt, *effect);  // shell drag-image manager
  }
  return S_OK;
}

Registration uses the top-level window (:237-259):

bool DropTarget::Impl::Register() {
  HWND hwnd = static_cast<HWND>(window->GetNativeObject());  // top-level HWND
  ...
  // OLE finds the target by walking up from the window under the cursor, so
  // registering the top-level window also covers child windows (a Flutter view).
  if (FAILED(RegisterDragDrop(hwnd, com))) { ... }
}

Contrast with the two mature implementations:

nativeapi 0.6.2 desktop_drop 0.9.2 super_native_extensions
Registered HWND top-level window FlutterView child window FlutterView child window
Helper gating none (if (helper_) always) drag_image_active_ gate set only after a successful DragEnter helper used, but still on the FlutterView HWND
Pre-register hygiene — explicit error on failure RevokeDragDrop first + warn if HWND already has a target

Ranked suspicion

  1. Ungated IDropTargetHelper + top-level HWND — the shell drag-image manager's internal round-trip wedging for this window shape (top-level + DWM-modified, no redirection bitmap) would leave DragOver never returning; since the source's DoDragDrop waits synchronously for target callbacks, the source STA freezes → explorer.exe hangs.
  2. Top-level-window registration itself (same divergence axis; one instrumented run can adjudicate both).
  3. Late ScopedOleInitialize (runtime first-listener) — minor, not the blocker.

A definitive adjudication needs a short instrumented run upstream (A/B: helper calls on/off; top-level vs child-window HWND; DragOver throttle on/off). We are not pursuing it further in our app (drop_target archived as disabled), but are happy to test a patched build.

Suggestions

  1. Register on the FlutterView child window by default (or expose a target-window parameter) — both mature packages do this; top-level registration is unverified against drag-image/DWM-modified windows.
  2. Gate IDropTargetHelper calls on a successful DragEnter (desktop_drop's drag_image_active_ pattern); today DragOver calls the helper on every move even if DragEnter never succeeded.
  3. Move ScopedOleInitialize to plugin/engine setup time (one-shot), matching both mature packages.
  4. RevokeDragDrop + warn before registering (super_native_extensions pattern), with actionable guidance on failure (e.g. E_OUTOFMEMORY → hint OleInitialize vs CoInitializeEx).
  5. Optional: throttle Moved events (per-move C-event construction + handle lease + post under high-frequency moves adds queue pressure).
  6. Add a per-callback-stage tracing switch (QueryPerformanceCounter/OutputDebugString: Entered → Emit → post → helper per-stage timing) — the biggest cost of diagnosing this was the lack of it.
中文版(Chinese version)

标题:Windows: DropTarget 悬停事件使拖拽源(explorer.exe)完全冻结——事件管道非阻塞,疑点收敛于 IDropTargetHelper + 顶层窗口注册

摘要:nativeapi 0.6.2 的 DropTarget(Windows)在拖拽源文件悬停于窗口上方(未释放鼠标、Drop 未触发)时,使拖拽源进程 explorer.exe 完全冻结,需强制中止拖拽。应用侧 Dart 回调未观察到执行。

复现:DropTarget.create(...) + addListener(首 listener 触发惰性 RegisterDragDrop)→ 从 explorer 拖 mp4 悬停窗口 → explorer.exe 立即冻结。对照:同 app 同窗口 desktop_drop 0.9.2 后端六项全过。

源码定位(0.6.2):事件管道逐跳核验为非阻塞(drop_target_windows.cpp → drop_target.cpp 同线程 Emit → event_emitter.h 锁外派发 → drop_target_c.cpp trampoline → Dart NativeCallable.listener post 语义)。回调内仅剩的同步跨进程工作:QueryGetData×2(轻)与 IDropTargetHelper::DragEnter/DragOver(drop_target_windows.cpp:117-120、127-130——后者每 move 无条件调用、无激活门控;hwnd 为顶层窗口)。注册面:RegisterDragDrop 用顶层窗口(:238-256),而 desktop_drop 与 super_native_extensions 均注册 FlutterView 子窗口;desktop_drop 对 helper 带激活门控,SNE 注册前先 Revoke 并警告。

嫌疑排序:① 顶层窗口 + 无门控 IDropTargetHelper(shell 拖影链路在该窗口形态下楔死内部往返 → 目标回调不返回 → 源端 DoDragDrop 同步等待卡死);② 顶层窗口注册本身;③ OLE 初始化时机偏晚。原始三候选(RegisterDragDrop 冲突 / OleInitialize 失败 / GetNativeObject null)均被证据排除——三者只能导致「无拖放」,无法冻结源进程。

建议:子窗口注册(或参数化 target window)、helper 激活门控、OleInitialize 前移到装配期、注册前 Revoke+警告、Moved 节流、阶段插桩开关。

Activity

  1. added a commit that references this issue on Nov 29, 2026
  2. lijy91 commented on Nov 29, 2026

    @lijy91
    Member

    Fixed in libnativeapi/nativeapi-core#102 and synced to main in #58.

    Root Cause

    Under Flutter's UIThreadPolicy::RunOnSeparateThread, adding a DropTarget listener ran OleInitialize, new DropTargetCom, and RegisterDragDrop on the Dart UI isolate thread, which has no Win32 message loop. Because DropTargetCom lived in an unpumped STA apartment, OLE's cross-apartment DragEnter RPC from explorer.exe deadlocked indefinitely.

    Fix

    • Marshaled OleInitialize, DropTargetCom creation, RegisterDragDrop, and teardown (RevokeDragDrop / Release / OleUninitialize) onto the HWND's owning Win32 thread, and handled DRAGDROP_E_ALREADYREGISTERED by revoking and retrying once.
    • Gated IDropTargetHelper calls behind helper_active_ (only set when helper_->DragEnter succeeds) and skipped redundant helper_->DragOver calls when position and effect are unchanged.
    • Cached the window DPI scale on DragEnter and throttled DropTargetMovedEvent emission to ~60 Hz (16 ms) while returning DROPEFFECT immediately on every DragOver for smooth drag feedback.
    • Added a Windows regression test verifying DropTarget registration from a thread without a Win32 message loop.

    This is now on main and will be included in the next release.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions