Skip to content

Using LoadLibraryEX [Reland] - #38302

Closed
peterjc123 wants to merge 4 commits into
masterfrom
ci-all/windows_dll_fix
Closed

peterjc123 wants to merge 4 commits into
masterfrom
ci-all/windows_dll_fix

Conversation

@peterjc123

@peterjc123 peterjc123 commented May 12, 2020 •

Copy link
Copy Markdown
Collaborator

This reverts commit 1ab4f35.

Without this PR, the OS try to find the DLL in the following directories.

  • The directory from which the application loaded.
  • The system directory. Use the GetSystemDirectory function to get the path of this directory.
  • The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
  • The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
  • The current directory.
  • The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

If we use LoadLibraryEx with LOAD_LIBRARY_SEARCH_* flags, the directories are searched in the following order.

  • The directory that contains the DLL (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR). This directory is searched only for dependencies of the DLL to be loaded.
  • The application directory (LOAD_LIBRARY_SEARCH_APPLICATION_DIR).
  • Paths explicitly added to the application search path with the AddDllDirectory function (LOAD_LIBRARY_SEARCH_USER_DIRS) or the SetDllDirectory function. If more than one path has been added, the order in which the paths are searched is unspecified.
  • The System32 directory (LOAD_LIBRARY_SEARCH_SYSTEM32).

Advantages:

  1. The directory that contains the DLL comes first and it's desirable for us, because the dependencies in lib should always be preferred.
  2. The system directory is considered in the last place. According to some of the bug reports, the DLL load failure are caused by loading the conflicting ones in systemroot.

Neural:

  1. The directories in PATH are not considered. Similar things happen as described in the previous point. So it may be beneficial for normal users. However, it may cause failures if there are some new dependencies if built from source. (Resolved by making the fallback to LoadLibraryW if error code is 126)

Disadvantages:

  1. LoadLibraryEx with LOAD_LIBRARY_SEARCH_* flags is only available for Win7/2008 R2 + KB2533623 and up. (Resolved by making the fallback to LoadLibraryW if it is not supported)
  2. Failure during the call of LoadLibraryEx will lead to the OS to pop up a modal dialog, which can block the process if user is using a CLI-only interface. This can be switched off by calling SetErrorMode. (Resolved by calling SetErrorMode)

Test plan:
Test some common cases (in a new repo maybe) including

  1. Python 3.6/3.7/3.8, conda python, conda install
  2. Python 3.6/3.7/3.8, conda python, pip install
  3. Python 3.6/3.7/3.8, official python, pip install
    Plus some corner cases like
  4. Conflicting DLLs in systemroot or PATH
  5. Remove some local dependencies and use global ones

References:

  1. https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode
  2. https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
  3. https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-desktop-applications

…LIBRARY_SEARCH_* flag for loading DLLs o…"

This reverts commit 1ab4f35.
@peterjc123
peterjc123 requested review from ezyang and malfet May 12, 2020 04:53
@dr-ci

dr-ci Bot commented May 12, 2020 •

Copy link
Copy Markdown

💊 CI failures summary and remediations

As of commit d01f449 (more details on the Dr. CI page):


  • 2/2 failures possibly* introduced in this PR
    • 2/2 non-CircleCI failure(s)

ci.pytorch.org: 2 failed


This comment was automatically generated by Dr. CI (expand for details).Follow this link to opt-out of these comments for your Pull Requests.

Please report bugs/suggestions on the GitHub issue tracker.

See how this bot performed.

This comment has been revised 8 times.

Comment thread torch/__init__.py Resolved

@malfet malfet left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@peterjc123 can you please change PR title "Using LoadLibraryEX [Reland]" and change description to original PR description

@peterjc123 peterjc123 changed the title Revert "Revert D21496081: [pytorch][PR] Using LoadLibraryEx and LOAD_… Using LoadLibraryEX [Reland] May 12, 2020
@peterjc123

Copy link
Copy Markdown
Collaborator Author

@malfet Done.

@facebook-github-bot facebook-github-bot 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.

@malfet is landing this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@vadimkantorov

vadimkantorov commented May 12, 2020 •

Copy link
Copy Markdown
Contributor

There may be a similar bug for Linux, in case gcc's multilib/multisys system directories in sysroot contain some important shared library dependency prior to conda install (and at some point there was a similar bug for include paths, system ones were searched before the local directory, so system-wide install of cudnn was preventing conda cudnn from begin found). I don't know if these "potential bugs" currently actually may happen in PyTorch or not

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@malfet merged this pull request in 7eb9f17.

