Fix DLL directory handle retention in Python controller on Windows#6934
Merged
omichel merged 2 commits intofix-python-controllers-on-windowsfrom Feb 12, 2026
Merged
Fix DLL directory handle retention in Python controller on Windows#6934omichel merged 2 commits intofix-python-controllers-on-windowsfrom
omichel merged 2 commits intofix-python-controllers-on-windowsfrom
Conversation
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
Contributor
There was a problem hiding this comment.
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_handlesto 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 beforectypes.cdll.LoadLibrary()executed.Related Issues
Addresses feedback from #6933: #6933 (comment)
Tasks
os.add_dll_directory()return values in module-level listChanges
_dll_directory_handleslist at module level to retain handle referencesos.add_dll_directory()calls to append returned handles to the listAdditional context
This prevents potential DLL loading failures on Windows when Python's garbage collector runs between
add_dll_directory()andLoadLibrary()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.