Skip to content

Conversation

staticlibs
Copy link
Contributor

@staticlibs staticlibs commented Jun 20, 2025

On Windows the constexpr constructor of std::mutex, that was supposed to be there since C++11, was not implemented until VS2022. Its implementation has required to use Slim Reader/Writer (SRW) Locks (ref) that are only supported since Window 7, but VS2019 was still supporting Windows Vista as a target (more details from SO).

The implementation of constexpr std::mutex in MSVC has required the ABI breakage in C++ STL that was eventually added in VS2022 17.10 (commit). This commit has introduced the macro
_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR that can be defined to stay ABI compatible with older versions of C++ stdlib. Thus the binaries built with VS2022 17.10 or later and without this macro cannot be run with older versions of C++ stdlib msvcp140.dll even if they do not use constexpr std::mutex - any mutex usage will crash with something like this (more examples and comments from MSFT):

Faulting application name: duckdb.exe, version: 1.3.1.0, time stamp: 0x684fdb82
Faulting module name: MSVCP140.dll, version: 14.36.32532.0, time stamp: 0x04a30cf0
Exception code: 0xc0000005

This PR adds the _DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR macro definition to Windows builds assuming that we are not going to use constexpr constructor of std::mutex and we want to be able to run with msvcp140.dll while being compiled with newer toolchains.

Note, that running binaries, that were built against msvcp140.dll version 14.40+, in environment with earlier versions msvcp140.dll is not officially supported by MSFT (ref). But we don't really have a choice here (unless we want to keep DuckDB itself on VS2019). Extensions (which build should include the engine CMake file being update by this PR) are going to be loaded though JDBC into JVM processes. As of mid 2025, OpenJDK builds from some of major vendors (e.g. Amazon Corretto), including the latest stable JDK 24 (released in March 2025) are still using VS2019 (likely the topic of this issue is one of the reasons for that). And all these JDKs include a vendored copy of msvcp140.dll version 14.29. Due to the way, how Windows shared lib loader works, this vendored C++ stdlib will be loaded by the JVM instead of any system one. And then later when the JVM will load DuckDB extensions shared libs through JDBC - these extensions will also use this pre-loaded version of C++ stdlib, as we do not do static linking for it. In general, LTS JDK versions have 10+ years of production support (for example, JDK 6 released in late 2006 is still supported by some vendors). And even if later updates of LTS JDKs (11, 17, 21, 24 etc) will move to a newer version of MSVC toolchain - the old JDK binaries are going to be still in use for a long time.

Thus this change is proposed as a permanent change - not a temporary workaround.

Testing: additional test run of unittest.exe is added that uses msvcp140.dll from VS2019 instead of the system-provided one.

Fixes: #17971

Edit: added wget installation on Windows CI, removed "1.3" labels as this PR is intended to be the mainline one when v1.3-ossivalis will be merged into the main branch later.

@staticlibs
Copy link
Contributor Author

It appeared that wget is not present by default on Windows runners, will add explicit install now.

staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
On Windows the `constexpr` constructor of `std::mutex`, that was
supposed to be there since C++11, was not implemented until VS2022.
Its implementation has required to use Slim Reader/Writer (SRW) Locks
([ref](https://learn.microsoft.com/en-us/windows/win32/sync/slim-reader-writer--srw--locks))
that are only supported since Window 7, but VS2019 was still supporting
Windows Vista as a target ([more details from SO](https://stackoverflow.com/a/74255773)).

The implementation of `constexpr std::mutex` in MSVC has required the
ABI breakage in C++ STL that was eventually added in VS2022 17.10
([commit](microsoft/STL@4aad4b8)).
This commit has introduced the macro
`_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` that can be defined to stay ABI
compatible with older versions of C++ stdlib. Thus the binaries built
with VS2022 17.10 or later and without this macro cannot be run with
older versions of C++ stdlib `msvcp140.dll` even if they do not use
`constexpr std::mutex` - any mutex usage will crash with something like
this ([more examples and comments from MSFT](https://web.archive.org/web/20250618223106/https://developercommunity.visualstudio.com/t/Access-violation-in-_Thrd_yield-after-up/10664660)):

```
Faulting application name: duckdb.exe, version: 1.3.1.0, time stamp: 0x684fdb82
Faulting module name: MSVCP140.dll, version: 14.36.32532.0, time stamp: 0x04a30cf0
Exception code: 0xc0000005
```

This PR adds the `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` macro
definition to Windows builds assuming that we are not going to use
`constexpr` constructor of `std::mutex` and we want to be able to run
with `msvcp140.dll` while being compiled with newer toolchains.

Note, that running binaries, that were built against `msvcp140.dll`
version `14.40+`, in environment with earlier versions `msvcp140.dll` is
not officially supported by MSFT ([ref](https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=msvc-170)).
But we don't really have a choice here (unless we want to keep DuckDB
itself on VS2019). Extensions (which build should include the engine
CMake file being update by this PR) are going to be loaded though JDBC
into JVM processes. As of mid 2025, OpenJDK builds from some of major
vendors (e.g. Amazon Corretto), including the latest stable JDK 24
(released in March 2025) are still using VS2019 (likely the topic of
this issue is one of the reasons for that). And all these JDKs include a
vendored copy of `msvcp140.dll` version `14.29`. Due to the way, how
Windows shared lib loader works, this vendored C++ stdlib will be loaded
by the JVM instead of any system one. And then later when the JVM will
load DuckDB extensions shared libs through JDBC - these extensions will
also use this pre-loaded version of C++ stdlib, as we do not do static
linking for it. In general, LTS JDK versions have 10+ years of
production support (for example, JDK 6 released in late 2006 is still
supported by some vendors). And even if later updates of LTS JDKs (11,
17, 21, 24 etc) will move to a newer version of MSVC toolchain - the
old JDK binaries are going to be still in use for a long time.

Thus this change is proposed as a permanent change - not a temporary
workaround.

Testing: additional test run of `unittest.exe` is added that uses
`msvcp140.dll` from VS2019 instead of the system-provided one.

Fixes: duckdb#17971
@staticlibs staticlibs changed the title Disable constexpr std::mutex on Windows (1.3) Disable constexpr std::mutex on Windows Jun 20, 2025
@staticlibs staticlibs marked this pull request as draft June 20, 2025 15:32
@staticlibs staticlibs marked this pull request as ready for review June 20, 2025 15:32
@duckdb-draftbot duckdb-draftbot marked this pull request as draft June 20, 2025 15:32
@staticlibs staticlibs marked this pull request as ready for review June 20, 2025 15:36
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
@staticlibs
Copy link
Contributor Author

Windows CI run has passed unittest.exe run with VS2019 C++ stdlib, but has failed later on choco install zip -y --force with the following:

zip package files install completed. Performing other installation steps.
Attempt to get headers for http://fossies.org/windows/misc/zip300xn-x64.zip failed.
  The remote file either doesn't exist, is unauthorized, or is forbidden for url 'http://fossies.org/windows/misc/zip300xn-x64.zip'. Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
Downloading zip 64 bit
  from 'http://fossies.org/windows/misc/zip300xn-x64.zip'
ERROR: The remote file either doesn't exist, is unauthorized, or is forbidden for url 'http://fossies.org/windows/misc/zip300xn-x64.zip'. Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (403) Forbidden." 
This package is likely not broken for licensed users - see https://docs.chocolatey.org/en-us/features/private-cdn.
The install of zip was NOT successful.
Error while running 'C:\ProgramData\chocolatey\lib\zip\tools\chocolateyInstall.ps1'.
 See log for details.

I've filed #17993 as an attempt to fix the zip.exe usage there.

staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 20, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jun 21, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
@Mytherin Mytherin merged commit 39fe13c into duckdb:v1.3-ossivalis Jun 24, 2025
55 of 98 checks passed
@Mytherin
Copy link
Collaborator

Thanks!

github-actions bot pushed a commit to duckdb/duckdb-r that referenced this pull request Jun 27, 2025
Use correct expression function after filter pushdown (duckdb/duckdb#17860)
Disable constexpr std::mutex on Windows (duckdb/duckdb#17991)
github-actions bot added a commit to duckdb/duckdb-r that referenced this pull request Jun 27, 2025
Use correct expression function after filter pushdown (duckdb/duckdb#17860)
Disable constexpr std::mutex on Windows (duckdb/duckdb#17991)

Co-authored-by: krlmlr <krlmlr@users.noreply.github.com>
staticlibs added a commit to staticlibs/duckdb-odbc that referenced this pull request Jul 3, 2025
This is a backport of the PR duckdb#169 to `v1.3-ossivalis` stable branch.

This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to duckdb/duckdb-odbc that referenced this pull request Jul 3, 2025
This is a backport of the PR #169 to `v1.3-ossivalis` stable branch.

This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` flag for Windows
builds that prevents crashes when the host process, into which the
ODBC driver is loaded, is run using older versions of C++ stdlib.

Details: duckdb/duckdb#17991

Testing: additional test run added that uses `msvcp140.dll` from VS2019
instead of the system-provided one.
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 4, 2025
Note: this PR is a draft, please do not merge.

This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 5, 2025
Note: this PR is a draft, please do not merge.

This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 5, 2025
This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.

Testing: it was checked in manual CI runs, that added unit test run
actually causes a crash when `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` is
not used.
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 6, 2025
This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.

Testing: it was checked in manual CI runs, that added unit test run
actually causes a crash when `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` is
not used.
@staticlibs staticlibs deleted the vs2019_mutex_13 branch July 9, 2025 20:55
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 9, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` preprocessor
definition to Python wheel builds on Windows.

For details see duckdb#17991, where the same definition was added for CLI
builds.

Fixes: duckdb#17971
staticlibs added a commit to staticlibs/duckdb that referenced this pull request Jul 9, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` preprocessor
definition to Python wheel builds on Windows.

For details see duckdb#17991, where the same definition was added for CLI
builds.

Fixes: duckdb#17971
Mytherin added a commit that referenced this pull request Jul 10, 2025
This change adds `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` preprocessor
definition to Python wheel builds on Windows.

For details see #17991, where the same definition was added for CLI
builds.

Fixes: #17971
carlopi added a commit to duckdb/vcpkg-duckdb-ports that referenced this pull request Jul 10, 2025
carlopi pushed a commit to carlopi/duckdb that referenced this pull request Jul 10, 2025
This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.

Testing: it was checked in manual CI runs, that added unit test run
actually causes a crash when `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` is
not used.
carlopi added a commit to duckdb/vcpkg-duckdb-ports that referenced this pull request Jul 10, 2025
carlopi pushed a commit to carlopi/duckdb that referenced this pull request Jul 10, 2025
This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.

Testing: it was checked in manual CI runs, that added unit test run
actually causes a crash when `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` is
not used.
carlopi pushed a commit to carlopi/duckdb that referenced this pull request Jul 14, 2025
This change enbales additional run of the test suite for all extensions
using C++ stdlib DLL from Visual Studion 2019. This is done to check
the compatibility with older versions of C++ stdlib introduced in duckdb#17991

New workflow step is only run for `x64-windows-static-md` build arch.

Testing: it was checked in manual CI runs, that added unit test run
actually causes a crash when `_DISABLE_CONSTEXPR_MUTEX_CONSTRUCTOR` is
not used.
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.

2 participants