@peterjc123
peterjc123 deleted the ci-all/windows_dll_fix branch May 13, 2020 02:35
peterjc123 added a commit to peterjc123/pytorch that referenced this pull request May 30, 2020
Summary:
This reverts commit 1ab4f35.

Without this PR, the OS try to find the DLL in the following directories.
- The directory from which the application loaded.
- The system directory. Use the GetSystemDirectory function to get the path of this directory.
- The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
- The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
- The current directory.
- The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.

If we use  LoadLibraryEx with LOAD_LIBRARY_SEARCH_* flags, the directories are searched in the following order.

- The directory that contains the DLL (LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR). This directory is searched only for dependencies of the DLL to be loaded.
- The application directory (LOAD_LIBRARY_SEARCH_APPLICATION_DIR).
- Paths explicitly added to the application search path with the AddDllDirectory function (LOAD_LIBRARY_SEARCH_USER_DIRS) or the SetDllDirectory function. If more than one path has been added, the order in which the paths are searched is unspecified.
- The System32 directory (LOAD_LIBRARY_SEARCH_SYSTEM32).

Advantages:
1. The directory that contains the DLL comes first and it's desirable for us, because the dependencies in `lib` should always be preferred.
2. The system directory is considered in the last place. According to some of the bug reports, the DLL load failure are caused by loading the conflicting ones in systemroot.

Neural:
1. The directories in `PATH` are not considered. Similar things happen as described in the previous point. So it may be beneficial for normal users. However, it may cause failures if there are some new dependencies if built from source. (Resolved by making the fallback to `LoadLibraryW` if error code is `126`)

Disadvantages:
1. LoadLibraryEx with LOAD_LIBRARY_SEARCH_* flags is only available for Win7/2008 R2 + KB2533623 and up. (Resolved by making the fallback to `LoadLibraryW` if it is not supported)
2. Failure during the call of `LoadLibraryEx` will lead to the OS to pop up a modal dialog, which can block the process if user is using a CLI-only interface. This can be switched off by calling `SetErrorMode`. (Resolved by calling `SetErrorMode`)
Pull Request resolved: pytorch#38302

Test Plan:
Test some common cases (in a new repo maybe) including
1. Python 3.6/3.7/3.8, conda python, conda install
2. Python 3.6/3.7/3.8, conda python, pip install
3. Python 3.6/3.7/3.8, official python, pip install
Plus some corner cases like
1. Conflicting DLLs in systemroot or `PATH`
2. Remove some local dependencies and use global ones

References:
1. https://docs.microsoft.com/en-us/windows/win32/api/errhandlingapi/nf-errhandlingapi-seterrormode
2. https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
3. https://docs.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order#standard-search-order-for-desktop-applications

Differential Revision: D21524090

Pulled By: malfet

fbshipit-source-id: 0cf5e260c91759b0af8c7aa0950a488e3b653ef5
facebook-github-bot pushed a commit that referenced this pull request Jun 24, 2020
Summary:
Addresses #27840 (comment).
Contains a bunch of fixes (#39376 + #39334 + #38302 + #35362)
Pull Request resolved: #39513

Differential Revision: D22190761

Pulled By: malfet

fbshipit-source-id: b2d52f6cb16c233d16071e9c0670dfff7da2710e
peterjc123 added a commit to peterjc123/pytorch that referenced this pull request Jun 24, 2020
Summary:
Addresses pytorch#27840 (comment).
Contains a bunch of fixes (pytorch#39376 + pytorch#39334 + pytorch#38302 + pytorch#35362)
Pull Request resolved: pytorch#39513

Differential Revision: D22190761

Pulled By: malfet

fbshipit-source-id: b2d52f6cb16c233d16071e9c0670dfff7da2710e
(cherry picked from commit e2201e2)
malfet pushed a commit that referenced this pull request Jun 30, 2020
Summary:
Addresses #27840 (comment).
Contains a bunch of fixes (#39376 + #39334 + #38302 + #35362)
Pull Request resolved: #39513

Differential Revision: D22190761

Pulled By: malfet

fbshipit-source-id: b2d52f6cb16c233d16071e9c0670dfff7da2710e
(cherry picked from commit e2201e2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants