Skip to content

fix(PIMETextService): defer ImeModule initialization out of DllMain (#940) - #948

Merged
PCMan merged 1 commit into
masterfrom
fix/issue-940-defer-imemodule-init
Oct 18, 2026
Merged

PCMan merged 1 commit into
masterfrom
fix/issue-940-defer-imemodule-init

Conversation

@PCMan

@PCMan PCMan commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #940

Summary of Changes

  • Save module handle hModule during DLL_PROCESS_ATTACH without eagerly constructing PIME::ImeModule.
  • Introduce getImeModule() helper with std::mutex synchronization to lazily instantiate PIME::ImeModule upon first invocation (DllGetClassObject, DllRegisterServer, DllUnregisterServer).
  • Introduce releaseImeModule() helper to encapsulate thread-safe destruction.
  • Safely clean up in DLL_PROCESS_DETACH only during dynamic unloads (lpReserved == NULL).
  • Update DllCanUnloadNow() to safely check if g_imeModule is null under mutex lock and return S_OK immediately if the module was never initialized, avoiding eager instantiation during COM garbage collection.

Rationale

Constructing ImeModule directly inside DllMain violates Windows OS guidelines regarding the OS loader lock (LdrpLoaderLock), as it initializes environment paths and reads filesystem/registry while holding the lock. Deferring construction to the COM exports avoids deadlock and crashes during DLL injection.

Generated with assistance from Antigravity (Gemini 3.8 Flash).

@PCMan
PCMan force-pushed the fix/issue-940-defer-imemodule-init branch 2 times, most recently from 9b1d9c7 to 0e40b0c Compare October 18, 2026 06:32
…940)

WHAT:
- Save module handle during DLL_PROCESS_ATTACH without constructing PIME::ImeModule.
- Lazily instantiate PIME::ImeModule on first use (DllGetClassObject, DllRegisterServer, DllUnregisterServer) under std::mutex protection.
- Add releaseImeModule() helper to encapsulate destruction under mutex lock.
- Only release ImeModule in DLL_PROCESS_DETACH when lpReserved == NULL (dynamic unload).
- Update DllCanUnloadNow() to return S_OK if the module was never initialized without triggering initialization.

WHY:
- Constructing ImeModule in DllMain violates the Windows loader lock contract by performing COM initialization, Shell API calls, and disk I/O under LdrpLoaderLock, which causes deadlocks and crashes in sandboxed processes.
- Deferring construction to COM entry points ensures safe initialization after loader lock is released.
- Avoiding initialization in DllCanUnloadNow prevents COM garbage collection from eagerly loading the module.
- Skipping Release() during process termination prevents teardown deadlocks and crashes.

Generated with assistance from Antigravity (Gemini 3.8 Flash).
@PCMan
PCMan force-pushed the fix/issue-940-defer-imemodule-init branch from 0e40b0c to ac0bab0 Compare October 18, 2026 06:35
@PCMan PCMan self-assigned this Oct 18, 2026
@PCMan
PCMan merged commit f23efd7 into master Oct 18, 2026
4 of 6 checks passed
@PCMan
PCMan deleted the fix/issue-940-defer-imemodule-init branch October 18, 2026 20:36
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.

PIMETextService: defer ImeModule initialization out of DllMain to avoid loader lock issues

1 participant