Skip to content

FIXED: PThreads4W exit crash in MSVC Debug builds#1463

Closed
EricGT wants to merge 1 commit intoSWI-Prolog:masterfrom
EricGT:fix-msvc-pthreads-cleanup
Closed

FIXED: PThreads4W exit crash in MSVC Debug builds#1463
EricGT wants to merge 1 commit intoSWI-Prolog:masterfrom
EricGT:fix-msvc-pthreads-cleanup

Conversation

@EricGT
Copy link
Contributor

@EricGT EricGT commented Feb 14, 2026

Prevents DllMain from accessing freed thread state during process exit. Details in commit message.

Call pthread_win32_process_detach_np() in PL_cleanup() after all
Prolog threads have terminated and memory has been reclaimed.
Without this, PThreads4W's DllMain(DLL_PROCESS_DETACH) accesses
freed thread data, causing ACCESS_VIOLATION in Debug builds where
the CRT fills freed heap with 0xDD.  The explicit cleanup makes
the subsequent DllMain call a no-op.

Fixes crashes in thread-related tests (http, json, pengines, etc.)
when built with MSVC in Debug configuration using PThreads4W from
vcpkg.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@JanWielemaker
Copy link
Member

Combined with the test PR. Also moved detection of this function to CMake as the MinGW thread runtime does not define this and this it breaks the build. Finally adjusted the conditions for running this test as the clib and http packages need not be there.

@EricGT
Copy link
Contributor Author

EricGT commented Feb 14, 2026 via email

@JanWielemaker
Copy link
Member

debug now has a build error

What is the error? I need to check, but traditionally we did not use the generated config.h (when we were using GNU autoconf) because Windows could not run that. With CMake it is generated, but I think we still use the hand crafted version. I'll have a look.

@JanWielemaker
Copy link
Member

I'll have a look.

Seems ok. It includes config/wincfg.h, which sets some stuff, but also includes the normal config.h. So, in theory ( 😄 ) it should work ...

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

@JanWielemaker
Copy link
Member

which I did not like.

Agree

check_symbol_exists

If that works I'm ok with it.

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

@JanWielemaker
Copy link
Member

If you need the details

That is merely the resulting crash during process termination. Anyway, do you make a PR or should I simply update the check in Config.cmake as you propose? If that works, I'm ok with it. Normally I'd try to figure out why it does not work to use the function check by examining the CMake error log. The difference is that the symbol exists merely checks (I think) the header and verifies the symbol is there, while the function check writes a minimal program and verifies this can be compiled and linked.

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

@EricGT
Copy link
Contributor Author

EricGT commented Feb 15, 2026 via email

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