Skip to content

Fix DLL directory handle retention in Python controller on Windows#6934

Merged
omichel merged 2 commits intofix-python-controllers-on-windowsfrom
copilot/sub-pr-6933
Feb 12, 2026
Merged

Fix DLL directory handle retention in Python controller on Windows#6934
omichel merged 2 commits intofix-python-controllers-on-windowsfrom
copilot/sub-pr-6933

Conversation

Copy link
Contributor

Copilot AI commented Feb 12, 2026

Description
os.add_dll_directory() returns handle objects that must remain alive to keep directories in the DLL search path. The previous implementation discarded these handles, allowing garbage collection to remove the directories before ctypes.cdll.LoadLibrary() executed.

Related Issues
Addresses feedback from #6933: #6933 (comment)

Tasks

  • Store os.add_dll_directory() return values in module-level list
  • Verify Python syntax and code quality
  • Security scan with CodeQL

Changes

  • Added _dll_directory_handles list at module level to retain handle references
  • Modified both os.add_dll_directory() calls to append returned handles to the list
# Before
os.add_dll_directory(dll_dir)

# After
_dll_directory_handles.append(os.add_dll_directory(dll_dir))

Additional context
This prevents potential DLL loading failures on Windows when Python's garbage collector runs between add_dll_directory() and LoadLibrary() calls.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: omichel <1264964+omichel@users.noreply.github.com>
Copilot AI changed the title [WIP] Update Python controllers fix for Windows based on feedback Fix DLL directory handle retention in Python controller on Windows Feb 12, 2026
Copilot AI requested a review from omichel February 12, 2026 15:49
@omichel omichel marked this pull request as ready for review February 12, 2026 15:52
@omichel omichel requested a review from a team as a code owner February 12, 2026 15:52
Copilot AI review requested due to automatic review settings February 12, 2026 15:52
@omichel omichel merged commit 4b1a954 into fix-python-controllers-on-windows Feb 12, 2026
24 checks passed
@omichel omichel deleted the copilot/sub-pr-6933 branch February 12, 2026 15:53
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a critical bug in the Python controller on Windows where DLL directories could be removed from the search path before DLLs were loaded. The os.add_dll_directory() function returns handle objects that must remain referenced to keep directories in the DLL search path. The previous implementation discarded these handles, making them eligible for garbage collection before ctypes.cdll.LoadLibrary() executed.

Changes:

  • Added a module-level list _dll_directory_handles to retain references to DLL directory handles
  • Modified both os.add_dll_directory() calls to append their return values to this list, preventing premature garbage collection

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

omichel added a commit that referenced this pull request Feb 13, 2026
* Fix python controllers on Windows

* Apply suggestion from @Copilot

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Support from Python 3.8 only

* simplified

* Fix DLL directory handle retention in Python controller on Windows (#6934)

* Initial plan

* Store DLL directory handles to prevent garbage collection

Co-authored-by: omichel <1264964+omichel@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: omichel <1264964+omichel@users.noreply.github.com>

* Update changelog for R2025 with Python fix

Added entry for fixing Python controllers on Windows.

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: omichel <1264964+omichel@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants